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_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_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 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 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"] 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 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