def fetch_archived(req): try: nid = node_id_from_req_path(req) except ValueError: return 400 node = q(Content).get(nid) archive = get_archive_for_node(node) if archive: try: archive.fetch_file_from_archive(node) except: logg.exception("exception in fetch_file_from_archive for archive %s", archive.archive_type) msg = "fetch archive for node failed" req.setStatus(500) req.write(msg) else: req.write('done') else: msg = "archive for node not found" req.setStatus(404) req.write(msg) logg.warn(msg) db.session.commit()
def fetch_archived(req): try: nid = node_id_from_req_path(req) except ValueError: return 400 node = q(Content).get(nid) archive = get_archive_for_node(node) if archive: try: archive.fetch_file_from_archive(node) except: logg.exception( "exception in fetch_file_from_archive for archive %s", archive.archive_type) msg = "fetch archive for node failed" req.setStatus(500) req.write(msg) else: req.write('done') else: msg = "archive for node not found" req.setStatus(404) req.write(msg) logg.warn(msg) db.session.commit()
def _send_thumbnail(thumb_type, req): try: nid = node_id_from_req_path(req) except ValueError: return 400 version_id = version_id_from_req(req) node_or_version = get_node_or_version(nid, version_id, Data) if not node_or_version.has_read_access(): return 404 FileVersion = version_class(File) if version_id: version = node_or_version files = version.files.filter_by( filetype=thumb_type, transaction_id=version.transaction_id).all() if not files: # files may be None if in this version only metadata changed # then try previous transaction_ids files = version.files.filter(FileVersion.filetype==thumb_type, FileVersion.transaction_id<=version.transaction_id). \ order_by(FileVersion.transaction_id.desc()) for f in files: if f.exists: return _request_handler.sendFile(req, f.abspath, f.mimetype) ntype, schema = version.type, version.schema else: # no version id given # XXX: better to use scalar(), but we must ensure that we have no dupes first node = node_or_version for f in node.files.filter_by(filetype=thumb_type): if f.exists: return _request_handler.sendFile(req, f.abspath, f.mimetype) try: ntype, schema = node.type, node.schema except NoResultFound: return 404 for p in _request_handler.getFileStorePaths("/img/"): for test in [ "default_thumb_%s_%s.*" % (ntype, schema), "default_thumb_%s.*" % schema, "default_thumb_%s.*" % ntype ]: fps = glob.glob(os.path.join(p, test)) if fps: thumb_mimetype, thumb_type = utils.utils.getMimeType(fps[0]) return _request_handler.sendFile(req, fps[0], thumb_mimetype, force=1) return _request_handler.sendFile(req, config.basedir + "/web/img/questionmark.png", "image/png", force=1)
def _send_thumbnail(thumb_type, req): try: nid = node_id_from_req_path(req) except ValueError: return 400 version_id = version_id_from_req(req) node_or_version = get_node_or_version(nid, version_id, Data) if not node_or_version.has_read_access(): return 404 FileVersion = version_class(File) if version_id: version = node_or_version files = version.files.filter_by(filetype=thumb_type, transaction_id=version.transaction_id).all() if not files: # files may be None if in this version only metadata changed # then try previous transaction_ids files = version.files.filter(FileVersion.filetype==thumb_type, FileVersion.transaction_id<=version.transaction_id). \ order_by(FileVersion.transaction_id.desc()) for f in files: if f.exists: return req.sendFile(f.abspath, f.mimetype) ntype, schema = version.type, version.schema else: # no version id given # XXX: better to use scalar(), but we must ensure that we have no dupes first node = node_or_version for f in node.files.filter_by(filetype=thumb_type): if f.exists: return req.sendFile(f.abspath, f.mimetype) try: ntype, schema = node.type, node.schema except NoResultFound: return 404 for p in athana.getFileStorePaths("/img/"): for test in ["default_thumb_%s_%s.*" % (ntype, schema), "default_thumb_%s.*" % schema, "default_thumb_%s.*" % ntype]: fps = glob.glob(os.path.join(p, test)) if fps: thumb_mimetype, thumb_type = utils.utils.getMimeType(fps[0]) return req.sendFile(fps[0], thumb_mimetype, force=1) return req.sendFile(config.basedir + "/web/img/questionmark.png", "image/png", force=1)
def send_attachment(req): try: nid = node_id_from_req_path(req) version_id = version_id_from_req(req) except ValueError: return 400 node = get_node_or_version(nid, version_id, Data) if (node is None or isinstance(node, Container) and not node.has_read_access() or isinstance(node, Content) and not node.has_data_access()): return 404 attachment_file = node.files.filter_by(filetype=u"attachment").first() if attachment_file is not None: # filename is attachment.zip sendZipFile(req, file.abspath)
def send_original_file(req): try: nid = node_id_from_req_path(req) except ValueError: return 400 version_id = version_id_from_req(req) node = get_node_or_version(nid, version_id, Data) if node is None or not node.has_data_access(): return 404 original_filetype = node.get_original_filetype() original_file = node.files.filter_by(filetype=original_filetype).scalar() if original_file is not None: return req.sendFile(original_file.abspath, original_file.mimetype) return 404
def _send_file_with_type(filetype, mimetype, req, checkonly=False): try: nid = node_id_from_req_path(req) except ValueError: return 400 version_id = version_id_from_req(req) node = get_node_or_version(nid, version_id, Content) if node is None or not node.has_data_access(): return 404 fileobj = None file_query = node.files.filter_by(filetype=filetype) # if version_id == u"published": if version_id: file_query = file_query.filter_by(transaction_id=node.transaction_id) fileobj = file_query.scalar() # fileobj may be None if in this version only metadata changed # then try previous transaction_ids if not fileobj: FileVersion = version_class(File) # this a long lasting query file_query = node.files.filter_by(filetype=filetype) fileobj = file_query.filter(FileVersion.transaction_id <= node.transaction_id).\ order_by(FileVersion.transaction_id.desc()).first() if mimetype: file_query = file_query.filter_by(mimetype=mimetype) if not fileobj: fileobj = file_query.scalar() if fileobj is not None: if checkonly: return 200 return _request_handler.sendFile(req, fileobj.abspath, fileobj.mimetype) return 404
def _send_file_with_type(filetype, mimetype, req, checkonly=False): try: nid = node_id_from_req_path(req) except ValueError: return 400 version_id = version_id_from_req(req) node = get_node_or_version(nid, version_id, Content) if node is None or not node.has_data_access(): return 404 fileobj = None file_query = node.files.filter_by(filetype=filetype) # if version_id == u"published": if version_id: file_query = file_query.filter_by(transaction_id=node.transaction_id) fileobj = file_query.scalar() # fileobj may be None if in this version only metadata changed # then try previous transaction_ids if not fileobj: FileVersion = version_class(File) # this a long lasting query file_query = node.files.filter_by(filetype=filetype) fileobj = file_query.filter(FileVersion.transaction_id <= node.transaction_id).\ order_by(FileVersion.transaction_id.desc()).first() if mimetype: file_query = file_query.filter_by(mimetype=mimetype) if not fileobj: fileobj = file_query.scalar() if fileobj is not None: if checkonly: return 200 return req.sendFile(fileobj.abspath, fileobj.mimetype) return 404