Skip to content

Commit

Permalink
Merge pull request #9 from ianepperson/filename_first
Browse files Browse the repository at this point in the history
Save methods use filename as first parameter
  • Loading branch information
ianepperson authored Jan 18, 2021
2 parents de2285e + 9bfbb27 commit 0b9e912
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# filestorage
A Python library to make storing files simple and easy.

> :warning: Although there are extensive tests within this project for Python 3.7, 3.8 and 3.9, it is a young project and there may be bugs and security holes. Be sure and test thoroughly prior to use in a production environment.
It is primarily intended to deal with file uploads to a static files directory or an object service like
[AWS S3](https://aws.amazon.com/s3/?nc2=h_ql_prod_st_s3) or [Linode](https://www.linode.com/products/object-storage/).
Files can by stored synchronously (for [WSGI](https://wsgi.readthedocs.io/en/latest/index.html) servers
Expand Down Expand Up @@ -287,14 +289,14 @@ Methods:
* Synchronous methods:
* `exists(filename: str)` - `True` if the given file exists in the store, false otherwise.
* `delete(filename: str)` - Deletes the given file from the store.
* `save_file(data: BinaryIO, filename: str)` - Save the binary IO object to the given file.
* `save_data(data: bytes, filename: str)` - Save the binary data to the given file.
* `save_file(filename: str, data: BinaryIO)` - Save the binary IO object to the given file.
* `save_data(filename: str, data: bytes)` - Save the binary data to the given file.
* `save_field(field: cgi.FieldStorage)` - Save the given field storage object.
* Asynchronous methods: (all will throw a `FilestorageConfigError` if the handler doesn't support async operations.)
* `async_exists(filename: str)` - Awaitable version
* `async_delete(filename: str)` - Awaitable version
* `async_save_file(data: BinaryIO, filename: str)` - Awaitable version
* `async_save_data(data: binary, filename: str)` - Awaitable version
* `async_save_file(filename: str, data: BinaryIO)` - Awaitable version
* `async_save_data(filename: str, data: binary)` - Awaitable version
* `async_save_field(field: cgi.FieldStorage)` - Awaitable version

Abstract Methods to be overridden when sub-classing:
Expand Down
2 changes: 1 addition & 1 deletion filestorage/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.4-alpha2
0.0.5
16 changes: 8 additions & 8 deletions filestorage/handler_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def _save(self, item: FileItem) -> str:
"""
pass

def save_file(self, data: BinaryIO, filename: str) -> str:
def save_file(self, filename: str, data: BinaryIO) -> str:
"""Verifies that the provided filename is legitimate and saves it to
the storage container.
Expand All @@ -180,13 +180,13 @@ def save_field(self, field: 'cgi.FieldStorage') -> str:
raise RuntimeError('No file data in the field')

return self.save_file(
cast(BinaryIO, field.file), field.filename or 'file'
field.filename or 'file', cast(BinaryIO, field.file)
)

def save_data(self, data: bytes, filename: str) -> str:
def save_data(self, filename: str, data: bytes) -> str:
"""Save a file from the byte data provided."""
fileio = BytesIO(data)
return self.save_file(fileio, filename)
return self.save_file(filename, fileio)


class AsyncStorageHandlerBase(StorageHandlerBase, ABC):
Expand Down Expand Up @@ -257,7 +257,7 @@ async def _async_save(self, item: FileItem) -> str:
"""
pass

async def async_save_file(self, data: BinaryIO, filename: str) -> str:
async def async_save_file(self, filename: str, data: BinaryIO) -> str:
"""Verifies that the provided filename is legitimate and saves it to
the storage container.
Expand All @@ -279,13 +279,13 @@ async def async_save_field(self, field: 'cgi.FieldStorage') -> str:
raise RuntimeError('No file data in the field')

return await self.async_save_file(
cast(BinaryIO, field.file), field.filename or 'file'
field.filename or 'file', cast(BinaryIO, field.file)
)

async def async_save_data(self, data: bytes, filename: str) -> str:
async def async_save_data(self, filename: str, data: bytes) -> str:
"""Save a file from the byte data provided."""
fileio = BytesIO(data)
return await self.async_save_file(fileio, filename)
return await self.async_save_file(filename, fileio)


class Folder(AsyncStorageHandlerBase):
Expand Down

0 comments on commit 0b9e912

Please sign in to comment.