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, filename=None, content_type=None)[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)[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)[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)[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
replace(file_or_id, content, filename=None, content_type=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)[source]

Implement StoredFile.close(). DBStoredFile never closes.

static closed()[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=-1)[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, whence=0)[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()[source]

Implement StoredFile.seekable().

tell()[source]

Returns current position of file cursor

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

Implement StoredFile.writable().

class kotti.filedepot.StoredFileResponse(f, request, disposition='attachment', cache_max_age=604800, content_type=None, content_encoding=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, 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='kotti.depot.', settings=None)[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)[source]

Pyramid includeme hook.

Parameters:config (pyramid.config.Configurator) – app config
kotti.filedepot.set_metadata(event)[source]

Set DBStoredFile metadata based on data

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