# Class File

File Descriptor class

Architecture tips:

1. Physically Saltcorn stores files on local filesystem of the server, where Saltcorn runs.
2. The path to file store is defined in db.connectObj.file_store.
3. List of files stored in _sc_files table in Saltcorn database.
4. Each tenant has own file list and file storage.
5. This class provides file descriptor and basic functions to manipulate with files.

- Defined in packages/saltcorn-data/models/file.ts:76
- Defined in packages/saltcorn-data/models/file.ts:1145

##### Index

### Constructors

constructor

### Properties

filename
id?
isDirectory
location
mime_sub
mime_super
min_role_read
s3_store
size_kb
uploaded_at
user_id?

### Accessors

absolutePath
current_folder
field_value
mimetype
path_to_serve

### Methods

delete
get_contents
is_symlink
move_to_dir
overwrite_contents
rename
set_role
set_user
absPathToServePath
allDirectories
buildDirCache
create
destroyDirCache
ensure_file_store
fieldValueFromRelative
find
findOne
from_contents
from_file_on_disk
from_req_files
get_new_path
getDirCache
isAbsoluteURL
nameToMimeType
new_folder
normalise
normalise_in_base
normalizeFieldValueInput
pathToServeUrl
reallyBuildDirCache
relativePathFromFieldValue
rootFolder
set_xattr_of_existing_file
update
update_table_references
upload

## Constructors

### constructor

- new File(o: FileCfg): File

  Constructor

  #### Parameters

  - o: FileCfg

    {object}

  #### Returns File

  - Defined in packages/saltcorn-data/models/file.ts:93

## Properties

### filename

filename: string

- Defined in packages/saltcorn-data/models/file.ts:77

### `Optional` id

id?: number

- Defined in packages/saltcorn-data/models/file.ts:83

### isDirectory

isDirectory: boolean

- Defined in packages/saltcorn-data/models/file.ts:87

### location

location: string

- Defined in packages/saltcorn-data/models/file.ts:78

### mime_sub

mime_sub: string

- Defined in packages/saltcorn-data/models/file.ts:80

### mime_super

mime_super: string

- Defined in packages/saltcorn-data/models/file.ts:79

### min_role_read

min_role_read: number

- Defined in packages/saltcorn-data/models/file.ts:85

### s3_store

s3_store: boolean

- Defined in packages/saltcorn-data/models/file.ts:86

### size_kb

size_kb: number

- Defined in packages/saltcorn-data/models/file.ts:82

### uploaded_at

uploaded_at: Date

- Defined in packages/saltcorn-data/models/file.ts:81

### `Optional` user_id

user_id?: number

- Defined in packages/saltcorn-data/models/file.ts:84

## Accessors

### absolutePath

- get absolutePath(): string

  #### Returns string

  - Defined in packages/saltcorn-data/models/file.ts:283

### current_folder

- get current_folder(): string

  Get current folder

  #### Returns string

  - Defined in packages/saltcorn-data/models/file.ts:655

### field_value

- get field_value(): string

  #### Returns string

  - Defined in packages/saltcorn-data/models/file.ts:647

### mimetype

- get mimetype(): string

  MIME type of the file

  #### Returns string

  - Defined in packages/saltcorn-data/models/file.ts:1085

### path_to_serve

- get path_to_serve(): string

  Get path to serve

  #### Returns string

  - Defined in packages/saltcorn-data/models/file.ts:634

## Methods

### delete

- delete(
  unlinker?: (arg0: File) => Promise<void>,
  ): Promise<void | { error: string }>

  Delete file

  #### Parameters

  - `Optional` unlinker: (arg0: File) => Promise<void>

  #### Returns Promise<void | { error: string }>

  - Defined in packages/saltcorn-data/models/file.ts:1040

### get_contents

- get_contents(
  encoding?: "ascii" | "utf8" | "base64" | "base64url" | "hex",
  ): Promise<Buffer<ArrayBufferLike>>

  #### Parameters

  - `Optional` encoding: "ascii" | "utf8" | "base64" | "base64url" | "hex"

  #### Returns Promise<Buffer<ArrayBufferLike>>

  - Defined in packages/saltcorn-data/models/file.ts:944

### is_symlink

- is_symlink(): Promise<boolean>

  #### Returns Promise<boolean>

  - Defined in packages/saltcorn-data/models/file.ts:475

### move_to_dir

- move_to_dir(newFolder: string): Promise<void>

  Move file to other folder

  #### Parameters

  - newFolder: string

    target folder for file

  #### Returns Promise<void>

  - Defined in packages/saltcorn-data/models/file.ts:794

### overwrite_contents

- overwrite_contents(
  contents: string | ArrayBuffer | Buffer<ArrayBufferLike>,
  ): Promise<void>

  #### Parameters

  - contents: string | ArrayBuffer | Buffer<ArrayBufferLike>

  #### Returns Promise<void>

  - Defined in packages/saltcorn-data/models/file.ts:1017

### rename

- rename(filenameIn: string): Promise<void>

  Rename file

  #### Parameters

  - filenameIn: string

    target file name

  #### Returns Promise<void>

  - Defined in packages/saltcorn-data/models/file.ts:722

### set_role

- set_role(min_role_read: number): Promise<void>

  Set Min Role to read for file

  #### Parameters

  - min_role_read: number

    target Role for file to read

  #### Returns Promise<void>

  - Defined in packages/saltcorn-data/models/file.ts:673

### set_user

- set_user(user_id: number): Promise<void>

  Set user for file

  #### Parameters

  - user_id: number

    target user_id

  #### Returns Promise<void>

  - Defined in packages/saltcorn-data/models/file.ts:694

### `Static` absPathToServePath

- absPathToServePath(absPath: string | number): string

  #### Parameters

  - absPath: string | number

  #### Returns string

  - Defined in packages/saltcorn-data/models/file.ts:272

### `Static` allDirectories

- allDirectories(ignoreCache?: boolean): Promise<File[]>

  get all directories in the root folder (tenant root dir for multi-tenant)

  #### Parameters

  - `Optional` ignoreCache: boolean

    if a cache exists, ignore it

  #### Returns Promise<File[]>

  - Defined in packages/saltcorn-data/models/file.ts:294

### `Static` buildDirCache

- buildDirCache(): Promise<void>

  #### Returns Promise<void>

  - Defined in packages/saltcorn-data/models/file.ts:360

### `Static` create

- create(f: FileCfg): Promise<File>

  Create file

  #### Parameters

  - f: FileCfg

  #### Returns Promise<File>

  - Defined in packages/saltcorn-data/models/file.ts:1096

### `Static` destroyDirCache

- destroyDirCache(): void

  #### Returns void

  - Defined in packages/saltcorn-data/models/file.ts:372

### `Static` ensure_file_store

- ensure_file_store(tenant_name?: string): Promise<void>

  Ensure that file_store path is physically exists in file system. In reality just recursively creates full absolute path to db.connectObj.file_store.

  #### Parameters

  - `Optional` tenant_name: string

  #### Returns Promise<void>

  - Defined in packages/saltcorn-data/models/file.ts:882

### `Static` fieldValueFromRelative

- fieldValueFromRelative(relPath?: string): string

  #### Parameters

  - `Optional` relPath: string

  #### Returns string

  - Defined in packages/saltcorn-data/models/file.ts:201

### `Static` find

- find(where?: Where, selectopts?: SelectOptions): Promise<File[]>

  Select list of file descriptors

  #### Parameters

  - `Optional` where: Where
  - selectopts: SelectOptions = {}

  #### Returns Promise<File[]>

  - Defined in packages/saltcorn-data/models/file.ts:117

### `Static` findOne

- findOne(where: string | Where): Promise<File | null>

  Select one file descriptor

  #### Parameters

  - where: string | Where

  #### Returns Promise<File | null>

  - Defined in packages/saltcorn-data/models/file.ts:542

### `Static` from_contents

- from_contents(
  name: string,
  mimetype: string,
  contents: string | ArrayBuffer | Buffer<ArrayBufferLike>,
  user_id: number,
  min_role_read?: number,
  folder?: string,
  ): Promise<File>

  Create new file

  #### Parameters

  - name: string
  - mimetype: string
  - contents: string | ArrayBuffer | Buffer<ArrayBufferLike>
  - user_id: number
  - min_role_read: number = 1
  - folder: string = "/"

  #### Returns Promise<File>

  - Defined in packages/saltcorn-data/models/file.ts:965

### `Static` from_file_on_disk

- from_file_on_disk(name: string, absoluteFolder: string): Promise<File>

  #### Parameters

  - name: string
  - absoluteFolder: string

  #### Returns Promise<File>

  - Defined in packages/saltcorn-data/models/file.ts:490

### `Static` from_req_files

- from_req_files(
  file: {
  mimetype: string;
  mv: Function;
  name: string;
  s3object?: boolean;
  size: number;
  },
  user_id: number,
  min_role_read?: number,
  folder?: string,
  ): Promise<File>

  Create new file

  #### Parameters

  - file: {
    mimetype: string;
    mv: Function;
    name: string;
    s3object?: boolean;
    size: number;
    }
  - user_id: number
  - min_role_read: number = 1
  - folder: string = "/"

  #### Returns Promise<File>

  - Defined in packages/saltcorn-data/models/file.ts:906

### `Static` get_new_path

- get_new_path(suggest?: string, renameIfExisting?: boolean): string

  Get absolute path to new file in db.connectObj.file_store.

  #### Parameters

  - `Optional` suggest: string

    path to file inside file store. If undefined that autogenerated uudv4 is used.

  - `Optional` renameIfExisting: boolean

  #### Returns string

  - path to file
  - Defined in packages/saltcorn-data/models/file.ts:840

### `Static` getDirCache

- getDirCache(): File[] | "building" | null

  #### Returns File[] | "building" | null

  - Defined in packages/saltcorn-data/models/file.ts:368

### `Static` isAbsoluteURL

- isAbsoluteURL(value?: string): boolean

  #### Parameters

  - `Optional` value: string

  #### Returns boolean

  - Defined in packages/saltcorn-data/models/file.ts:197

### `Static` nameToMimeType

- nameToMimeType(filepath: string): string | false

  #### Parameters

  - filepath: string

  #### Returns string | false

  - Defined in packages/saltcorn-data/models/file.ts:484

### `Static` new_folder

- new_folder(name: string, inFolder?: string): Promise<void>

  Create new folder

  #### Parameters

  - name: string
  - inFolder: string = ""

  #### Returns Promise<void>

  - Defined in packages/saltcorn-data/models/file.ts:593

### `Static` normalise

- normalise(fpath: string): string

  #### Parameters

  - fpath: string

  #### Returns string

  - Defined in packages/saltcorn-data/models/file.ts:178

### `Static` normalise_in_base

- normalise_in_base(
  trusted_base: string,
  ...unsafe_paths: string[],
  ): string | null

  #### Parameters

  - trusted_base: string
  - ...unsafe_paths: string[]

  #### Returns string | null

  - Defined in packages/saltcorn-data/models/file.ts:182

### `Static` normalizeFieldValueInput

- normalizeFieldValueInput(value?: string): string

  #### Parameters

  - `Optional` value: string

  #### Returns string

  - Defined in packages/saltcorn-data/models/file.ts:222

### `Static` pathToServeUrl

- pathToServeUrl(
  value?: string,
  opts?: {
  download?: boolean;
  filename?: string;
  preferDirect?: boolean;
  targetPrefix?: string;
  },
  ): string

  #### Parameters

  - `Optional` value: string
  - opts: {
    download?: boolean;
    filename?: string;
    preferDirect?: boolean;
    targetPrefix?: string;
    } = {}

  #### Returns string

  - Defined in packages/saltcorn-data/models/file.ts:229

### `Static` reallyBuildDirCache

- reallyBuildDirCache(): Promise<void>

  #### Returns Promise<void>

  - Defined in packages/saltcorn-data/models/file.ts:363

### `Static` relativePathFromFieldValue

- relativePathFromFieldValue(value?: string): string | null

  #### Parameters

  - `Optional` value: string

  #### Returns string | null

  - Defined in packages/saltcorn-data/models/file.ts:207

### `Static` rootFolder

- rootFolder(): Promise<File>

  #### Returns Promise<File>

  - Defined in packages/saltcorn-data/models/file.ts:263

### `Static` set_xattr_of_existing_file

- set_xattr_of_existing_file(
  name: string,
  absoluteFolder: string,
  user: User,
  ): Promise<void>

  #### Parameters

  - name: string
  - absoluteFolder: string
  - user: User

  #### Returns Promise<void>

  - Defined in packages/saltcorn-data/models/file.ts:1132

### `Static` update

- update(id: number, row: Row): Promise<void>

  Update File descriptor

  #### Parameters

  - id: number

    primary key

  - row: Row

    row data

  #### Returns Promise<void>

  no returns

  - Defined in packages/saltcorn-data/models/file.ts:665

### `Static` update_table_references

- update_table_references(from: string, to: string): Promise<void>

  #### Parameters

  - from: string
  - to: string

  #### Returns Promise<void>

  - Defined in packages/saltcorn-data/models/file.ts:766

### `Static` upload

- upload(file: { blob: Blob; fileObj: any }): Promise<any>

  This is a mobile-app function, it uploads a file to the saltcorn server.

  #### Parameters

  - file: { blob: Blob; fileObj: any }

    file to upload

  #### Returns Promise<any>

  JSON response from POST 'file/upload'

  - Defined in packages/saltcorn-data/models/file.ts:1113