def get(self, key): filename = os.path.join(self.directory, key) if not os.path.exists(filename): return None with open_file(filename, "rb") as f: return f.read()
def download(context, request): path = open_file(context.filesystem_path).name response = FileResponse(path=path, request=request, content_type=context.mimetype) response.headers['Content-Disposition'] = ('attachment; filename="%s"' % context.filename) return response
def __getitem__(self, key): filename = os.path.join(self.directory, key) if not os.path.exists(filename): raise KeyError() with open_file(filename, "rb") as f: return f.read()
def download(context, request): path = open_file(context.filesystem_path).name mimetype = context.mimetype or 'application/octet-stream' response = FileResponse(path=path, request=request, content_type=mimetype.encode('utf8')) response.headers['Content-Disposition'] = ('inline; filename="%s"' % context.filename.encode('utf8')) return response
def read(self, id): """ Get the data for an object with the given ID. :param id: ID of the file object :type id: unicode :result: Data / value of the file object :rtype: """ f = open_file(self.path(id), mode='r') return f.read()
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 _write(self, key, data): filename = os.path.join(self.directory, key) with open_file(filename, "wb") as f: return f.write(data)
def data(self): with open_file(self.filesystem_path, 'rb') as fd: data = fd.read() # beware, this will load all data into memory! return data