core.modules.file

Module Contents

Classes

InjectStoreURLBone

The BaseBone class serves as the base class for all bone types in the ViUR framework.

FileBaseSkel

Default file leaf skeleton.

FileNodeSkel

Default file node skeleton.

File

Tree module prototype.

Functions

importBlobFromViur2(dlKey, fileName)

thumbnailer(fileSkel, existingFiles, params)

cloudfunction_thumbnailer(fileSkel, existingFiles, params)

External Thumbnailer for images.

decodeFileName(name)

startCheckForUnreferencedBlobs()

Start searching for blob locks that have been recently freed

doCheckForUnreferencedBlobs([cursor])

startCleanupDeletedFiles()

Increase deletion counter on each blob currently not referenced and delete

doCleanupDeletedFiles([cursor])

start_delete_pending_files()

Start deletion of pending FileSkels that are older than 7 days.

Attributes

client

bucket

iamClient

core.modules.file.client
core.modules.file.bucket
core.modules.file.iamClient
core.modules.file.importBlobFromViur2(dlKey, fileName)
class core.modules.file.InjectStoreURLBone(*, compute=None, defaultValue=None, descr='', getEmptyValueFunc=None, indexed=True, isEmptyFunc=None, languages=None, multiple=False, params=None, readOnly=None, required=False, searchable=False, unique=None, vfunc=None, visible=True)

Bases: viur.core.bones.BaseBone

The BaseBone class serves as the base class for all bone types in the ViUR framework. It defines the core functionality and properties that all bones should implement.

Parameters:
  • descr (str) – Textual, human-readable description of that bone. Will be translated.

  • defaultValue (Any) – If set, this bone will be preinitialized with this value

  • required (Union[bool, List[str], Tuple[str]]) – If True, the user must enter a valid value for this bone (the viur.core refuses to save the skeleton otherwise). If a list/tuple of languages (strings) is provided, these language must be entered.

  • multiple (Union[bool, MultipleConstraints]) – If True, multiple values can be given. (ie. n:m relations instead of n:1)

  • searchable (bool) – If True, this bone will be included in the fulltext search. Can be used without the need of also been indexed.

  • vfunc (callable) – If given, a callable validating the user-supplied value for this bone. This callable must return None if the value is valid, a String containing an meaningful error-message for the user otherwise.

  • readOnly (bool) – If True, the user is unable to change the value of this bone. If a value for this bone is given along the POST-Request during Add/Edit, this value will be ignored. Its still possible for the developer to modify this value by assigning skel.bone.value.

  • visible (bool) – If False, the value of this bone should be hidden from the user. This does not protect the value from being exposed in a template, nor from being transferred to the client (ie to the admin or as hidden-value in html-form)

  • compute (Compute) –

    If set, the bone’s value will be computed in the given method.

    Note

    The kwarg ‘multiple’ is not supported by all bones

  • getEmptyValueFunc (callable) –

  • indexed (bool) –

  • isEmptyFunc (callable) –

  • languages (Union[None, List[str]]) –

  • params (Dict) –

  • unique (Union[None, UniqueValue]) –

Initializes a new Bone.

unserialize(skel, name)

Deserialize bone data from the datastore and populate the bone with the deserialized values.

This function is the inverse of the serialize function. It converts data from the datastore into a format that can be used by the bones in the skeleton.

Parameters:
  • skel – A SkeletonInstance object containing the values to be deserialized.

  • name – The property name of the bone in its Skeleton (not the description).

Returns:

True if deserialization is successful, False otherwise.

core.modules.file.thumbnailer(fileSkel, existingFiles, params)
core.modules.file.cloudfunction_thumbnailer(fileSkel, existingFiles, params)

External Thumbnailer for images.

The corresponding cloudfunction can be found here . https://github.com/viur-framework/viur-cloudfunctions/tree/main/thumbnailer

You can use it like so: main.py:

from viur.core.modules.file import cloudfunction_thumbnailer

conf[“viur.file.thumbnailerURL”]=”https://xxxxx.cloudfunctions.net/imagerenderer” conf[“viur.file.derivers”] = {“thumbnail”: cloudfunction_thumbnailer}

conf[“derives_pdf”] = { “thumbnail”: [{“width”: 1920,”sites”:”1,2”}] } skeletons/xxx.py:

test = FileBone(derive=conf[“derives_pdf”])

class core.modules.file.FileBaseSkel(*args, **kwargs)

Bases: viur.core.prototypes.tree.TreeSkel

Default file leaf skeleton.

kindName = 'file'
size
dlkey
name
mimetype
weak
pending
width
height
downloadUrl
derived
pendingparententry
preProcessBlobLocks(locks)

Ensure that our dlkey is locked even if we don’t have a filebone here

classmethod refresh(skelValues)

Refresh the bones current content.

This function causes a refresh of all relational bones and their associated information.

class core.modules.file.FileNodeSkel(*args, **kwargs)

Bases: viur.core.prototypes.tree.TreeSkel

Default file node skeleton.

kindName = 'file_rootNode'
name
rootNode
core.modules.file.decodeFileName(name)
class core.modules.file.File(moduleName, modulePath, *args, **kwargs)

Bases: viur.core.prototypes.tree.Tree

Tree module prototype.

It is used for hierarchical structures, either as a tree with nodes and leafs, or as a hierarchy with nodes only.

leafSkelCls
nodeSkelCls
maxuploadsize
uploadHandler = []
handler = 'tree.simple.file'
adminInfo
roles
blobCacheTime
write(filename, content, mimetype='text/plain', width=None, height=None)

Write a file from any buffer into the file module.

Parameters:
  • filename (str) – Filename to be written.

  • content (Any) – The file content to be written, as bytes-like object.

  • mimetype (str) – The file’s mimetype.

  • width (int) – Optional width information for the file.

  • height (int) – Optional height information for the file.

Returns:

Returns the key of the file object written. This can be associated e.g. with a FileBone.

Return type:

viur.core.db.Key

deleteRecursive(parentKey)

Recursively processes a delete request.

This will delete all entries which are children of nodeKey, except key nodeKey.

Parameters:

parentKey – URL-safe key of the node which children should be deleted.

signUploadURL(mimeTypes=None, maxSize=None, node=None)

Internal helper that will create a signed upload-url that can be used to retrieve an uploadURL from getUploadURL for guests / users without having file/add permissions. This URL is valid for an hour and can be used to upload multiple files. :param mimeTypes: A list of valid mimetypes that can be uploaded (wildcards like “image/*” are supported) or None (no restriction on filetypes) :param maxSize: The maximum filesize in bytes or None for no limit :param node: The (string encoded) key of a file-leaf (=directory) where this file will be uploaded into or None (the file will then not show up in the filebrowser).

Warning

If node is set it’s the callers responsibility to ensure node is a valid key and that the user has the permission to upload into that directory. ViUR does not enforce any canAccess restrictions for keys passed to this function!

Returns:

authData and authSig for the getUploadURL function below

Parameters:
  • mimeTypes (Union[List[str], None]) –

  • maxSize (Union[int, None]) –

  • node (Union[str, None]) –

initializeUpload(fileName, mimeType, node, size=None)

Internal helper that registers a new upload. Will create the pending fileSkel entry (needed to remove any started uploads from GCS if that file isn’t used) and creates a resumable (and signed) uploadURL for that. :param fileName: Name of the file that will be uploaded :param mimeType: Mimetype of said file :param node: If set (to a string-key representation of a file-node) the upload will be written to this directory :param size: The exact filesize we’re accepting in Bytes. Used to enforce a filesize limit by getUploadURL :return: Str-Key of the new file-leaf entry, the signed upload-url

Parameters:
  • fileName (str) –

  • mimeType (str) –

  • node (Union[str, None]) –

  • size (Union[int, None]) –

Return type:

Tuple[str, str]

getUploadURL(fileName, mimeType, size=None, *args, **kwargs)
Parameters:
  • fileName (str) –

  • mimeType (str) –

  • size (int) –

download(blobKey, fileName='', download='', sig='', *args, **kwargs)

Download a file. :param blobKey: The unique blob key of the file. :param fileName: Optional filename to provide in the header. :param download: Set header to attachment retrival, set explictly to “1” if download is wanted.

Parameters:
  • blobKey (str) –

  • fileName (str) –

  • download (str) –

  • sig (str) –

add(skelType, node=None, *args, **kwargs)

Add a new entry with the given parent node, and render the entry, eventually with error notes on incorrect data. Data is taken by any other arguments in kwargs.

The function performs several access control checks on the requested entity before it is added.

See also

canAdd(), onAdd(), , onAdded()

Parameters:
  • skelType (viur.core.prototypes.tree.SkelType) – Defines the type of the new entry and may either be “node” or “leaf”.

  • node (viur.core.db.Key | int | str | None) – URL-safe key of the parent.

Returns:

The rendered, added object of the entry, eventually with error hints.

Raises:

viur.core.errors.NotAcceptable, when no valid skelType was provided.

Raises:

viur.core.errors.NotFound, when no valid node was found.

Raises:

viur.core.errors.Unauthorized, if the current user does not have the required permissions.

Raises:

viur.core.errors.PreconditionFailed, if the skey could not be verified.

onEdit(skelType, skel)

Hook function that is called before editing an entry.

It can be overridden for a module-specific behavior.

Parameters:
  • skelType (viur.core.prototypes.tree.SkelType) – Defines the type of the node that shall be edited.

  • skel (viur.core.skeleton.SkeletonInstance) – The Skeleton that is going to be edited.

See also

edit(), onEdited()

onItemUploaded(skel)
core.modules.file.startCheckForUnreferencedBlobs()

Start searching for blob locks that have been recently freed

core.modules.file.doCheckForUnreferencedBlobs(cursor=None)
core.modules.file.startCleanupDeletedFiles()

Increase deletion counter on each blob currently not referenced and delete it if that counter reaches maxIterCount

core.modules.file.doCleanupDeletedFiles(cursor=None)
core.modules.file.start_delete_pending_files()

Start deletion of pending FileSkels that are older than 7 days.