def fileops_createfolder(): # Root and path data are passed as POST data rather than URL args. root = request.form['root'] path_ = request.form['path'] tools.validate_root_or_abort(root) tools.validate_path_or_abort(path_) # A file operation is always traduced by a new Commit object in order to # track the changes. try: commit = database.create_commit(root) ref_node, new_node = database.copy_hierarchy(root, path_, commit.root) except database.MissingNodeException: raise BasicError(404, E_NON_EXISTING_DESTINATION_PATH) # To stick with Dropbox behaviour, we raise a 403 if the directory already # exists. treename = tools.split_path(path_)[-1] existing = ref_node and ref_node.sub_trees.get(treename) if existing and not existing.is_deleted: raise BasicError(403, E_DIR_ALREADY_EXISTS) # Create the new directory and commit the change. output = models.TreeLink(tree=models.Tree(), path=treename) new_node.sub_trees[treename] = output database.store_commit(root, commit) return metadata.make_metadata(root, path_, output)
def fileops_copy(): root, from_path, to_path = _get_params() tools.validate_root_or_abort(root) # A copy operation is always traduced by a new Commit object in order to # track the changes. commit = database.create_commit(root) obj, obj_copy = do_copy(commit.root, root, from_path, to_path) # Store the commit, and return the metadata for the new object. database.store_commit(root, commit) return metadata.make_metadata(root, to_path, obj_copy)
def fileops_move(): root, from_path, to_path = _get_params() tools.validate_root_or_abort(root) # Move is implemented in terms of copy. commit = database.create_commit(root) obj, obj_copy = copy.do_copy(commit.root, root, from_path, to_path) # Delete the source object. source = database.get_stored_object(root, from_path, commit.root) delete.recursive_delete(source) # Store the commit, and return the metadata for the new object. database.store_commit(root, commit) return metadata.make_metadata(root, to_path, obj_copy)
def do_put(root, path_, stream, hasher, encryption_iv): # A file operation is always traduced by a new Commit object in order to # track the changes. If copying fails because of an incomplete source # hierarchy we abort with a 404. try: commit = database.create_commit(root) ref_node, new_node = database.copy_hierarchy(root, path_, commit.root) except database.MissingNodeException: raise BasicError(404, E_NON_EXISTING_DESTINATION_PATH) # Handle the case where the file already exists. In the general case, if # the filename already exists and that 'overwrite' is set to False, we put # to a new filename such that 'test.txt' becomes 'test (1).txt'. Also, when # a content is put to an older revision (identified by 'parent_rev'), then # the filename 'test.txt' becomes 'test (conflicted copy).txt'. split_pt = tools.split_path(path_) filename = split_pt[-1] # It is an error to post a file named like an (non deleted) directory. existing_dir = ref_node.sub_trees.get(filename) if existing_dir and not existing_dir.is_deleted: raise BasicError(403, E_DIR_ALREADY_EXISTS) if filename in ref_node.sub_files: filename = _handle_conflict(ref_node, filename, **_get_url_params()) path_ = '/'.join(['/'.join(split_pt[:-1]), filename]) # We start by storing the provided content, and then we try and make the # database structure reflect the requested change. filehash = file_store.register_blob(root, path_, stream, hasher) fileblob = _find_or_create_blob(filehash, encryption_iv) # Update the blob entry if it's actually different from the previous one, # and commit to the database. Considering that the on disk blobs are # encrypted with a randomly generated IV, this is more than unlikely. old_blob = ref_node and ref_node.sub_files.get(filename) if old_blob and (old_blob.hash == fileblob.hash): # pragma: no cover output = old_blob old_blob.is_deleted = False # Restore the file if it was deleted else: output = models.BlobLink(blob=fileblob, path=filename, parent=old_blob) new_node.sub_files[filename] = output database.store_commit(root, commit) return metadata.make_metadata(root, path_, output)
def fileops_delete(): root, path_ = _get_params() tools.validate_root_or_abort(root) commit = database.create_commit(root) # Retrieve the stored object (could be a blob or tree link) or abort with # a 404 if we fail. try: stored_object = database.get_stored_object(root, path_, commit.root) except database.MissingNodeException: raise BasicError(404, E_FILE_NOT_FOUND) # Mark the database object as deleted if it's not already. if stored_object.is_deleted: raise BasicError(404, E_ALREADY_DELETED) # Recursively delete the object (if necessary), and commit the transaction. recursive_delete(stored_object) database.store_commit(root, commit) return metadata.make_metadata(root, path_, stored_object)