core.modules.file

Module Contents

Classes

injectStoreURLBone

fileBaseSkel

Default file leaf skeleton.

fileNodeSkel

Default file node skeleton.

File

Functions

importBlobFromViur2(dlKey)

thumbnailer(fileSkel, existingFiles, params)

sanitizeFileName(fileName: str) → str

Sanitize the filename so it can be safely downloaded or be embedded into html

decodeFileName(name)

startCheckForUnreferencedBlobs()

Start searching for blob locks that have been recently freed

doCheckForUnreferencedBlobs(cursor=None)

startCleanupDeletedFiles()

Increase deletion counter on each blob currently not referenced and delete

doCleanupDeletedFiles(cursor=None)

Attributes

client

bucket

iamClient

json

html

core.modules.file.client
core.modules.file.bucket
core.modules.file.iamClient
core.modules.file.importBlobFromViur2(dlKey)
class core.modules.file.injectStoreURLBone

Bases: baseBone

unserialize(self, skel, name)
core.modules.file.thumbnailer(fileSkel, existingFiles, params)
core.modules.file.sanitizeFileName(fileName: str) str

Sanitize the filename so it can be safely downloaded or be embedded into html

class core.modules.file.fileBaseSkel

Bases: viur.core.prototypes.tree.TreeSkel

Default file leaf skeleton.

kindName = file
size
dlkey
name
mimetype
weak
pending
width
height
downloadUrl
derived
pendingparententry
def refresh(self):

# Update from blobimportmap try:

oldKeyHash = sha256(self[“dlkey”]).hexdigest().encode(“hex”) res = db.Get(db.Key.from_path(“viur-blobimportmap”, oldKeyHash))

except:

res = None

if res and res[“oldkey”] == self[“dlkey”]:

self[“dlkey”] = res[“newkey”] self[“servingurl”] = res[“servingurl”] logging.info(“Refreshing file dlkey %s (%s)” % (self[“dlkey”], self[“servingurl”]))

else:
if self[“servingurl”]:
try:

self[“servingurl”] = images.get_serving_url(self[“dlkey”])

except Exception as e:

logging.exception(e)

super(fileBaseSkel, self).refresh()

preProcessBlobLocks(self, locks)

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

classmethod refresh(cls, skelValues)
class core.modules.file.fileNodeSkel

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

Bases: viur.core.prototypes.tree.Tree

leafSkelCls
nodeSkelCls
maxuploadsize
uploadHandler = []
adminInfo
blobCacheTime
deleteRecursive(self, parentKey)
signUploadURL(self, mimeTypes: Union[List[str], None] = None, maxSize: Union[int, None] = None, node: Union[str, None] = 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)

Parameters
  • maxSize – The maximum filesize in bytes or None for no limit

  • 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

initializeUpload(self, fileName: str, mimeType: str, node: Union[str, None], size: Union[int, None] = None) Tuple[str, str]

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

getUploadURL(self, fileName, mimeType, size=None, skey=None, *args, **kwargs)
download(self, blobKey, fileName='', download='', sig='', *args, **kwargs)

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

add(self, skelType, node=None, *args, **kwargs)
onItemUploaded(self, skel)
core.modules.file.json = True
core.modules.file.html = True
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)