View Source IVCU.File (IVCU v0.2.0)

Internal file representation used everywhere else in the library.

Link to this section Summary

Types

t()

File representation used in the library.

Functions

Create new File struct from file content.

Create new File struct with only name present.

Create new File struct from path.

Return random filename.

Replaces :filename with random filename with no extension.

Link to this section Types

Specs

t() :: %IVCU.File{
  content: binary() | nil,
  filename: Path.t(),
  path: Path.t() | nil
}

File representation used in the library.

File must always have a :name, even though it may not have :path or :content depending on the way it was produced.

Link to this section Functions

Specs

from_content(binary()) :: t()

Create new File struct from file content.

This function is supposed to be used when you need to save some binary payload as a file.

example

Example

iex(1)> %IVCU.File{path: nil, content: <<255, 255>>} =
...(1)>   IVCU.File.from_content(<<255, 255>>)

Note

This function puts random :filename with no extension into the file.

Create new File struct with only name present.

This function is supposed to be used for deletion of an already existing file or for getting an URL for the file.

example

Example

iex(1)> IVCU.File.from_name("file.txt")
%IVCU.File{filename: "file.txt", path: nil, content: nil}

Warning

It cannot be used to save a new file, because we need some source from which we copy the file.

Specs

from_path(Path.t()) :: t()

Create new File struct from path.

This function is supposed to be used when you need to save a file already stored on the filesystem.

example

Example

iex(1)> IVCU.File.from_path("./uploads/file.txt")
%IVCU.File{
  filename: "file.txt",
  path: "./uploads/file.txt",
  content: nil
}

Return random filename.

Link to this function

with_random_filename(file)

View Source

Replaces :filename with random filename with no extension.

It's usefull when you need to override original filename to keep filenames unique.

example

Example

iex(1)> file = IVCU.File.from_path("./uploads/file.txt")
iex(2)> %IVCU.File{
...(2)>   content: nil,
...(2)>   path: "./uploads/file.txt",
...(2)>   filename: filename
...(2)> } = IVCU.File.with_random_filename(file)
iex(3)> filename != file.filename
true