# File Management

To manipulate file ([`upload`](#upload-file), [`write`](#write-file)or [`downlaod`](#download-file)), you need to generate a `PreSignUrl`

{% hint style="info" %}
You can securly send the `PreSignUrl` to your Client to let him [`upload`](#upload-file), [`write`](#write-file) `or`[`downlaod`](#download-file) a file from your storage.
{% endhint %}

When Uploading or Writing a file, it will automatically generate a new thumbnail for this file. You can disabling it by providing <mark style="color:blue;">`should_not_generate_thumbnail=true`</mark> as Query params (second parameter of this `uploadApplicationFile` function

### Upload File

<pre class="language-typescript" data-overflow="wrap" data-line-numbers><code class="lang-typescript"><strong>// Generate a preSignUrl
</strong>const resultPreSignUrl = await getPreSignedUrlApplicationFile(
  { applicationId: "my-application-id" },
  {
    type: PreSignUrlType.UPLOAD,
    fileName: fileToUpload.name,
  },
);

const transfer = new Transfer({
  name: "My Transfer",
  type: TransferType.UPLOAD,
});

transfer.setAction(() => {
  // Set any action to trigger on Upload/Download Progression
});

// Use the PreSignUrl to upload the file
await uploadApplicationFile(
  { token: resultPreSignUrl.data.preSignUrl },
  {},
  file,
  { transfer },
);
</code></pre>

### Write File

{% code overflow="wrap" lineNumbers="true" %}

```typescript
// Generate a preSignUrl
const resultPreSignUrl = await getPreSignedUrlApplicationFile(
  { 
    applicationId: "my-application-id", 
    applicationFileId: "file-id" 
  },
  {
    type: PreSignUrlType.WRITE,
  },
);

const transfer = new Transfer({
  name: "My Transfer",
  type: TransferType.UPLOAD,
});

transfer.setAction(() => {
  // Set any action to trigger on Upload/Download Progression
});

// Use the PreSignUrl to write the new file
await writeApplicationFile(
  { token: resultPreSignUrl.data.preSignUrl },
  {},
  file,
  { transfer },
);
```

{% endcode %}

### Download File

<pre class="language-typescript" data-overflow="wrap" data-line-numbers><code class="lang-typescript">// Generate a preSignUrl
const resultPreSignUrl = await getPreSignedUrlApplicationFile(
  { 
    applicationId: "my-application-id", 
  },
  {
    type: PreSignUrlType.DOWNLOAD,
    userApplicationFileId: userApplicationFile.id,
  },
);

const transfer = new Transfer({
  name: "My Transfer",
  type: TransferType.DOWNLOAD,
});

transfer.setAction(() => {
  // Set any action to trigger on Upload/Download Progression
});

// Use the PreSignUrl to downloadthe new file
const result = await downloadApplicationFile(
<strong>  { token: resultPreSignUrl.data.preSignUrl },
</strong>  { transfer },
);
</code></pre>

### Publish a file

Each file get be accessed from `PreSignUrl` but you can also enable a public access link to let anybody download your file, such as a Image Provider for your Website

{% code overflow="wrap" lineNumbers="true" %}

```typescript
// By reseting the PublicAccess, you can also enable it
await resetPublicAccessApplicationFile({
  applicationId: "application-id",
  applicationFileId: "application-file-id",
});

// You can also disable this feature by unsetting the PublicAccess
await unsetPublicAccessApplicationFile({
  applicationId: "application-id",
  applicationFileId: "application-file-id",
});
```

{% endcode %}

{% hint style="info" %}
You can download (without authentication) file by PublicAccess with <mark style="color:blue;">`downloadWithPublicAccessApplicationFile`</mark> method&#x20;
{% endhint %}

### Upload from an external link

You can provide a link referring to a file to upload it to your storage.

```typescript
await uploadFromUrlApplicationFile(
  { applicationId: "application-id" }, 
  {
    name: "filename.png",
    url: "https://some-url.com/file/download",
  },
);
```
