kotti.filedepot

class kotti.filedepot.DBFileStorage[source]

Implementation of depot.io.interfaces.FileStorage,

Uses kotti.filedepot.DBStoredFile to store blob data in an SQL database.

create(content: Union[bytes, cgi.FieldStorage], filename: Optional[str] = None, content_type: Optional[str] = None) → str[source]

Saves a new file and returns the file id

Parameters:
  • content – can either be bytes, another file object or a cgi.FieldStorage. When filename and content_type parameters are not provided they are deducted from the content itself.
  • filename (string) – filename for this file
  • content_type (string) – Mimetype of this file
Returns:

the unique file_id associated to this file

Return type:

string

delete(file_or_id: str) → None[source]

Deletes a file. If the file didn’t exist it will just do nothing.

Parameters:file_or_id – can be either DBStoredFile or a file_id
exists(file_or_id: str) → bool[source]

Returns if a file or its ID still exist.

Returns:Returns if a file or its ID still exist.
Return type:bool
static get(file_id: str) → kotti.filedepot.DBStoredFile[source]

Returns the file given by the file_id

Parameters:file_id (string) – the unique id associated to the file
Result:a kotti.filedepot.DBStoredFile instance
Return type:kotti.filedepot.DBStoredFile
static list(*args) → None[source]

Returns a list of file IDs that exist in the Storage.

Depending on the implementation there is the possibility that this returns more IDs than there have been created. Therefore this method is NOT guaranteed to be RELIABLE.

replace(file_or_id: Union[kotti.filedepot.DBStoredFile, str], content: bytes, filename: Optional[str] = None, content_type: Optional[str] = None) → None[source]

Replaces an existing file, an IOError is raised if the file didn’t already exist.

Given a StoredFile or its ID it will replace the current content with the provided content value. If filename and content_type are provided or can be deducted by the content itself they will also replace the previous values, otherwise the current values are kept.

Parameters:
  • file_or_id – can be either DBStoredFile or a file_id
  • content – can either be bytes, another file object or a cgi.FieldStorage. When filename and content_type parameters are not provided they are deducted from the content itself.
  • filename (string) – filename for this file
  • content_type (string) – Mimetype of this file
class kotti.filedepot.DBStoredFile(file_id, filename=None, content_type=None, last_modified=None, content_length=None, **kwds)[source]

depot.io.interfaces.StoredFile implementation that stores file data in SQL database.

Can be used together with kotti.filedepot.DBFileStorage to implement blobs storage in the database.

static close(*args, **kwargs) → None[source]

Implement StoredFile.close(). DBStoredFile never closes.

static closed() → bool[source]

Implement StoredFile.closed().

content_length

Size of the blob in bytes (sqlalchemy.types.Integer)

content_type

MIME type of the blob (sqlalchemy.types.String)

data

The binary data itself (sqlalchemy.types.LargeBinary)

file_id

Unique file id given to this blob (sqlalchemy.types.String)

filename

The original filename it had when it was uploaded. (sqlalchemy.types.String)

id

Primary key column in the DB (sqlalchemy.types.Integer)

last_modified

Date / time the blob was created or last modified (sqlalchemy.types.DateTime)

name

Implement StoredFile.name().

Result:the filename of the saved file
Return type:string
read(n: int = -1) → bytes[source]

Reads n bytes from the file.

If n is not specified or is -1 the whole file content is read in memory and returned

seek(offset: int, whence: int = 0) → None[source]

Change stream position.

Change the stream position to the given byte offset. The offset is interpreted relative to the position indicated by whence.

Parameters:
  • offset (int) – Position for the cursor
  • whence (int) –
    • 0 – start of stream (the default);
      offset should be zero or positive
    • 1 – current stream position; offset may be negative
    • 2 – end of stream; offset is usually negative
static seekable() → bool[source]

Implement StoredFile.seekable().

tell() → int[source]

Returns current position of file cursor

Result:Current file cursor position.
Return type:int
static writable() → bool[source]

Implement StoredFile.writable().

class kotti.filedepot.StoredFileResponse(f: depot.io.memory.MemoryStoredFile, request: kotti.request.Request, disposition: str = 'attachment', cache_max_age: int = 604800, content_type: None = None, content_encoding: None = None)[source]

A Response object that can be used to serve an UploadedFile instance.

Code adapted from pyramid.response.FileResponse.

class kotti.filedepot.TweenFactory(handler: Optional[Callable], registry: pyramid.registry.Registry)[source]

Factory for a Pyramid tween in charge of serving Depot files.

This is the Pyramid tween version of depot.middleware.DepotMiddleware. It does exactly the same as Depot’s WSGI middleware, but operates on a pyramid.request.Request object instead of the WSGI environment.

kotti.filedepot.extract_depot_settings(prefix: Optional[str] = 'kotti.depot.', settings: Optional[Dict[str, str]] = None) → List[Dict[str, str]][source]

Merges items from a dictionary that have keys that start with prefix to a list of dictionaries.

Parameters:
  • prefix (string) – A dotted string representing the prefix for the common values
  • settings – A dictionary with settings. Result is extracted from this
kotti.filedepot.includeme(config: pyramid.config.Configurator) → None[source]

Pyramid includeme hook.

Parameters:config (pyramid.config.Configurator) – app config
kotti.filedepot.set_metadata(event: Union[kotti.events.ObjectUpdate, kotti.events.ObjectInsert]) → None[source]

Set DBStoredFile metadata based on data

Parameters:event (ObjectInsert or ObjectUpdate) – event that triggered this handler.