Example #1
0
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)
Example #2
0
def do_copy(target_node, root, from_path, to_path):
    _validate_paths(root, from_path, to_path, target_node)

    # We start by retrieving the original object to copy. If it exists but is
    # deleted, we raise a 404.
    obj = _get_source_object(root, from_path, target_node)

    # Do the copy and return both the original and the copy.
    _, new_node = database.copy_hierarchy(root, to_path, target_node)
    return obj, copy_object(obj, new_node, tools.last_element(to_path))
Example #3
0
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)