def doc_title_and_type (doc): """Return title (perhaps unchanged) which reflects the MIME type. For example, a text/plain 'Readme' will be mapped to 'Readme.txt'.""" title = doc.get_metadata("title") if title: filename = re.sub('[^A-Za-z0-9 \._-]+', '_', title.strip(' \t.,-;')) elif doc.original_name(): filename = os.path.basename(doc.original_name()).strip(' \t.,-;') else: filename = doc.id assert '\r' not in filename assert '\n' not in filename # OK, we have a filename, now get the MIME type mtype = doc.get_metadata("apparent-mime-type") if not mtype: # look at the original name oname = doc.original_name() if oname: mtype = get_content_type(oname.lower()) else: odir = doc.originals_path() if os.path.isdir(odir) and _check_for_webpage_complete(odir): mtype = "text/html" if not mtype: # fallback to text/plain mtype = "text/plain" return filename, mtype
def is_good_extension(ext): """Boolean, true if uploading with this file extension could succeed. That is, after mapping the extension to a content-type, we will want extensions/UploadDocument.py's _add_internal to find it in CONTENT_TYPES. """ if ext.startswith('.'): # e.g. might be '' ext = ext[1:] # get_content_type() punts on '.JPG' &c., letting mimetypes.guess_type() deal with it. ext = ext.lower() ct = get_content_type('foo.' + ext) if ct == 'application/octet-stream': return False # This binary type is absolutely unacceptable to _add_internal. return get_extension_for_type(ct) == ext and ext in CONTENT_TYPES.values()
def get_contenttype(doc): """Returns a good MIME content-type for a document, obeying its apparent-mime-type if present.""" filename = doc.get_metadata('title') or doc.original_name() or doc.id ct = get_content_type(filename) if doc.get_metadata('content-type'): ct = doc.get_metadata('content-type') if doc.get_metadata('apparent-mime-type'): ct = doc.get_metadata('apparent-mime-type') if ct == 'application/octet-stream': ct = 'text/plain' # NB: add_document() will get 415 HTTPCodes.UNSUPPORTED_MEDIA_TYPE # if we fail to pick one of the known repo.content_types(), e.g. # an attempt to use 'application/octet-stream'. return ct
def _common(self): if self.allow_cache: # no need to re-fetch doc images or HTML self.set_header('Expires', datetime.datetime.utcnow() + datetime.timedelta(days=EXPIRES_DAYS)) else: # dynamic content self.set_header('Cache-Control', 'no-store') self.set_header('Server', "UpLib/%s (Tornado %s)" % (self.repo.get_version(), TornadoVersion)) # we check to see if this request is on a document. If so, it may # need special handling scheme, netloc, path, params, query, fragment = urlparse.urlparse(self.request.uri) m = DOCPATH.match(path) if m: docid = m.group('docid') if self.repo.valid_doc_id(docid): if TOUCHPATH.match(path): self.repo.touch_doc(docid) # we check to see if the file exists. If not, we invoke the document # object's method to fetch it filepath = os.path.join(self.repo.docs_folder(), path[6:]) if ((not os.path.exists(filepath)) or path[5:].endswith("/contents.txt") or path[5:].endswith("/summary.txt") or path[5:].endswith("/metadata.txt")): doc = self.repo.get_document(docid) bits, mime_type = doc.get_requested_part(path, params, query, fragment) else: try: bits = open(filepath, 'rb').read() mime_type = get_content_type(filepath) except: bits = None mime_type = None raise if bits and mime_type: self.set_header('Content-Type', mime_type) self.set_status(200) self.write(bits) return None
def set_content_type(self, filename, request): request['Content-Type'] = get_content_type(filename) or "application/octet-stream"
def set_content_type(self, filename, request): # override this to provide better types request['Content-Type'] = get_content_type(filename) or "application/octet-stream"