def attachments_detach(req, object_type, object_id, path, webid): check_login(req) match_right(req, [R_ADMIN, 'attachments_author']) check_origin(req) attachment = Attachment(Attachment.web_to_id(webid)) attachment.detach(req, object_type, object_id) if attachment.delete(req) is None: raise SERVER_RETURN(state.HTTP_NOT_FOUND) return js_items(req, object_type=object_type, object_id=object_id)
def attachments_download(req, path, webid): attachment = Attachment(Attachment.web_to_id(webid)) attachment.get(req) req.headers_out.add_header( 'Content-Disposition', 'attachment', # filename=attachment.file_name.encode('ascii','xmlcharrefreplace')) filename=attachment.file_name.encode('ascii', 'backslashreplace')) return send_file(req, req.cfg.attachments_path + '/' + path + '/' + webid, content_type=attachment.mime_type)
def thumb_images(req, job): items = Attachment.list_images(req) i, length = 0, len(items) for item in items: item.thumb(req) i += 1 job.mod(req, progress=i*100/length)
def attachments_resize(req, path, webid, width, height): attachment = Attachment(Attachment.web_to_id(webid)) last_modified = Attachment.last_modified(req, webid) if last_modified is None: # file not found raise SERVER_RETURN(state.HTTP_NOT_FOUND) if 'If-Modified-Since' in req.headers_in: # check cache header hdt = datetime.strptime(req.headers_in['If-Modified-Since'], time_format) if last_modified <= hdt: req.headers_out.add_header('Last-Modified', last_modified.strftime(time_format)) req.headers_out.add_header('Date', datetime.utcnow().strftime(time_format)) raise SERVER_RETURN(state.HTTP_NOT_MODIFIED) attachment.get(req) # check resize hash if req.args.get('hash', '') != attachment.resize_hash("%dx%d" % (width, height)): raise SERVER_RETURN(state.HTTP_FORBIDDEN) retval = attachment.resize(req, width, height) if retval is None: # it is not image raise SERVER_RETURN(state.HTTP_BAD_REQUEST) req.content_type = attachment.mime_type req.clength = len(retval) req.headers_out.add_header('Last-Modified', last_modified.strftime(time_format)) return retval
def js_items(req, **kwargs): # size of thumb could be set from request thumb_size = req.args.getfirst('thumb_size', '320x200', str) pager = Pager(limit=-1) items = [] for item in Attachment.list(req, pager, **kwargs): item.webname = item.webname() item.resize_hash = item.resize_hash(thumb_size) items.append(item.__dict__) req.content_type = 'application/json' return json.dumps({'items': items})
def admin_attachments(req): check_login(req) check_right(req, R_ADMIN) pager = Pager(order='timestamp', sort='desc') pager.bind(req.args) kwargs = {} if 'obty' in req.args: kwargs['object_type'] = req.args.getfirst('obty', fce=uni) or None pager.set_params(obty=kwargs['object_type']) if 'obid' in req.args: kwargs['object_id'] = req.args.getfirst('obid', fce=int) pager.set_params(obid=kwargs['object_id']) rows = Attachment.list(req, pager, **kwargs) return generate_page(req, "admin/attachments.html", pager=pager, rows=rows)
def admin_attachments_add_update(req, id=None): check_login(req) match_right(req, [R_ADMIN, 'attachments_author']) check_origin(req) attachment = Attachment() attachment.bind(req.form, req.login.id) status = attachment.add(req) if not status == attachment: req.status = state.HTTP_BAD_REQUEST req.content_type = 'application/json' return json.dumps({'reason': Attachment.error(status)}) req.content_type = 'application/json' return json.dumps({'attachment': attachment.dumps()})
def attachments_normal(req, path, webid): attachment = Attachment(Attachment.web_to_id(webid)) attachment.get(req) return send_file(req, req.cfg.attachments_path + '/' + path + '/' + webid, content_type=attachment.mime_type)