Magpie.Files (Magpie v0.1.0)

Copy Markdown View Source

This module contains endpoints and data types for basic file operations.

Summary

Functions

Copy a file or folder to a different location in the user's Dropbox. If the source path is a folder all its contents will be copied.

Create folder returns map

Same as create_folder/2 but returns {:ok, %Magpie.Folder{}}.

Delete the file or folder at a given path. If the path is a folder, all its contents will be deleted too. A successful response indicates that the file or folder was deleted. The returned metadata will be the corresponding FileMetadata or FolderMetadata for the item at time of deletion, and not a DeletedMetadata object.

Same as delete_folder/2 but returns {:ok, %Magpie.Folder{}}.

Download a file from a user's Dropbox.

Download a folder from the user's Dropbox as a zip file.

Export a file from the user's Dropbox to a portable format (for files that cannot be downloaded directly, e.g. Paper docs). opts accepts "export_format".

Get a preview for a file.

Get a temporary link to stream content of a file. This link will expire in four hours and afterwards you will get 410 Gone. Content-Type of the link is determined automatically by the file's mime type.

Get a one-time-use temporary upload link for a direct binary upload. commit_info takes the /files/upload argument fields, e.g. %{"path" => "/a.txt", "mode" => "add"}; duration is in seconds.

Get thumbnails for a list of images. We allow up to 25 thumbnails in a single batch.

Get a thumbnail for an image or document, addressed by path or shared link. resource is %{".tag" => "path", "path" => ...} or %{".tag" => "link", "url" => ...}; opts accepts "format", "size", "mode" and "quality".

Move a file or folder to a different location in the user's Dropbox. If the source path is a folder all its contents will be moved.

Permanently delete the file or folder at a given path. Requires a Dropbox Business account with Advanced or Enterprise plan. opts accepts "parent_rev".

Restore a file to a specific revision.

Searches for files and folders.

Fetches the next page of search results returned from search/3.

Returns a lazy Stream over all search matches, fetching pages through search/3 + search_continue/2 on demand. Raises Magpie.Error if a page request fails.

Create a new file with the contents provided in the request.

Uploads the local file at local_path to path in the user's Dropbox, picking the right strategy automatically

Functions

copy(client, from_path, to_path)

Copy a file or folder to a different location in the user's Dropbox. If the source path is a folder all its contents will be copied.

Example

Magpie.Files.copy(client, "/Temp/first", "/Tmp/second")

More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-copy_v2

create_folder(client, path)

@spec create_folder(Magpie.Client.t(), binary()) :: any()

Create folder returns map

Example

Magpie.Files.create_folder client, "/Path"

More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-create_folder

create_folder_to_struct(client, path)

@spec create_folder_to_struct(Magpie.Client.t(), binary()) ::
  {:ok, Magpie.Folder.t()} | {:error, Magpie.Error.t()}

Same as create_folder/2 but returns {:ok, %Magpie.Folder{}}.

Example

Magpie.Files.create_folder_to_struct client, "/Path"

More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-create_folder

delete_folder(client, path)

Delete the file or folder at a given path. If the path is a folder, all its contents will be deleted too. A successful response indicates that the file or folder was deleted. The returned metadata will be the corresponding FileMetadata or FolderMetadata for the item at time of deletion, and not a DeletedMetadata object.

Example

Magpie.Files.delete_folder client, "/Homework/math/Prime_Numbers.txt"

More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-delete_v2

delete_folder_to_struct(client, path)

@spec delete_folder_to_struct(Magpie.Client.t(), binary()) ::
  {:ok, Magpie.Folder.t()} | {:error, Magpie.Error.t()}

Same as delete_folder/2 but returns {:ok, %Magpie.Folder{}}.

Example

Magpie.Files.delete_folder_to_struct client, "/Path"

More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-delete_v2

download(client, path)

Download a file from a user's Dropbox.

Example

Magpie.Files.download client, "/mypdf.pdf"

More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-download

download_zip(client, path)

Download a folder from the user's Dropbox as a zip file.

More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-download_zip

export(client, path, opts \\ %{})

Export a file from the user's Dropbox to a portable format (for files that cannot be downloaded directly, e.g. Paper docs). opts accepts "export_format".

More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-export

get_metadata(client, path, include_media_info \\ false, include_deleted \\ false, include_has_explicit_shared_members \\ false)

Returns the metadata for a file or folder.

Example

Magpie.Files.get_metadata client, "/mypdf.pdf"

More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-get_metadata

get_preview(client, path)

Get a preview for a file.

Example

Magpie.Files.get_preview client, "/mypdf.pdf"

More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-get_preview

get_temporary_link(client, path)

Get a temporary link to stream content of a file. This link will expire in four hours and afterwards you will get 410 Gone. Content-Type of the link is determined automatically by the file's mime type.

Example

Magpie.Files.get_temporary_link client, "/video.mp4"

More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-get_preview

get_temporary_upload_link(client, commit_info, duration \\ 14400)

Get a one-time-use temporary upload link for a direct binary upload. commit_info takes the /files/upload argument fields, e.g. %{"path" => "/a.txt", "mode" => "add"}; duration is in seconds.

More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-get_temporary_upload_link

get_thumbnail(client, path, format \\ "jpeg", size \\ "w64h64")

Get a thumbnail for an image.

Example

Magpie.Files.get_thumbnail client, "/image.jpg"

More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-get_thumbnail

get_thumbnail_batch(client, entries)

Get thumbnails for a list of images. We allow up to 25 thumbnails in a single batch.

Example

batch = %{ "path" => "/image.jpg", "format" => "jpeg", "size" => "w64h64"} entries = [batch] Magpie.Files.get_thumbnail_batch client, entries

More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-get_thumbnail_batch

get_thumbnail_v2(client, resource, opts \\ %{})

Get a thumbnail for an image or document, addressed by path or shared link. resource is %{".tag" => "path", "path" => ...} or %{".tag" => "link", "url" => ...}; opts accepts "format", "size", "mode" and "quality".

More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-get_thumbnail_v2

move(client, from_path, to_path)

Move a file or folder to a different location in the user's Dropbox. If the source path is a folder all its contents will be moved.

Example

Magpie.Files.move(client, "/Homework/math", "/Homework/algebra")

More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-move_v2

permanently_delete(client, path, opts \\ %{})

Permanently delete the file or folder at a given path. Requires a Dropbox Business account with Advanced or Enterprise plan. opts accepts "parent_rev".

More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-permanently_delete

restore(client, path, rev)

Restore a file to a specific revision.

Example

Magpie.Files.restore(client, "/root/word.docx", "a1c10ce0dd78")

More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-restore

search(client, query, options \\ %{})

Searches for files and folders.

options accepts the SearchOptions fields, e.g. %{"path" => "/Photos", "max_results" => 100, "filename_only" => true}.

Example

Magpie.Files.search(client, "word.docx", %{"path" => "/root"})

More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-search_v2

search_continue(client, cursor)

Fetches the next page of search results returned from search/3.

Example

Magpie.Files.search_continue(client, cursor)

More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-search-continue_v2

search_stream(client, query, options \\ %{})

Returns a lazy Stream over all search matches, fetching pages through search/3 + search_continue/2 on demand. Raises Magpie.Error if a page request fails.

Example

client
|> Magpie.Files.search_stream("report", %{"path" => "/Work"})
|> Enum.take(50)

upload(client, path, file, mode \\ "add", autorename \\ true, mute \\ false)

Create a new file with the contents provided in the request.

Example

Magpie.Files.upload client, "/mypdf.pdf", "/mypdf.pdf"

More info at: https://www.dropbox.com/developers/documentation/http/documentation#files-upload

upload_file(client, path, local_path, opts \\ [])

Uploads the local file at local_path to path in the user's Dropbox, picking the right strategy automatically:

  • files up to :session_threshold bytes go through a single /files/upload call;
  • larger files are streamed through an upload session (startappend_v2 × N → finish) in chunks of :chunk_size bytes, without ever loading the whole file into memory.

Returns {:ok, file_metadata} on success, {:error, %Magpie.Error{}} on Dropbox errors, or {:error, posix} when the local file cannot be read.

Options

  • :chunk_size — upload session chunk size in bytes (default 8 MiB)
  • :session_threshold — size above which an upload session is used (default 150 MiB, the Dropbox limit for single-request uploads)
  • :mode"add" (default) or "overwrite"
  • :autorename — default true
  • :mute — default false

Example

{:ok, metadata} = Magpie.Files.upload_file(client, "/Backup/db.dump", "priv/db.dump")