def save(self, name, fobj, max_length=None, blob_object=None): if not blob_object: blob = Blob(name, self.bucket) else: blob = blob_object # force the current file to be at file location 0, to # because that's what google wants # determine the current file's mimetype based on the name content_type = self._determine_content_type(name) fobj.seek(0) if self._is_file_empty(fobj): logging.warning( "Stopping the upload of an empty file: {}".format(name)) return name # set a max-age of 5 if we're uploading to content/databases if self.is_database_file(name): blob.cache_control = 'private, max-age={}, no-transform'.format( CONTENT_DATABASES_MAX_AGE) blob.upload_from_file( fobj, content_type=content_type, ) return name
def save_file(self, file, filename, folder=None, randomize=False, extensions=None, acl=None, replace=False, headers=None): """ :param filename: local filename :param folder: relative path of sub-folder :param randomize: randomize the filename :param extensions: iterable of allowed extensions, if not default :param acl: ACL policy (if None then uses default) :returns: modified filename """ extensions = extensions or self.extensions if not self.filename_allowed(filename, extensions): raise FileNotAllowed() filename = utils.secure_filename(os.path.basename(filename)) if randomize: filename = utils.random_filename(filename) if folder: filename = folder + "/" + filename content_type, _ = mimetypes.guess_type(filename) content_type = content_type or 'application/octet-stream' blob = self.get_bucket().get_blob(filename) # If the file exist and we explicitely asked not to replace it: ignore it. if blob and not replace: return filename # If the file doesn't exist: create it. if not blob: blob = Blob(filename, self.get_bucket()) blob.cache_control = self.cache_control file.seek(0) acl = acl or self.acl blob.upload_from_file(file, rewind=True, content_type=content_type, predefined_acl=acl) return filename
def save(self, name, fobj, max_length=None, blob_object=None): if not blob_object: blob = Blob(name, self.bucket) else: blob = blob_object buffer = None # set a max-age of 5 if we're uploading to content/databases if self.is_database_file(name): blob.cache_control = "private, max-age={}, no-transform".format( CONTENT_DATABASES_MAX_AGE) # Compress the database file so that users can save bandwith and download faster. buffer = BytesIO() compressed = GzipFile(fileobj=buffer, mode="w") compressed.write(fobj.read()) compressed.close() blob.content_encoding = "gzip" fobj = buffer # determine the current file's mimetype based on the name # import determine_content_type lazily in here, so we don't get into an infinite loop with circular dependencies from contentcuration.utils.storage_common import determine_content_type content_type = determine_content_type(name) # force the current file to be at file location 0, to # because that's what google wants fobj.seek(0) if self._is_file_empty(fobj): logging.warning( "Stopping the upload of an empty file: {}".format(name)) return name blob.upload_from_file( fobj, content_type=content_type, ) # Close StringIO object and discard memory buffer if created if buffer: buffer.close() return name
def save_file(self, file, filename, folder=None, randomize=False, extensions=None, acl=None, replace=False, headers=None): """ :param filename: local filename :param folder: relative path of sub-folder :param randomize: randomize the filename :param extensions: iterable of allowed extensions, if not default :param acl: ACL policy (if None then uses default) :returns: modified filename """ extensions = extensions or self.extensions if not self.filename_allowed(filename, extensions): raise FileNotAllowed() filename = utils.secure_filename( os.path.basename(filename) ) if randomize: filename = utils.random_filename(filename) if folder: filename = folder + "/" + filename content_type, _ = mimetypes.guess_type(filename) content_type = content_type or 'application/octet-stream' blob = self.get_bucket().get_blob(filename) # If the file exist and we explicitely asked not to replace it: ignore it. if blob and not replace: return filename # If the file doesn't exist: create it. if not blob: blob = Blob(filename, self.get_bucket()) blob.cache_control = self.cache_control file.seek(0) acl = acl or self.acl blob.upload_from_file(file, rewind=True, content_type=content_type, predefined_acl=acl) return filename