def delete(request): filepath = request.POST.get('filepath', '') filepath = os.path.join('/', filepath) error = filesystem_ajax_helpers.check_filepath_exists(filepath) if error == None: if os.path.isdir(filepath): try: shutil.rmtree(filepath) except Exception: logging.exception('Error deleting directory {}'.format(filepath)) error = 'Error attempting to delete directory.' else: os.remove(filepath) # if deleting from originals, delete ES data as well if ORIGINAL_DIR in filepath and filepath.index(ORIGINAL_DIR) == 0: transfer_uuid = _find_uuid_of_transfer_in_originals_directory_using_path(filepath) if transfer_uuid != None: elasticSearchFunctions.connect_and_remove_backlog_transfer_files(transfer_uuid) if error is not None: response = { 'message': error, 'error': True, } else: response = {'message': 'Delete successful.'} return helpers.json_response(response)
def _copy_to_start_transfer(filepath="", type="", accession="", access_id="", transfer_metadata_set_row_uuid=""): error = filesystem_ajax_helpers.check_filepath_exists(filepath) if error is None: temp_uuid = str(uuid.uuid4()) # confine destination to subdir of originals basename = os.path.basename(filepath) # default to standard transfer type_subdir = TRANSFER_TYPE_DIRECTORIES.get(type, "standardTransfer") destination = os.path.join(ACTIVE_TRANSFER_DIR, type_subdir, "{}-{}".format(basename, temp_uuid)) destination = helpers.pad_destination_filepath_if_it_already_exists( destination) # Ensure directories end with a trailing / if os.path.isdir(filepath): destination = os.path.join(destination, "") mcp_destination = destination.replace( os.path.join(SHARED_DIRECTORY_ROOT, ""), "%sharedPath%") kwargs = { "uuid": temp_uuid, "accessionid": accession, "access_system_id": access_id, "currentlocation": mcp_destination, } # Even if a UUID is passed, there might not be a row with # that UUID yet - for instance, if the user opened an edit # form but did not save any metadata for that row. if transfer_metadata_set_row_uuid: try: row = models.TransferMetadataSet.objects.get( id=transfer_metadata_set_row_uuid) kwargs["transfermetadatasetrow"] = row except models.TransferMetadataSet.DoesNotExist: pass # Create the Transfer here instead of letting MCPClient create it # Used to pass additional information to the Transfer models.Transfer.objects.create(**kwargs) try: shutil.move(filepath, destination) except (OSError, shutil.Error) as e: error = ("Error copying from " + filepath + " to " + destination + ". (" + str(e) + ")") if error: raise Exception(error) return destination
def copy_to_start_transfer(filepath='', type='', accession='', transfer_metadata_set_row_uuid=''): error = filesystem_ajax_helpers.check_filepath_exists(filepath) if error is None: # confine destination to subdir of originals basename = os.path.basename(filepath) # default to standard transfer type_subdir = TRANSFER_TYPE_DIRECTORIES.get(type, 'standardTransfer') destination = os.path.join(ACTIVE_TRANSFER_DIR, type_subdir, basename) destination = helpers.pad_destination_filepath_if_it_already_exists( destination) # Ensure directories end with a trailing / if os.path.isdir(filepath): destination = os.path.join(destination, '') # If we need to pass additional data to the Transfer, create the object here instead of letting MCPClient create it if accession != '' or transfer_metadata_set_row_uuid != '': temp_uuid = str(uuid.uuid4()) mcp_destination = destination.replace( os.path.join(SHARED_DIRECTORY_ROOT, ''), '%sharedPath%') kwargs = { "uuid": temp_uuid, "accessionid": accession, "currentlocation": mcp_destination } # Even if a UUID is passed, there might not be a row with # that UUID yet - for instance, if the user opened an edit # form but did not save any metadata for that row. if transfer_metadata_set_row_uuid: try: row = models.TransferMetadataSet.objects.get( id=transfer_metadata_set_row_uuid) kwargs["transfermetadatasetrow"] = row except models.TransferMetadataSet.DoesNotExist: pass transfer = models.Transfer.objects.create(**kwargs) transfer.save() try: shutil.move(filepath, destination) except (OSError, shutil.Error) as e: error = 'Error copying from ' + filepath + ' to ' + destination + '. (' + str( e) + ')' if error: raise Exception(error) return destination