def get_fallback(self, resource, context): n = len(Path(self.mount_path)) path = Path(context.path)[n:] path = '%s%s' % (self.local_path, path) # 404 Not Found if not isfile(path): return context.set_default_response(404) # 304 Not Modified mtime = getmtime(path) mtime = datetime.utcfromtimestamp(mtime) mtime = mtime.replace(microsecond=0) mtime = fixed_offset(0).localize(mtime) since = context.get_header('If-Modified-Since') if since and since >= mtime: raise NotModified # 200 Ok # FIXME Check we set the encoding for text files mimetype = get_mimetype(basename(path)) # Get data with open(path, 'r') as f: data = f.read() # Response context.status = 200 context.set_content_type(mimetype) context.set_header('Last-Modified', mtime) return data
def GET(self, query, context): n = len(Path(self.mount_path)) path = Path(context.path)[n:] path = '%s%s' % (self.local_path, path) # 404 Not Found if not isfile(path): return context.set_default_response(404) # 304 Not Modified mtime = getmtime(path) mtime = datetime.utcfromtimestamp(mtime) mtime = mtime.replace(microsecond=0) mtime = fixed_offset(0).localize(mtime) since = context.get_header('If-Modified-Since') if since and since >= mtime: raise NotModified # 200 Ok # FIXME Check we set the encoding for text files mimetype = get_mimetype(basename(path)) # Get data with open(path, 'r') as f: data = f.read() # Response context.status = 200 context.set_content_type(mimetype) context.set_header('Last-Modified', mtime) return data
def http_get(self): n = len(Path(self.mount_path)) path = Path(self.path)[n:] path = '%s%s' % (self.local_path, path) # 404 Not Found if not isfile(path): return set_response(self.soup_message, 404) # 304 Not Modified mtime = getmtime(path) mtime = datetime.utcfromtimestamp(mtime) mtime = mtime.replace(microsecond=0) mtime = fixed_offset(0).localize(mtime) since = self.get_header('If-Modified-Since') if since and since >= mtime: return set_response(self.soup_message, 304) # 200 Ok # FIXME Check we set the encoding for text files mimetype = get_mimetype(basename(path)) data = open(path).read() self.soup_message.set_status(200) self.soup_message.set_response(mimetype, data) self.set_header('Last-Modified', mtime)
def _add_image(filename, document, resource): if type(filename) is unicode: filename = filename.encode('UTF-8') data = document.get_part('Pictures/%s' % filename) name, a_type, language = FileName.decode(filename) # Check the filename is good name = checkid(name) if name is None: return None # XXX If the resource exists, we assume it's the good resource if resource.get_resource(name, soft=True) is None: # Get mimetype / class mimetype = get_mimetype(filename) cls = get_context().database.get_resource_class(mimetype) # Add the image resource.make_resource(name, cls, body=data, format=mimetype, filename=filename, extension=a_type) # All OK return name