def _bibdoc_modify_files(recid, form): from invenio.legacy.bibdocfile.api import BibRecDocs try: recdocs = BibRecDocs(recid) except: current_app.logger.error("REST API: Error while building BibRecDocs for record %d" % (recid,)) return [] actions = {} for (k,v) in form.items(): if k.startswith('__file__name__'): docid = int(k[len('__file__name__'):]) docname = recdocs.get_docname(docid) if docname != v: actions[docid] = ('rename', docname, v) if k.startswith('__file__delete__') and v == 'Delete': docid = int(k[len('__file__delete__'):]) docname = recdocs.get_docname(docid) actions[docid] = ('delete', docname, None) # overwrite rename for (_,(act, docname, newname)) in actions.items(): if act == 'delete': current_app.logger.info("deleting bibdoc/file: {}/'{}'".format(recid, docname)) recdocs.delete_bibdoc(docname) elif act == 'rename': current_app.logger.info("renaming bibdoc/file: {}/'{}' -> '{}'".format(recid, docname, newname)) recdocs.change_name(newname=newname, oldname=docname)
def tearDown(self): from invenio.legacy.bibdocfile.api import BibRecDocs my_bibrecdoc = BibRecDocs(2) #delete my_bibrecdoc.delete_bibdoc('img_test') my_bibrecdoc.delete_bibdoc('file') my_bibrecdoc.delete_bibdoc('test')
def Move_Photos_to_Storage(parameters, curdir, form, user_info=None): """ The function moves files received from the submission's form through the PHOTO_MANAGER element and its asynchronous uploads at CFG_SITE_URL/submit/uploadfile. Parameters: @iconsize - Seperate multiple sizes with commas. The ImageMagick geometry inputs are supported. Use type 'geometry' as defined in ImageMagick. (eg. 320 or 320x240 or 100> or 5%) Example: "180>,700>" will create two icons, one with maximum dimension 180px, one 700px @iconformat - Allowed extensions (as defined in websubmit_icon_creator.py) are: "pdf", "gif", "jpg", "jpeg", "ps", "png", "bmp" "eps", "epsi", "epsf" The PHOTO_MANAGER elements builds the following file organization in the directory curdir:: curdir/ | ______________________________________________________________________ | | | files/ PHOTO_MANAGER_ICONS icons/ | PHOTO_MANAGER_ORDER | (user id)/ PHOTO_MANAGER_DELETE (user id)/ | PHOTO_MANAGER_NEW | NewFile/ PHOTO_MANAGER_DESCRIPTION_X NewFile/ | | _______________________ _____________________ | | | | | | photo1.jpg myPhoto.gif ... photo1.jpg myPhoto.gif ... where the files are: - PHOTO_MANAGER_ORDER: ordered list of file IDs. One per line. - PHOTO_MANAGER_ICONS: mappings from file IDs to URL of the icons. One per line. Separator: / - PHOTO_MANAGER_NEW: mapping from file ID to filename on disk. Only applicable to files that have just been uploaded (i.e. not bibdocfiles). One per line. Separator: / - PHOTO_MANAGER_DELETE: list of files IDs that must be deleted. One per line - PHOTO_MANAGER_DESCRIPTION_X, where X is file ID: contains photos descriptions (one per file) """ global sysno icon_sizes = parameters.get('iconsize').split(',') icon_format = parameters.get('iconformat') if not icon_format: icon_format = 'gif' PHOTO_MANAGER_ICONS = read_param_file(curdir, 'PHOTO_MANAGER_ICONS', split_lines=True) photo_manager_icons_dict = dict([value.split('/', 1) \ for value in PHOTO_MANAGER_ICONS \ if '/' in value]) PHOTO_MANAGER_ORDER = read_param_file(curdir, 'PHOTO_MANAGER_ORDER', split_lines=True) photo_manager_order_list = [value for value in PHOTO_MANAGER_ORDER if value.strip()] PHOTO_MANAGER_DELETE = read_param_file(curdir, 'PHOTO_MANAGER_DELETE', split_lines=True) photo_manager_delete_list = [value for value in PHOTO_MANAGER_DELETE if value.strip()] PHOTO_MANAGER_NEW = read_param_file(curdir, 'PHOTO_MANAGER_NEW', split_lines=True) photo_manager_new_dict = dict([value.split('/', 1) \ for value in PHOTO_MANAGER_NEW \ if '/' in value]) ## Create an instance of BibRecDocs for the current recid(sysno) bibrecdocs = BibRecDocs(sysno) for photo_id in photo_manager_order_list: photo_description = read_param_file(curdir, 'PHOTO_MANAGER_DESCRIPTION_' + photo_id) # We must take different actions depending if we deal with a # file that already exists, or if it is a new file if photo_id in photo_manager_new_dict.keys(): # New file if photo_id not in photo_manager_delete_list: filename = photo_manager_new_dict[photo_id] filepath = os.path.join(curdir, 'files', str(user_info['uid']), 'NewFile', filename) icon_filename = os.path.splitext(filename)[0] + ".gif" fileiconpath = os.path.join(curdir, 'icons', str(user_info['uid']), 'NewFile', icon_filename) # Add the file if os.path.exists(filepath): _do_log(curdir, "Adding file %s" % filepath) bibdoc = bibrecdocs.add_new_file(filepath, doctype="picture", never_fail=True) has_added_default_icon_subformat_p = False for icon_size in icon_sizes: # Create icon if needed try: (icon_path, icon_name) = create_icon( { 'input-file' : filepath, 'icon-name' : icon_filename, 'icon-file-format' : icon_format, 'multipage-icon' : False, 'multipage-icon-delay' : 100, 'icon-scale' : icon_size, # Resize only if width > 300 'verbosity' : 0, }) fileiconpath = os.path.join(icon_path, icon_name) except InvenioWebSubmitIconCreatorError as e: _do_log(curdir, "Icon could not be created to %s: %s" % (filepath, e)) pass if os.path.exists(fileiconpath): try: if not has_added_default_icon_subformat_p: bibdoc.add_icon(fileiconpath) has_added_default_icon_subformat_p = True _do_log(curdir, "Added icon %s" % fileiconpath) else: icon_suffix = icon_size.replace('>', '').replace('<', '').replace('^', '').replace('!', '') bibdoc.add_icon(fileiconpath, subformat=CFG_BIBDOCFILE_DEFAULT_ICON_SUBFORMAT + "-" + icon_suffix) _do_log(curdir, "Added icon %s" % fileiconpath) except InvenioBibDocFileError as e: # Most probably icon already existed. pass if photo_description and bibdoc: for file_format in [bibdocfile.get_format() \ for bibdocfile in bibdoc.list_latest_files()]: bibdoc.set_comment(photo_description, file_format) _do_log(curdir, "Added comment %s" % photo_description) else: # Existing file bibdocname = bibrecdocs.get_docname(int(photo_id)) if photo_id in photo_manager_delete_list: # In principle we should not get here. but just in case... bibrecdocs.delete_bibdoc(bibdocname) _do_log(curdir, "Deleted %s" % bibdocname) else: bibdoc = bibrecdocs.get_bibdoc(bibdocname) for file_format in [bibdocfile.get_format() \ for bibdocfile in bibdoc.list_latest_files()]: bibdoc.set_comment(photo_description, file_format) _do_log(curdir, "Added comment %s" % photo_description) # Now delete requeted files for photo_id in photo_manager_delete_list: try: bibdocname = bibrecdocs.get_docname(int(photo_id)) bibrecdocs.delete_bibdoc(bibdocname) _do_log(curdir, "Deleted %s" % bibdocname) except: # we tried to delete a photo that does not exist (maybe already deleted) pass # Update the MARC _do_log(curdir, "Asking bibdocfile to fix marc") bibdocfile_bin = os.path.join(CFG_BINDIR, 'bibdocfile --yes-i-know') os.system(bibdocfile_bin + " --fix-marc --recid=" + str(sysno)) # Delete the HB BibFormat cache in the DB, so that the fulltext # links do not point to possible dead files run_sql("DELETE LOW_PRIORITY from bibfmt WHERE format='HB' AND id_bibrec=%s", (sysno,)) return ""
def Move_Photos_to_Storage(parameters, curdir, form, user_info=None): """ The function moves files received from the submission's form through the PHOTO_MANAGER element and its asynchronous uploads at CFG_SITE_URL/submit/uploadfile. Parameters: @iconsize - Seperate multiple sizes with commas. The ImageMagick geometry inputs are supported. Use type 'geometry' as defined in ImageMagick. (eg. 320 or 320x240 or 100> or 5%) Example: "180>,700>" will create two icons, one with maximum dimension 180px, one 700px @iconformat - Allowed extensions (as defined in websubmit_icon_creator.py) are: "pdf", "gif", "jpg", "jpeg", "ps", "png", "bmp" "eps", "epsi", "epsf" The PHOTO_MANAGER elements builds the following file organization in the directory curdir:: curdir/ | ______________________________________________________________________ | | | files/ PHOTO_MANAGER_ICONS icons/ | PHOTO_MANAGER_ORDER | (user id)/ PHOTO_MANAGER_DELETE (user id)/ | PHOTO_MANAGER_NEW | NewFile/ PHOTO_MANAGER_DESCRIPTION_X NewFile/ | | _______________________ _____________________ | | | | | | photo1.jpg myPhoto.gif ... photo1.jpg myPhoto.gif ... where the files are: - PHOTO_MANAGER_ORDER: ordered list of file IDs. One per line. - PHOTO_MANAGER_ICONS: mappings from file IDs to URL of the icons. One per line. Separator: / - PHOTO_MANAGER_NEW: mapping from file ID to filename on disk. Only applicable to files that have just been uploaded (i.e. not bibdocfiles). One per line. Separator: / - PHOTO_MANAGER_DELETE: list of files IDs that must be deleted. One per line - PHOTO_MANAGER_DESCRIPTION_X, where X is file ID: contains photos descriptions (one per file) """ global sysno icon_sizes = parameters.get('iconsize').split(',') icon_format = parameters.get('iconformat') if not icon_format: icon_format = 'gif' PHOTO_MANAGER_ICONS = read_param_file(curdir, 'PHOTO_MANAGER_ICONS', split_lines=True) photo_manager_icons_dict = dict([value.split('/', 1) \ for value in PHOTO_MANAGER_ICONS \ if '/' in value]) PHOTO_MANAGER_ORDER = read_param_file(curdir, 'PHOTO_MANAGER_ORDER', split_lines=True) photo_manager_order_list = [ value for value in PHOTO_MANAGER_ORDER if value.strip() ] PHOTO_MANAGER_DELETE = read_param_file(curdir, 'PHOTO_MANAGER_DELETE', split_lines=True) photo_manager_delete_list = [ value for value in PHOTO_MANAGER_DELETE if value.strip() ] PHOTO_MANAGER_NEW = read_param_file(curdir, 'PHOTO_MANAGER_NEW', split_lines=True) photo_manager_new_dict = dict([value.split('/', 1) \ for value in PHOTO_MANAGER_NEW \ if '/' in value]) ## Create an instance of BibRecDocs for the current recid(sysno) bibrecdocs = BibRecDocs(sysno) for photo_id in photo_manager_order_list: photo_description = read_param_file( curdir, 'PHOTO_MANAGER_DESCRIPTION_' + photo_id) # We must take different actions depending if we deal with a # file that already exists, or if it is a new file if photo_id in photo_manager_new_dict.keys(): # New file if photo_id not in photo_manager_delete_list: filename = photo_manager_new_dict[photo_id] filepath = os.path.join(curdir, 'files', str(user_info['uid']), 'NewFile', filename) icon_filename = os.path.splitext(filename)[0] + ".gif" fileiconpath = os.path.join(curdir, 'icons', str(user_info['uid']), 'NewFile', icon_filename) # Add the file if os.path.exists(filepath): _do_log(curdir, "Adding file %s" % filepath) bibdoc = bibrecdocs.add_new_file(filepath, doctype="picture", never_fail=True) has_added_default_icon_subformat_p = False for icon_size in icon_sizes: # Create icon if needed try: (icon_path, icon_name) = create_icon({ 'input-file': filepath, 'icon-name': icon_filename, 'icon-file-format': icon_format, 'multipage-icon': False, 'multipage-icon-delay': 100, 'icon-scale': icon_size, # Resize only if width > 300 'verbosity': 0, }) fileiconpath = os.path.join(icon_path, icon_name) except InvenioWebSubmitIconCreatorError as e: _do_log( curdir, "Icon could not be created to %s: %s" % (filepath, e)) pass if os.path.exists(fileiconpath): try: if not has_added_default_icon_subformat_p: bibdoc.add_icon(fileiconpath) has_added_default_icon_subformat_p = True _do_log(curdir, "Added icon %s" % fileiconpath) else: icon_suffix = icon_size.replace( '>', '').replace('<', '').replace( '^', '').replace('!', '') bibdoc.add_icon( fileiconpath, subformat= CFG_BIBDOCFILE_DEFAULT_ICON_SUBFORMAT + "-" + icon_suffix) _do_log(curdir, "Added icon %s" % fileiconpath) except InvenioBibDocFileError as e: # Most probably icon already existed. pass if photo_description and bibdoc: for file_format in [bibdocfile.get_format() \ for bibdocfile in bibdoc.list_latest_files()]: bibdoc.set_comment(photo_description, file_format) _do_log(curdir, "Added comment %s" % photo_description) else: # Existing file bibdocname = bibrecdocs.get_docname(int(photo_id)) if photo_id in photo_manager_delete_list: # In principle we should not get here. but just in case... bibrecdocs.delete_bibdoc(bibdocname) _do_log(curdir, "Deleted %s" % bibdocname) else: bibdoc = bibrecdocs.get_bibdoc(bibdocname) for file_format in [bibdocfile.get_format() \ for bibdocfile in bibdoc.list_latest_files()]: bibdoc.set_comment(photo_description, file_format) _do_log(curdir, "Added comment %s" % photo_description) # Now delete requeted files for photo_id in photo_manager_delete_list: try: bibdocname = bibrecdocs.get_docname(int(photo_id)) bibrecdocs.delete_bibdoc(bibdocname) _do_log(curdir, "Deleted %s" % bibdocname) except: # we tried to delete a photo that does not exist (maybe already deleted) pass # Update the MARC _do_log(curdir, "Asking bibdocfile to fix marc") bibdocfile_bin = os.path.join(CFG_BINDIR, 'bibdocfile --yes-i-know') os.system(bibdocfile_bin + " --fix-marc --recid=" + str(sysno)) # Delete the HB BibFormat cache in the DB, so that the fulltext # links do not point to possible dead files run_sql( "DELETE LOW_PRIORITY from bibfmt WHERE format='HB' AND id_bibrec=%s", (sysno, )) return ""