feat: create file route

This commit is contained in:
Arman committed 2022-06-20 14:32:12 +02:00
1 parent 7928fea575
commit 6bbef011f6
4 files changed
+261

No files matched your search

@@ -0,0 +1,36 @@
<script lang="ts">
import { browser } from '$app/env';
import { page } from '$app/stores';
import { tabs, title, backButton, copyData } from '$lib/stores/layout';
import { base } from '$app/paths';
import { file } from './store';
const bucketId = $page.params.bucket;
const fileId = $page.params.file;
$: {
if (browser) {
file.load(bucketId, fileId);
}
}
$: {
if ($file) {
title.set($file.name);
}
}
backButton.set(`${base}/console/${$page.params.project}/storage/bucket/${bucketId}`);
copyData.set({
text: fileId,
value: fileId
});
tabs.set([]);
</script>
<svelte:head>
<title>Appwrite - File</title>
</svelte:head>
<slot />
@@ -0,0 +1,36 @@
<script lang="ts">
import { goto } from '$app/navigation';
import { base } from '$app/paths';
import { page } from '$app/stores';
import { Modal } from '$lib/components';
import { Button, Form } from '$lib/elements/forms';
import { addNotification } from '$lib/stores/notifications';
import { sdkForProject } from '$lib/stores/sdk';
import { file } from './store';
export let showDelete = false;
const deleteUser = async () => {
try {
await sdkForProject.storage.deleteFile($file.bucketId, $file.$id);
showDelete = false;
await goto(`${base}/console/${$page.params.project}/storage/bucket/${$file.bucketId}`);
} catch (error) {
addNotification({
type: 'error',
message: error.message
});
}
};
</script>
<Form on:submit={deleteUser}>
<Modal bind:show={showDelete}>
<svelte:fragment slot="header">Delete File</svelte:fragment>
<p>Are you sure you want to delete <b>{$file.name}</b>?</p>
<svelte:fragment slot="footer">
<Button text on:click={() => (showDelete = false)}>Cancel</Button>
<Button secondary submit>Delete</Button>
</svelte:fragment>
</Modal>
</Form>
@@ -0,0 +1,172 @@
<script lang="ts">
import { Card, Alert } from '$lib/components';
import { Container } from '$lib/layout';
import { Button, InputTags } from '$lib/elements/forms';
import { Pill } from '$lib/elements';
import { file } from './store';
import { toLocaleDate } from '$lib/helpers/date';
import { sdkForProject } from '$lib/stores/sdk';
import { addNotification } from '$lib/stores/notifications';
import Delete from './_deleteFile.svelte';
let showDelete = false;
let fileRead = $file?.$read;
let fileWrite = $file?.$write;
let arePermsDisabled = true;
const getPreview = (fileId: string) =>
sdkForProject.storage.getFilePreview($file.bucketId, fileId, 205, 125).toString() +
'&mode=admin';
const getView = (fileId: string) =>
sdkForProject.storage.getFileView($file.bucketId, fileId).toString() + '&mode=admin';
$: if (fileRead || fileWrite) {
if (fileRead) {
if (JSON.stringify(fileRead) !== JSON.stringify($file.$read)) {
arePermsDisabled = false;
} else arePermsDisabled = true;
} else if (fileWrite) {
if (JSON.stringify(fileWrite) !== JSON.stringify($file.$write)) {
arePermsDisabled = false;
} else arePermsDisabled = true;
}
}
function downloadFile() {
return (
sdkForProject.storage.getFileDownload($file.bucketId, $file.$id).toString() +
'&mode=admin'
);
}
async function updatePermissions() {
try {
await sdkForProject.storage.updateFile($file.bucketId, $file.$id, fileRead, fileWrite);
$file.$read = fileRead;
$file.$write = fileWrite;
arePermsDisabled = true;
addNotification({
message: 'Permissions have been updated',
type: 'success'
});
} catch (error) {
addNotification({
message: error.message,
type: 'error'
});
}
}
const copy = async (value: string) => {
try {
await navigator.clipboard.writeText(value);
addNotification({
message: 'Copied to clipboard.',
type: 'success'
});
} catch (error) {
addNotification({
message: error.message,
type: 'error'
});
}
};
</script>
<Container>
{#if $file}
<Card>
<div class="u-flex u-main-space-between u-gap-12 common-section">
<div class="u-flex u-gap-12">
<img width="205" height="125" src={getPreview($file.$id)} alt={$file.name} />
<div>
<h2 class="heading-level-7">{$file.name}</h2>
<Pill button on:click={() => copy(getView($file.$id))}
><i class="icon-duplicate" />File URL
</Pill>
</div>
</div>
<div>
<p>MIME Type: {$file.mimeType}</p>
<p>Size: {$file.sizeOriginal}</p>
<p>Created: {toLocaleDate($file.dateCreated)}</p>
<p>Last Updated: {toLocaleDate($file?.dateUpdated)}</p>
</div>
</div>
<div class="u-flex u-main-space-end common-section">
<Button secondary href={downloadFile()}>
<span class="icon-download" aria-hidden="true" />
<span class="text"> Download</span></Button>
</div>
</Card>
<Card>
<div class="u-flex u-main-space-between u-gap-12 common-section">
<div>
<h6 class="heading-level-7">Update Permissions</h6>
<p>
Assign read or write permissions at the Bucket Level or File Level. If
Bucket Level permissions are enabled, file permissions will be ignored.
</p>
</div>
<div>
<Alert type="info">
<svelte:fragment slot="title">
You have Bucket Level permissions enabled
</svelte:fragment>
<p>
If you want to assign permissions specific to this file, you will need
to update your <a class="link" href="#"> Bucket Settings</a> to enable File
Level permissions.
</p>
</Alert>
<ul class="common-section">
<InputTags
id="read"
label="Read Access"
placeholder="User ID, Team ID, or Role"
bind:tags={fileRead} />
<InputTags
id="write"
label="Write Access"
placeholder="User ID, Team ID, or Role"
bind:tags={fileWrite} />
</ul>
</div>
</div>
<div class="u-flex u-main-space-end common-section">
<Button
disabled={arePermsDisabled}
on:click={() => {
updatePermissions();
}}>Update</Button>
</div>
</Card>
<Card>
<div class="u-flex u-main-space-between u-gap-12 common-section">
<div>
<h6 class="heading-level-7">Delete file</h6>
<p>
The file will be permanently deleted, including all the files within it.
This action is irreversible.
</p>
</div>
<div>
<div class="user-profile-button">
<span class="user-profile-info">
<h6 class="heading-level-7">{$file.name}</h6>
<p>Last Updated: {toLocaleDate($file?.dateUpdated)}</p>
</span>
</div>
</div>
</div>
<div class="u-flex u-main-space-end common-section">
<Button secondary on:click={() => (showDelete = true)}>Delete</Button>
</div>
</Card>
{/if}
</Container>
<Delete bind:showDelete />
@@ -0,0 +1,17 @@
import { sdkForProject } from '$lib/stores/sdk';
import type { Models } from 'src/sdk';
import { writable } from 'svelte/store';
function createFileStore() {
const { subscribe, set } = writable<Models.File>();
return {
subscribe,
set,
load: async (bucketId: string, fileId: string) => {
set(await sdkForProject.storage.getFile(bucketId, fileId));
}
};
}
export const file = createFileStore();