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
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.

close(*args, **kwargs)[source]

Implement StoredFile.close(). DBStoredFile never closes.

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
public_url

Integration with depot.middleware.DepotMiddleware

When supported by the storage this will provide the public url to which the file content can be accessed. In case this returns None it means that the file can only be served by the depot.middleware.DepotMiddleware itself.

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(n)[source]

Move the file cursor to position n

Parameters:n (int) – Position for the cursor
seekable()[source]

Implement StoredFile.seekable().

tell()[source]

Returns current position of file cursor

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

Implement StoredFile.writable().

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.