def scale_animal_images(dbo): """ Goes through all animal images in the database and scales them to the current incoming media scaling factor. """ mp = db.query( dbo, "SELECT MediaName FROM media WHERE LOWER(MediaName) LIKE '%.jpg' AND LinkTypeID = 0" ) for i, m in enumerate(mp): filepath = db.query_string( dbo, "SELECT Path FROM dbfs WHERE Name='%s'" % m["MEDIANAME"]) name = str(m["MEDIANAME"]) inputfile = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) outputfile = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) odata = dbfs.get_string(dbo, name) inputfile.write(odata) inputfile.flush() inputfile.close() outputfile.close() al.debug("scaling %s (%d of %d)" % (name, i, len(mp)), "media.scale_animal_images", dbo) scale_image_file(inputfile.name, outputfile.name, configuration.incoming_media_scaling(dbo)) f = open(outputfile.name, "r") data = f.read() f.close() os.unlink(inputfile.name) os.unlink(outputfile.name) # Update the image file data dbfs.put_string(dbo, name, filepath, data) al.debug("scaled %d images" % len(mp), "media.scale_animal_images", dbo)
def create_document_media(dbo, username, linktype, linkid, template, content): """ Creates a new media record for a document for the link given. linktype: ANIMAL, PERSON, etc linkid: ID for the link template: The name of the template used to create the document content: The document contents """ mediaid = db.get_id(dbo, "media") sql = db.make_insert_sql( "media", (("ID", db.di(mediaid)), ("MediaName", db.ds("%d.html" % mediaid)), ("MediaType", db.di(0)), ("MediaNotes", db.ds(template)), ("WebsitePhoto", db.di(0)), ("WebsiteVideo", db.di(0)), ("DocPhoto", db.di(0)), ("ExcludeFromPublish", db.di(0)), ("NewSinceLastPublish", db.di(1)), ("UpdatedSinceLastPublish", db.di(0)), ("LinkID", db.di(linkid)), ("LinkTypeID", db.di(linktype)), ("Date", db.nowsql()))) db.execute(dbo, sql) path = "" if linktype == ANIMAL: path = "/animal" elif linktype == PERSON: path = "/owner" elif linktype == LOSTANIMAL: path = "/lostanimal" elif linktype == FOUNDANIMAL: path = "/foundanimal" path += "/" + str(linkid) name = str(mediaid) + ".html" dbfs.put_string(dbo, name, path, content) audit.create(dbo, username, "media", str(mediaid) + ": for " + str(linkid) + "/" + str(linktype))
def check_and_scale_pdfs(dbo, force = False): """ Goes through all PDFs in the database to see if they have been scaled (have a suffix of _scaled.pdf) and scales down any unscaled ones. If force is set, then all PDFs are checked and scaled again even if they've been scaled before. """ if not configuration.scale_pdfs(dbo): al.warn("ScalePDFs config option disabled in this database, not scaling pdfs", "media.check_and_scale_pdfs", dbo) return if force: mp = db.query(dbo, \ "SELECT ID, MediaName FROM media WHERE LOWER(MediaName) LIKE '%.pdf' ORDER BY ID DESC") else: mp = db.query(dbo, \ "SELECT ID, MediaName FROM media WHERE LOWER(MediaName) LIKE '%.pdf' AND " \ "LOWER(MediaName) NOT LIKE '%_scaled.pdf' ORDER BY ID DESC") for i, m in enumerate(mp): filepath = db.query_string(dbo, "SELECT Path FROM dbfs WHERE Name='%s'" % m["MEDIANAME"]) original_name = str(m["MEDIANAME"]) new_name = str(m["ID"]) + "_scaled.pdf" odata = dbfs.get_string(dbo, original_name) data = scale_pdf(odata) al.debug("scaling %s (%d of %d): old size %d, new size %d" % (new_name, i, len(mp), len(odata), len(data)), "check_and_scale_pdfs", dbo) # Update the media entry with the new name db.execute(dbo, "UPDATE media SET MediaName = '%s' WHERE ID = %d" % ( new_name, m["ID"])) # Update the dbfs entry from old name to new name (will be overwritten in a minute but safer than delete) dbfs.rename_file(dbo, filepath, original_name, new_name) # Store the PDF file data with the new name - if there was a need to change it if len(data) < len(odata): dbfs.put_string(dbo, new_name, filepath, data) al.debug("found and scaled %d pdfs" % len(mp), "media.check_and_scale_pdfs", dbo)
def scale_animal_images(dbo): """ Goes through all animal images in the database and scales them to the current incoming media scaling factor. """ mp = db.query(dbo, "SELECT MediaName FROM media WHERE LOWER(MediaName) LIKE '%.jpg' AND LinkTypeID = 0") for i, m in enumerate(mp): filepath = db.query_string(dbo, "SELECT Path FROM dbfs WHERE Name='%s'" % m["MEDIANAME"]) name = str(m["MEDIANAME"]) inputfile = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) outputfile = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) odata = dbfs.get_string(dbo, name) inputfile.write(odata) inputfile.flush() inputfile.close() outputfile.close() al.debug("scaling %s (%d of %d)" % (name, i, len(mp)), "media.scale_animal_images", dbo) scale_image_file(inputfile.name, outputfile.name, configuration.incoming_media_scaling(dbo)) f = open(outputfile.name, "r") data = f.read() f.close() os.unlink(inputfile.name) os.unlink(outputfile.name) # Update the image file data dbfs.put_string(dbo, name, filepath, data) al.debug("scaled %d images" % len(mp), "media.scale_animal_images", dbo)
def scale_all_odt(dbo): """ Goes through all odt files attached to records in the database and scales them down (throws away images and objects so only the text remains to save space) """ mo = dbo.query( "SELECT ID, MediaName FROM media WHERE MediaMimeType = 'application/vnd.oasis.opendocument.text'" ) total = 0 for i, m in enumerate(mo): name = str(m.MEDIANAME) al.debug("scaling %s (%d of %d)" % (name, i, len(mo)), "media.scale_all_odt", dbo) odata = dbfs.get_string(dbo, name) if odata == "": al.error("file %s does not exist" % name, "media.scale_all_odt", dbo) continue path = dbo.query_string("SELECT Path FROM dbfs WHERE Name = ?", [name]) ndata = scale_odt(odata) if len(ndata) < 512: al.error( "scaled odt %s came back at %d bytes, abandoning" % (name, len(ndata)), "scale_all_odt", dbo) else: dbfs.put_string(dbo, name, path, ndata) dbo.update("media", m.ID, {"MediaSize": len(ndata)}) total += 1 al.debug("scaled %d of %d odts" % (total, len(mo)), "media.scale_all_odt", dbo)
def check_and_scale_pdfs(dbo): """ Goes through all PDFs in the database to see if they have been scaled (have a suffix of _scaled.pdf) and scales down any unscaled ones. """ if not SCALE_PDF_DURING_BATCH: al.warn("SCALE_PDF_DURING_BATCH is disabled, not scaling pdfs", "media.check_and_scale_pdfs", dbo) if not configuration.scale_pdfs(dbo): al.warn( "ScalePDFs config option disabled in this database, not scaling pdfs", "media.check_and_scale_pdfs", dbo) mp = db.query(dbo, \ "SELECT MediaName FROM media WHERE LOWER(MediaName) LIKE '%.pdf' AND " \ "LOWER(MediaName) NOT LIKE '%_scaled.pdf'") for m in mp: filepath = db.query_string( dbo, "SELECT Path FROM dbfs WHERE Name='%s'" % m["MEDIANAME"]) original_name = str(m["MEDIANAME"]) new_name = str(m["MEDIANAME"]) new_name = new_name[0:len(new_name) - 4] + "_scaled.pdf" odata = dbfs.get_string(dbo, original_name) data = scale_pdf(odata) # Update the media entry with the new name db.execute(dbo, "UPDATE media SET MediaName = '%s' WHERE MediaName = '%s'" % \ ( new_name, original_name)) # Update the dbfs entry with the new name dbfs.rename_file(dbo, filepath, original_name, new_name) # Update the PDF file data dbfs.put_string(dbo, new_name, filepath, data) al.debug("found and scaled %d pdfs" % len(mp), "media.check_and_scale_pdfs", dbo)
def scale_all_animal_images(dbo): """ Goes through all animal images in the database and scales them to the current incoming media scaling factor. """ mp = dbo.query("SELECT ID, MediaName FROM media WHERE MediaMimeType = 'image/jpeg' AND LinkTypeID = 0") for i, m in enumerate(mp): filepath = dbo.query_string("SELECT Path FROM dbfs WHERE Name = ?", [m.MEDIANAME]) name = str(m.MEDIANAME) inputfile = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) outputfile = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) odata = dbfs.get_string(dbo, name) inputfile.write(odata) inputfile.flush() inputfile.close() outputfile.close() al.debug("scaling %s (%d of %d)" % (name, i, len(mp)), "media.scale_all_animal_images", dbo) try: scale_image_file(inputfile.name, outputfile.name, configuration.incoming_media_scaling(dbo)) except Exception as err: al.error("failed scaling image, doing nothing: %s" % err, "media.scale_all_animal_images", dbo) continue data = utils.read_binary_file(outputfile.name) os.unlink(inputfile.name) os.unlink(outputfile.name) # Update the image file data dbfs.put_string(dbo, name, filepath, data) dbo.update("media", m.ID, { "MediaSize": len(data) }) al.debug("scaled %d images" % len(mp), "media.scale_all_animal_images", dbo)
def create_document_media(dbo, username, linktype, linkid, template, content): """ Creates a new media record for a document for the link given. linktype: ANIMAL, PERSON, etc linkid: ID for the link template: The name of the template used to create the document content: The document contents """ mediaid = dbo.get_id("media") path = get_dbfs_path(linkid, linktype) name = str(mediaid) + ".html" dbfsid = dbfs.put_string(dbo, name, path, content) dbo.insert("media", { "ID": mediaid, "DBFSID": dbfsid, "MediaSize": len(content), "MediaName": "%d.html" % mediaid, "MediaMimeType": "text/html", "MediaType": 0, "MediaNotes": template, "WebsitePhoto": 0, "WebsiteVideo": 0, "DocPhoto": 0, "ExcludeFromPublish": 0, # ASM2_COMPATIBILITY "NewSinceLastPublish": 1, "UpdatedSinceLastPublish": 0, # ASM2_COMPATIBILITY "LinkID": linkid, "LinkTypeID": linktype, "Date": dbo.now(), "RetainUntil": None }, username, setCreated=False, generateID=False) return mediaid
def create_blank_document_media(dbo, username, linktype, linkid): """ Creates a new media record for a blank document for the link given. linktype: ANIMAL, PERSON, etc linkid: ID for the link returns the new media id """ mediaid = db.get_id(dbo, "media") sql = db.make_insert_sql( "media", ( ("ID", db.di(mediaid)), ("MediaName", db.ds("%d.html" % mediaid)), ("MediaType", db.di(0)), ("MediaNotes", db.ds("New document")), ("WebsitePhoto", db.di(0)), ("WebsiteVideo", db.di(0)), ("DocPhoto", db.di(0)), ("ExcludeFromPublish", db.di(0)), # ASM2_COMPATIBILITY ("NewSinceLastPublish", db.di(1)), ("UpdatedSinceLastPublish", db.di(0)), # ASM2_COMPATIBILITY ("LinkID", db.di(linkid)), ("LinkTypeID", db.di(linktype)), ("Date", db.nowsql()))) db.execute(dbo, sql) path = "" if linktype == ANIMAL: path = "/animal" elif linktype == PERSON: path = "/owner" elif linktype == LOSTANIMAL: path = "/lostanimal" elif linktype == FOUNDANIMAL: path = "/foundanimal" path += "/" + str(linkid) name = str(mediaid) + ".html" dbfs.put_string(dbo, name, path, "") audit.create(dbo, username, "media", str(mediaid) + ": for " + str(linkid) + "/" + str(linktype)) return mediaid
def rotate_media(dbo, username, mid, clockwise = True): """ Rotates an image media record 90 degrees if clockwise is true, or 270 degrees if false """ mr = dbo.first_row(dbo.query("SELECT * FROM media WHERE ID=?", [mid])) if not mr: raise utils.ASMError("Record does not exist") # If it's not a jpg image, we can stop right now mn = mr.MEDIANAME ext = mn[mn.rfind("."):].lower() if ext != ".jpg" and ext != ".jpeg": raise utils.ASMError("Image is not a JPEG file, cannot rotate") # Load the image data path = get_dbfs_path(mr.LINKID, mr.LINKTYPEID) imagedata = dbfs.get_string(dbo, mn, path) imagedata = rotate_image(imagedata, clockwise) # Store it back in the dbfs and add an entry to the audit trail dbfs.put_string(dbo, mn, path, imagedata) # Update the date stamp on the media record dbo.update("media", mid, { "Date": dbo.now(), "MediaSize": len(imagedata) }) audit.edit(dbo, username, "media", mid, "", "media id %d rotated, clockwise=%s" % (mid, str(clockwise)))
def check_and_scale_pdfs(dbo): """ Goes through all PDFs in the database to see if they have been scaled (have a suffix of _scaled.pdf) and scales down any unscaled ones. """ if not gs_installed(): al.warn("ghostscript is not installed, can't scale pdfs", "media.check_and_scale_pdfs", dbo) return mp = db.query(dbo, \ "SELECT MediaName FROM media WHERE LOWER(MediaName) LIKE '%.pdf' AND " \ "LOWER(MediaName) NOT LIKE '%_scaled.pdf'") for m in mp: filepath = db.query_string( dbo, "SELECT Path FROM dbfs WHERE Name='%s'" % m["MEDIANAME"]) original_name = str(m["MEDIANAME"]) new_name = str(m["MEDIANAME"]) new_name = new_name[0:len(new_name) - 4] + "_scaled.pdf" inputfile = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) outputfile = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) odata = dbfs.get_string(dbo, original_name) inputfile.write(odata) inputfile.flush() inputfile.close() outputfile.close() scale_pdf_file(inputfile.name, outputfile.name) f = open(outputfile.name, "r") data = f.read() f.close() os.unlink(inputfile.name) os.unlink(outputfile.name) # Update the media entry with the new name db.execute(dbo, "UPDATE media SET MediaName = '%s' WHERE MediaName = '%s'" % \ ( new_name, original_name)) # Update the dbfs entry with the new name dbfs.rename_file(dbo, filepath, original_name, new_name) # Update the PDF file data dbfs.put_string(dbo, new_name, filepath, data) al.debug("found and scaled %d pdfs" % len(mp), "media.check_and_scale_pdfs", dbo)
def rotate_media(dbo, username, mid, clockwise = True): """ Rotates an image media record 90 degrees if clockwise is true, or 270 degrees if false """ mr = db.query(dbo, "SELECT * FROM media WHERE ID=%d" % int(mid)) if len(mr) == 0: raise utils.ASMError("Record does not exist") mr = mr[0] mn = mr["MEDIANAME"] # If it's not a jpg image, we can stop right now ext = mn[mn.rfind("."):].lower() if ext != ".jpg" and ext != ".jpeg": raise utils.ASMError("Image is not a JPEG file, cannot rotate") # Load the image data path = get_dbfs_path(mr["LINKID"], mr["LINKTYPEID"]) imagedata = dbfs.get_string(dbo, mn, path) imagedata = rotate_image(imagedata, clockwise) # Store it back in the dbfs and add an entry to the audit trail dbfs.put_string(dbo, mn, path, imagedata) # Update the date stamp on the media record db.execute(dbo, "UPDATE media SET Date = %s WHERE ID = %d" % (db.nowsql(), mid)) audit.edit(dbo, username, "media", "media id %d rotated, clockwise=%s" % (mid, str(clockwise)))
def create_document_media(dbo, username, linktype, linkid, template, content): """ Creates a new media record for a document for the link given. linktype: ANIMAL, PERSON, etc linkid: ID for the link template: The name of the template used to create the document content: The document contents """ mediaid = db.get_id(dbo, "media") sql = db.make_insert_sql("media", ( ( "ID", db.di(mediaid) ), ( "MediaName", db.ds("%d.html" % mediaid) ), ( "MediaType", db.di(0)), ( "MediaNotes", db.ds(template) ), ( "WebsitePhoto", db.di(0) ), ( "WebsiteVideo", db.di(0) ), ( "DocPhoto", db.di(0) ), ( "ExcludeFromPublish", db.di(0) ), # ASM2_COMPATIBILITY ( "NewSinceLastPublish", db.di(1) ), ( "UpdatedSinceLastPublish", db.di(0) ), # ASM2_COMPATIBILITY ( "LinkID", db.di(linkid) ), ( "LinkTypeID", db.di(linktype) ), ( "Date", db.nowsql() ) )) db.execute(dbo, sql) path = "" if linktype == ANIMAL: path = "/animal" elif linktype == PERSON: path = "/owner" elif linktype == LOSTANIMAL: path = "/lostanimal" elif linktype == FOUNDANIMAL: path = "/foundanimal" path += "/" + str(linkid) name = str(mediaid) + ".html" dbfs.put_string(dbo, name, path, content) audit.create(dbo, username, "media", mediaid, str(mediaid) + ": for " + str(linkid) + "/" + str(linktype))
def check_and_scale_pdfs(dbo): """ Goes through all PDFs in the database to see if they have been scaled (have a suffix of _scaled.pdf) and scales down any unscaled ones. """ if not gs_installed(): al.warn("ghostscript is not installed, can't scale pdfs", "media.check_and_scale_pdfs", dbo) return mp = db.query(dbo, \ "SELECT MediaName FROM media WHERE LOWER(MediaName) LIKE '%.pdf' AND " \ "LOWER(MediaName) NOT LIKE '%_scaled.pdf'") for m in mp: filepath = db.query_string(dbo, "SELECT Path FROM dbfs WHERE Name='%s'" % m["MEDIANAME"]) original_name = str(m["MEDIANAME"]) new_name = str(m["MEDIANAME"]) new_name = new_name[0:len(new_name)-4] + "_scaled.pdf" inputfile = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) outputfile = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) odata = dbfs.get_string(dbo, original_name) inputfile.write(odata) inputfile.flush() inputfile.close() outputfile.close() scale_pdf_file(inputfile.name, outputfile.name) f = open(outputfile.name, "r") data = f.read() f.close() os.unlink(inputfile.name) os.unlink(outputfile.name) # Update the media entry with the new name db.execute(dbo, "UPDATE media SET MediaName = '%s' WHERE MediaName = '%s'" % \ ( new_name, original_name)) # Update the dbfs entry with the new name dbfs.rename_file(dbo, filepath, original_name, new_name) # Update the PDF file data dbfs.put_string(dbo, new_name, filepath, data) al.debug("found and scaled %d pdfs" % len(mp), "media.check_and_scale_pdfs", dbo)
def create_blank_document_media(dbo, username, linktype, linkid): """ Creates a new media record for a blank document for the link given. linktype: ANIMAL, PERSON, etc linkid: ID for the link returns the new media id """ mediaid = db.get_id(dbo, "media") sql = db.make_insert_sql("media", ( ( "ID", db.di(mediaid) ), ( "MediaName", db.ds("%d.html" % mediaid) ), ( "MediaType", db.di(0)), ( "MediaNotes", db.ds("New document") ), ( "WebsitePhoto", db.di(0) ), ( "WebsiteVideo", db.di(0) ), ( "DocPhoto", db.di(0) ), ( "ExcludeFromPublish", db.di(0) ), ( "NewSinceLastPublish", db.di(1) ), ( "UpdatedSinceLastPublish", db.di(0) ), ( "LinkID", db.di(linkid) ), ( "LinkTypeID", db.di(linktype) ), ( "Date", db.nowsql() ) )) db.execute(dbo, sql) path = "" if linktype == ANIMAL: path = "/animal" elif linktype == PERSON: path = "/owner" elif linktype == LOSTANIMAL: path = "/lostanimal" elif linktype == FOUNDANIMAL: path = "/foundanimal" path += "/" + str(linkid) name = str(mediaid) + ".html" dbfs.put_string(dbo, name, path, "") audit.create(dbo, username, "media", str(mediaid) + ": for " + str(linkid) + "/" + str(linktype)) return mediaid
def rotate_media(dbo, username, mid, clockwise=True): """ Rotates an image media record 90 degrees if clockwise is true, or 270 degrees if false """ mr = db.query(dbo, "SELECT * FROM media WHERE ID=%d" % int(mid)) if len(mr) == 0: raise utils.ASMError("Record does not exist") mr = mr[0] mn = mr["MEDIANAME"] # If it's not a jpg image, we can stop right now ext = mn[mn.rfind("."):].lower() if ext != ".jpg" and ext != ".jpeg": raise utils.ASMError("Image is not a JPEG file, cannot rotate") # Load the image data path = get_dbfs_path(mr["LINKID"], mr["LINKTYPEID"]) imagedata = dbfs.get_string(dbo, mn, path) imagedata = rotate_image(imagedata, clockwise) # Store it back in the dbfs and add an entry to the audit trail dbfs.put_string(dbo, mn, path, imagedata) # Update the date stamp on the media record db.execute(dbo, "UPDATE media SET Date = %s WHERE ID = %d" % (db.nowsql(), mid)) audit.edit(dbo, username, "media", "media id %d rotated, clockwise=%s" % (mid, str(clockwise)))
def scale_all_odt(dbo): """ Goes through all odt files attached to records in the database and scales them down (throws away images and objects so only the text remains to save space) """ mo = db.query(dbo, "SELECT MediaName FROM media WHERE LOWER(MediaName) LIKE '%.odt'") for i, m in enumerate(mo): name = str(m["MEDIANAME"]) al.debug("scaling %s (%d of %d)" % (name, i, len(mo)), "media.scale_all_odt", dbo) print "%s (%d of %d)" % (name, i, len(mo)) odata = dbfs.get_string(dbo, name) if odata == "": al.error("file %s does not exist" % name, "media.scale_all_odt", dbo) print "file %s does not exist" % name continue path = db.query_string(dbo, "SELECT Path FROM dbfs WHERE Name='%s'" % name) ndata = scale_odt(odata) if len(ndata) < 512: al.error("scaled odt %s came back at %d bytes, abandoning" % (name, len(ndata)), "scale_all_odt", dbo) print "file too small < 512, doing nothing" else: print "old size: %d, new size: %d" % (len(odata), len(ndata)) dbfs.put_string(dbo, name, path, ndata)
def attach_file_from_form(dbo, username, linktype, linkid, data): """ Attaches a media file from the posted form data is the web.py data object and should contain comments, the filechooser object, with filename and value props - OR a parameter called base64image containing a base64 encoded jpg image. """ ext = "" base64data = "" base64image = utils.df_ks(data, "base64image") if base64image != "": ext = ".jpg" # If an HTML5 data url was used, strip the prefix so we just have base64 data if base64image.find("data:") != -1: base64data = base64image[base64image.find(",")+1:] # Browser escaping turns base64 pluses back into spaces, so switch back base64data = base64data.replace(" ", "+") else: base64data = base64image else: ext = data.filechooser.filename ext = ext[ext.rfind("."):].lower() mediaid = db.get_id(dbo, "media") medianame = "%d%s" % ( mediaid, ext ) ispicture = ext == ".jpg" or ext == ".jpeg" ispdf = ext == ".pdf" # Does this link have anything with web/doc set? If not, set the # web/doc flags web = 0 doc = 0 existing_web = db.query_int(dbo, "SELECT COUNT(*) FROM media WHERE WebsitePhoto = 1 " \ "AND LinkID = %d AND LinkTypeID = %d" % ( int(linkid), int(linktype) )) existing_doc = db.query_int(dbo, "SELECT COUNT(*) FROM media WHERE DocPhoto = 1 " \ "AND LinkID = %d AND LinkTypeID = %d" % ( int(linkid), int(linktype) )) if existing_web == 0 and ispicture: web = 1 if existing_doc == 0 and ispicture: doc = 1 if base64image != "": filedata = base64.b64decode(base64data) else: filedata = data.filechooser.value # Is it a picture? if ispicture: # Autorotate it to match the EXIF orientation filedata = auto_rotate_image(filedata) # Scale it down to the system set size scalespec = configuration.incoming_media_scaling(dbo) if scalespec != "None": filedata = scale_image(filedata, scalespec) # Is it a PDF? If so, compress it if we can and the option is on if ispdf and gs_installed() and SCALE_PDF: filedata = scale_pdf(filedata) medianame = "%d_scaled.pdf" % mediaid # Attach the file in the dbfs path = get_dbfs_path(linkid, linktype) dbfs.put_string(dbo, medianame, path, filedata) # Are the notes for an image blank and we're defaulting them from animal comments? comments = utils.df_ks(data, "comments") if comments == "" and ispicture and linktype == ANIMAL and configuration.auto_media_notes(dbo): comments = animal.get_comments(dbo, int(linkid)) # Are the notes blank and we're defaulting them from the filename? elif comments == "" and configuration.default_media_notes_from_file(dbo) and base64image == "": comments = utils.filename_only(data.filechooser.filename) # Create the media record sql = db.make_insert_sql("media", ( ( "ID", db.di(mediaid) ), ( "MediaName", db.ds(medianame) ), ( "MediaType", db.di(0) ), ( "MediaNotes", db.ds(comments) ), ( "WebsitePhoto", db.di(web) ), ( "WebsiteVideo", db.di(0) ), ( "DocPhoto", db.di(doc) ), ( "ExcludeFromPublish", db.di(0) ), ( "NewSinceLastPublish", db.di(1) ), ( "UpdatedSinceLastPublish", db.di(0) ), ( "LinkID", db.di(linkid) ), ( "LinkTypeID", db.di(linktype) ), ( "Date", db.nowsql() ) )) db.execute(dbo, sql) audit.create(dbo, username, "media", str(mediaid) + ": for " + str(linkid) + "/" + str(linktype))
def attach_file_from_form(dbo, username, linktype, linkid, post): """ Attaches a media file from the posted form data is the web.py data object and should contain comments and either the filechooser object, with filename and value OR filedata, filetype and filename parameters (filetype is the MIME type, filedata is base64 encoded contents) """ ext = "" filedata = post["filedata"] filename = post["filename"] comments = post["comments"] if filedata != "": filetype = post["filetype"] if filetype.startswith("image") or filename.lower().endswith(".jpg"): ext = ".jpg" elif filetype.startswith("image") or filename.lower().endswith(".png"): ext = ".png" elif filetype.find("pdf") != -1 or filename.lower().endswith(".pdf"): ext = ".pdf" elif filetype.find("html") != -1 or filename.lower().endswith(".html"): ext = ".html" # Strip the data:mime prefix so we just have base64 data if filedata.startswith("data:"): filedata = filedata[filedata.find(",") + 1:] # Browser escaping turns base64 pluses back into spaces, so switch back filedata = filedata.replace(" ", "+") filedata = base64.b64decode(filedata) al.debug( "received data URI '%s' (%d bytes)" % (filename, len(filedata)), "media.attach_file_from_form", dbo) if ext == "": msg = "could not determine extension from file.type '%s', abandoning" % filetype al.error(msg, "media.attach_file_from_form", dbo) raise utils.ASMValidationError(msg) else: # It's a traditional form post with a filechooser, we should make # it the default web/doc picture after posting if none is available. ext = post.filename() ext = ext[ext.rfind("."):].lower() filedata = post.filedata() filename = post.filename() al.debug( "received POST file data '%s' (%d bytes)" % (filename, len(filedata)), "media.attach_file_from_form", dbo) # If we receive some images in formats other than JPG, we'll # pretend they're jpg as that's what they'll get transformed into # by scale_image if ext == ".png": ext = ".jpg" mediaid = dbo.get_id("media") medianame = "%d%s" % (mediaid, ext) ispicture = ext == ".jpg" or ext == ".jpeg" ispdf = ext == ".pdf" excludefrompublish = 0 if configuration.auto_new_images_not_for_publish(dbo) and ispicture: excludefrompublish = 1 # Are we allowed to upload this type of media? if ispicture and not configuration.media_allow_jpg(dbo): msg = "upload of media type jpg is disabled" al.error(msg, "media.attach_file_from_form", dbo) raise utils.ASMValidationError(msg) if ispdf and not configuration.media_allow_pdf(dbo): msg = "upload of media type pdf is disabled" al.error(msg, "media.attach_file_from_form", dbo) raise utils.ASMValidationError(msg) # Is it a picture? if ispicture: # Autorotate it to match the EXIF orientation filedata = auto_rotate_image(dbo, filedata) # Scale it down to the system set size scalespec = configuration.incoming_media_scaling(dbo) if scalespec != "None": filedata = scale_image(filedata, scalespec) al.debug( "scaled image to %s (%d bytes)" % (scalespec, len(filedata)), "media.attach_file_from_form", dbo) # Is it a PDF? If so, compress it if we can and the option is on if ispdf and SCALE_PDF_DURING_ATTACH and configuration.scale_pdfs(dbo): orig_len = len(filedata) filedata = scale_pdf(filedata) if len(filedata) < orig_len: al.debug("compressed PDF (%d bytes)" % (len(filedata)), "media.attach_file_from_form", dbo) # Attach the file in the dbfs path = get_dbfs_path(linkid, linktype) dbfsid = dbfs.put_string(dbo, medianame, path, filedata) # Are the notes for an image blank and we're defaulting them from animal comments? if comments == "" and ispicture and linktype == ANIMAL and configuration.auto_media_notes( dbo): comments = animal.get_comments(dbo, int(linkid)) # Are the notes blank and we're defaulting them from the filename? elif comments == "" and configuration.default_media_notes_from_file(dbo): comments = utils.filename_only(filename) # Create the media record dbo.insert( "media", { "ID": mediaid, "DBFSID": dbfsid, "MediaSize": len(filedata), "MediaName": medianame, "MediaMimeType": mime_type(medianame), "MediaType": 0, "MediaNotes": comments, "WebsitePhoto": 0, "WebsiteVideo": 0, "DocPhoto": 0, "ExcludeFromPublish": excludefrompublish, # ASM2_COMPATIBILITY "NewSinceLastPublish": 1, "UpdatedSinceLastPublish": 0, # ASM2_COMPATIBILITY "LinkID": linkid, "LinkTypeID": linktype, "Date": dbo.now(), "RetainUntil": None }, username, setCreated=False, generateID=False) # Verify this record has a web/doc default if we aren't excluding it from publishing if ispicture and excludefrompublish == 0: check_default_web_doc_pic(dbo, mediaid, linkid, linktype) return mediaid
def attach_file_from_form(dbo, username, linktype, linkid, post): """ Attaches a media file from the posted form data is the web.py data object and should contain comments and either the filechooser object, with filename and value OR filedata, filetype and filename parameters (filetype is the MIME type, filedata is base64 encoded contents) """ ext = "" filedata = post["filedata"] filename = post["filename"] comments = post["comments"] checkpref = True if filedata != "": checkpref = False filetype = post["filetype"] if filetype.startswith("image"): ext = ".jpg" elif filetype.find("pdf") != -1: ext = ".pdf" elif filetype.find("html") != -1: ext = ".html" # Strip the data:mime prefix so we just have base64 data if filedata.startswith("data:"): filedata = filedata[filedata.find(",")+1:] # Browser escaping turns base64 pluses back into spaces, so switch back filedata = filedata.replace(" ", "+") filedata = base64.b64decode(filedata) al.debug("received HTML5 file data '%s' (%d bytes)" % (filename, len(filedata)), "media.attach_file_from_form", dbo) else: # It's a traditional form post with a filechooser, we should make # it the default web/doc picture after posting if none is available. checkpref = True ext = post.filename() ext = ext[ext.rfind("."):].lower() filedata = post.filedata() filename = post.filename() al.debug("received POST file data '%s' (%d bytes)" % (filename, len(filedata)), "media.attach_file_from_form", dbo) mediaid = db.get_id(dbo, "media") medianame = "%d%s" % ( mediaid, ext ) ispicture = ext == ".jpg" or ext == ".jpeg" ispdf = ext == ".pdf" # Is it a picture? if ispicture: # Autorotate it to match the EXIF orientation filedata = auto_rotate_image(dbo, filedata) # Scale it down to the system set size scalespec = configuration.incoming_media_scaling(dbo) if scalespec != "None": filedata = scale_image(filedata, scalespec) al.debug("scaled image to %s (%d bytes)" % (scalespec, len(filedata)), "media.attach_file_from_form", dbo) # Is it a PDF? If so, compress it if we can and the option is on if ispdf and SCALE_PDF_DURING_ATTACH and configuration.scale_pdfs(dbo): filedata = scale_pdf(filedata) medianame = "%d_scaled.pdf" % mediaid al.debug("compressed PDF (%d bytes)" % (len(filedata)), "media.attach_file_from_form", dbo) # Attach the file in the dbfs path = get_dbfs_path(linkid, linktype) dbfs.put_string(dbo, medianame, path, filedata) # Are the notes for an image blank and we're defaulting them from animal comments? if comments == "" and ispicture and linktype == ANIMAL and configuration.auto_media_notes(dbo): comments = animal.get_comments(dbo, int(linkid)) # Are the notes blank and we're defaulting them from the filename? elif comments == "" and configuration.default_media_notes_from_file(dbo): comments = utils.filename_only(filename) # Create the media record sql = db.make_insert_sql("media", ( ( "ID", db.di(mediaid) ), ( "MediaName", db.ds(medianame) ), ( "MediaType", db.di(0) ), ( "MediaNotes", db.ds(comments) ), ( "WebsitePhoto", db.di(0) ), ( "WebsiteVideo", db.di(0) ), ( "DocPhoto", db.di(0) ), ( "ExcludeFromPublish", db.di(0) ), # ASM2_COMPATIBILITY ( "NewSinceLastPublish", db.di(1) ), ( "UpdatedSinceLastPublish", db.di(0) ), # ASM2_COMPATIBILITY ( "LinkID", db.di(linkid) ), ( "LinkTypeID", db.di(linktype) ), ( "Date", db.dd(i18n.now(dbo.timezone))) )) db.execute(dbo, sql) audit.create(dbo, username, "media", mediaid, str(mediaid) + ": for " + str(linkid) + "/" + str(linktype)) if ispicture and checkpref: check_default_web_doc_pic(dbo, mediaid, linkid, linktype)
def attach_file_from_form(dbo, username, linktype, linkid, post): """ Attaches a media file from the posted form data is the web.py data object and should contain comments, the filechooser object, with filename and value props - OR a parameter called base64image containing a base64 encoded jpg image. """ ext = "" base64data = "" base64image = post["base64image"] if base64image != "": ext = ".jpg" # If an HTML5 data url was used, strip the prefix so we just have base64 data if base64image.find("data:") != -1: base64data = base64image[base64image.find(",") + 1:] # Browser escaping turns base64 pluses back into spaces, so switch back base64data = base64data.replace(" ", "+") else: base64data = base64image al.debug( "received HTML5 base64 image data (%d bytes)" % (len(base64image)), "media.attach_file_from_form", dbo) else: ext = post.filename() ext = ext[ext.rfind("."):].lower() mediaid = db.get_id(dbo, "media") medianame = "%d%s" % (mediaid, ext) ispicture = ext == ".jpg" or ext == ".jpeg" ispdf = ext == ".pdf" # Does this link have anything with web/doc set? If not, set the # web/doc flags web = 0 doc = 0 existing_web = db.query_int(dbo, "SELECT COUNT(*) FROM media WHERE WebsitePhoto = 1 " \ "AND LinkID = %d AND LinkTypeID = %d" % ( int(linkid), int(linktype) )) existing_doc = db.query_int(dbo, "SELECT COUNT(*) FROM media WHERE DocPhoto = 1 " \ "AND LinkID = %d AND LinkTypeID = %d" % ( int(linkid), int(linktype) )) if existing_web == 0 and ispicture: web = 1 if existing_doc == 0 and ispicture: doc = 1 if base64image != "": filedata = base64.b64decode(base64data) else: filedata = post.filedata() al.debug( "received POST file data '%s' (%d bytes)" % (post.filename(), len(filedata)), "media.attach_file_from_form", dbo) # Is it a picture? if ispicture: # Autorotate it to match the EXIF orientation filedata = auto_rotate_image(dbo, filedata) # Scale it down to the system set size scalespec = configuration.incoming_media_scaling(dbo) if scalespec != "None": filedata = scale_image(filedata, scalespec) al.debug( "scaled image to %s (%d bytes)" % (scalespec, len(filedata)), "media.attach_file_from_form", dbo) # Is it a PDF? If so, compress it if we can and the option is on if ispdf and SCALE_PDF_DURING_ATTACH and configuration.scale_pdfs(dbo): filedata = scale_pdf(filedata) medianame = "%d_scaled.pdf" % mediaid al.debug("compressed PDF (%d bytes)" % (len(filedata)), "media.attach_file_from_form", dbo) # Attach the file in the dbfs path = get_dbfs_path(linkid, linktype) dbfs.put_string(dbo, medianame, path, filedata) # Are the notes for an image blank and we're defaulting them from animal comments? comments = post["comments"] if comments == "" and ispicture and linktype == ANIMAL and configuration.auto_media_notes( dbo): comments = animal.get_comments(dbo, int(linkid)) # Are the notes blank and we're defaulting them from the filename? elif comments == "" and configuration.default_media_notes_from_file( dbo) and base64image == "": comments = utils.filename_only(post.filename()) # Create the media record sql = db.make_insert_sql( "media", ( ("ID", db.di(mediaid)), ("MediaName", db.ds(medianame)), ("MediaType", db.di(0)), ("MediaNotes", db.ds(comments)), ("WebsitePhoto", db.di(web)), ("WebsiteVideo", db.di(0)), ("DocPhoto", db.di(doc)), ("ExcludeFromPublish", db.di(0)), # ASM2_COMPATIBILITY ("NewSinceLastPublish", db.di(1)), ("UpdatedSinceLastPublish", db.di(0)), # ASM2_COMPATIBILITY ("LinkID", db.di(linkid)), ("LinkTypeID", db.di(linktype)), ("Date", db.dd(i18n.now(dbo.timezone))))) db.execute(dbo, sql) audit.create(dbo, username, "media", str(mediaid) + ": for " + str(linkid) + "/" + str(linktype))
def create_animal(dbo, username, wlid): """ Creates an animal record from a waiting list entry with the id given """ l = dbo.locale a = dbo.first_row( dbo.query("SELECT * FROM animalwaitinglist WHERE ID = ?", [wlid])) data = { "animalname": _("Waiting List {0}", l).format(wlid), "markings": str(a["ANIMALDESCRIPTION"]), "reasonforentry": str(a["REASONFORWANTINGTOPART"]), "species": str(a["SPECIESID"]), "hiddenanimaldetails": str(a["COMMENTS"]), "broughtinby": str(a["OWNERID"]), "originalowner": str(a["OWNERID"]), "animaltype": configuration.default_type(dbo), "breed1": configuration.default_breed(dbo), "breed2": configuration.default_breed(dbo), "basecolour": configuration.default_colour(dbo), "size": configuration.default_size(dbo), "internallocation": configuration.default_location(dbo), "dateofbirth": python2display(l, subtract_years(now(dbo.timezone))), "estimateddob": "1" } # If we aren't showing the time brought in, set it to midnight if not configuration.add_animals_show_time_brought_in(dbo): data["timebroughtin"] = "00:00:00" # If we're creating shelter codes manually, we need to put something unique # in there for now. Use the id if configuration.manual_codes(dbo): data["sheltercode"] = "WL" + str(wlid) data["shortcode"] = "WL" + str(wlid) nextid, code = animal.insert_animal_from_form(dbo, utils.PostedData(data, l), username) # Now that we've created our animal, we should remove this entry from the waiting list dbo.update( "animalwaitinglist", wlid, { "DateRemovedFromList": dbo.today(), "ReasonForRemoval": _("Moved to animal record {0}", l).format(code) }, username) # If there were any logs and media entries on the waiting list, create them on the animal # Media for me in dbo.query( "SELECT * FROM media WHERE LinkTypeID = ? AND LinkID = ?", (media.WAITINGLIST, wlid)): ext = me.medianame ext = ext[ext.rfind("."):].lower() mediaid = dbo.get_id("media") medianame = "%d%s" % (mediaid, ext) dbo.insert( "media", { "ID": mediaid, "DBFSID": 0, "MediaSize": 0, "MediaName": medianame, "MediaMimeType": media.mime_type(medianame), "MediaType": me.mediatype, "MediaNotes": me.medianotes, "WebsitePhoto": me.websitephoto, "WebsiteVideo": me.websitevideo, "DocPhoto": me.docphoto, "ExcludeFromPublish": me.excludefrompublish, # ASM2_COMPATIBILITY "NewSinceLastPublish": 1, "UpdatedSinceLastPublish": 0, # ASM2_COMPATIBILITY "LinkID": nextid, "LinkTypeID": media.ANIMAL, "Date": me.date, "RetainUntil": me.retainuntil }, generateID=False) # Now clone the dbfs item pointed to by this media item if it's a file if me.mediatype == media.MEDIATYPE_FILE: filedata = dbfs.get_string(dbo, me.medianame) dbfsid = dbfs.put_string(dbo, medianame, "/animal/%d" % nextid, filedata) dbo.execute( "UPDATE media SET DBFSID = ?, MediaSize = ? WHERE ID = ?", (dbfsid, len(filedata), mediaid)) # Logs for lo in dbo.query("SELECT * FROM log WHERE LinkType = ? AND LinkID = ?", (log.WAITINGLIST, wlid)): dbo.insert( "log", { "LinkID": nextid, "LinkType": log.ANIMAL, "LogTypeID": lo.LOGTYPEID, "Date": lo.DATE, "Comments": lo.COMMENTS }, username) return nextid