def update(self, filename, mimetype=None, data=None, id=None): self.filename = filename self.mimetype = mimetype if data is not None: _, extension = splitext(filename) self.path = self.generate_path(extension) self.size = len(data) with create_file(self.filesystem_path, 'wb') as fd: fd.write(data)
def update(self, filename=None, data=None, uuid=None, **kw): super(File, self).update(**kw) if filename is not None: self.filename = filename if data is not None: base64 = match(r'^data:([\w/]+);base64,(.*)', data) if base64 is not None: self.mimetype, data = base64.groups() data = decodestring(data) self.size = len(data) with create_file(self.filesystem_path, 'wb') as fd: fd.write(data)
def set_image(self, data=None, filename=None): if data is None: self.path = filename or '/dev/null' return img_dir = os.path.join(self.root_path, str(self.album_id)) if not os.path.isdir(img_dir): os.mkdir(img_dir) self.path = os.path.join(str(self.album_id), filename) img = create_file(self.filesystem_path, 'wb') if hasattr(img, 'fileno'): os.fchmod(img.fileno(), 0644) img.write(data) img.close()
def update(self, filename, mimetype=None, data=None, id=None): self.filename = filename self.mimetype = mimetype if data is not None: _, extension = splitext(filename) self.path = self.generate_path(extension) base64 = match(r'^data:([\w/]+);base64,(.*)', data) if base64 is not None: self.mimetype, data = base64.groups() data = decodestring(data) self.size = len(data) with create_file(self.filesystem_path, 'wb') as fd: fd.write(data)
def _set_content(self, data, filename=None): extension = None if filename: extension = os.path.splitext(filename)[1] if not extension: extension = extension_for_image_data(BytesIO(data)) self.path = generate_path(extension) file = create_file(self.filesystem_path, 'wb') if hasattr(file, 'fileno'): # pragma: no cover (for testing only) try: os.fchmod(file.fileno(), 0o644) except io.UnsupportedOperation: # BytesIO for testing pass file.write(data) file.close()
def download(self, timeout=0.5): # pragma: no cover """Download a remote image so we have a local copy. """ if self.path is not None: raise TypeError('Image already has local data.') r = requests.get(self.url, timeout=timeout) extension = extension_for_image_data(BytesIO(r.content)) self.path = generate_path(extension) file = create_file(self.filesystem_path, 'wb') if hasattr(file, 'fileno'): # pragma: no cover (for testing only) try: os.fchmod(file.fileno(), 0o644) except io.UnsupportedOperation: # BytesIO for testing pass file.write(r.content) file.close()
def write(self, data): """ Create or update an object with the given ``id`` and write ``data`` to its contents. :param data: Data / value of the file object :type data: ??? :result: ID for the file object :rtype: str """ id = uuid4() # TODO: call create_directory during transaction # check if the filepath and the tempdir exist and create if necessary create_directory(os.path.split(self.path(id))[0]) create_directory(self.path()) f = create_file(self.path(id), mode='w', tempdir=self.path()) f.write(data) f.close() return str(id)
def __init__(self, image, width=None, height=None, crop=False, strip_whitespace=False): self.image_id = image.id self.param_width = width or 0 self.param_height = height or 0 self.param_crop = crop self.param_strip_whitespace = strip_whitespace file = open_file(image.filesystem_path, 'rb') (data, format, size) = scale_image(file, width, height, crop, strip_whitespace) self.path = generate_path('.' + format.lower()) self.width = size[0] self.height = size[1] file = create_file(self.filesystem_path, 'wb') if hasattr(file, 'fileno'): # pragma: no cover (for testing only) try: os.fchmod(file.fileno(), 0o644) except io.UnsupportedOperation: # BytesIO for testing pass file.write(data) file.close()
def _callFUT(self, *a, **kw): from repoze.filesafe import create_file return create_file(*a, **kw)
def getFile(self, fn): if fn: return create_file(fn, 'w+b', TMPDIR) return tempfile.TemporaryFile()