Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update storage settings fix #178

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import Delete from '../deleteBucket.svelte';
import { trackEvent } from '$lib/actions/analytics';
import { writable } from 'svelte/store';
import { Models } from '@aw-labs/appwrite-console';

let showDelete = false;

Expand Down Expand Up @@ -69,161 +70,137 @@
} else isExtensionsDisabled = true;
}

async function toggleBucket() {
type TUpdateBucketMisc = {
successMessage?: string;
trackEventName: string;
trackEventData?: { value: boolean };
arePermsDisabled?: boolean;
};

async function updateBucket(updates: Partial<Models.Bucket>, misc: TUpdateBucketMisc) {
const values = { ...$bucket, ...updates };

try {
await sdkForProject.storage.updateBucket(
$bucket.$id,
$bucket.name,
undefined,
undefined,
enabled
values.$id,
values.name,
values.$permissions,
values.fileSecurity,
values.enabled,
values.maximumFileSize,
values.allowedFileExtensions,
values.compression,
values.encryption,
values.antivirus
);

invalidate(Dependencies.BUCKET);

if (misc.arePermsDisabled !== undefined) {
arePermsDisabled = misc.arePermsDisabled;
}

addNotification({
message: `${$bucket.name} has been updated`,
message: misc.successMessage ?? `${$bucket.name} has been updated`,
type: 'success'
});
trackEvent('submit_bucket_enable', {
value: enabled
});

if (misc.trackEventName) {
trackEvent(misc.trackEventName, misc.trackEventData);
}
} catch (error) {
addNotification({
message: error.message,
type: 'error'
});
}
}
async function updateName() {
try {
await sdkForProject.storage.updateBucket($bucket.$id, bucketName);
invalidate(Dependencies.BUCKET);
addNotification({
message: 'Name has been updated',
type: 'success'
});
trackEvent('submit_bucket_update_name');
} catch (error) {
addNotification({
message: error.message,
type: 'error'
});
}

function toggleBucket() {
updateBucket(
{
enabled
},
{
trackEventName: 'submit_bucket_enable',
trackEventData: {
value: enabled
}
}
);
}
async function updatePermissions() {
try {
await sdkForProject.storage.updateBucket($bucket.$id, $bucket.name, bucketPermissions);
invalidate(Dependencies.BUCKET);
arePermsDisabled = true;
addNotification({
message: 'Permissions have been updated',
type: 'success'
});
trackEvent('submit_bucket_update_permissions');
} catch (error) {
addNotification({
message: error.message,
type: 'error'
});
}

function updateName() {
updateBucket(
{
name: bucketName
},
{
successMessage: 'Name has been updated',
trackEventName: 'submit_bucket_update_name'
}
);
}
async function updateFileSecurity() {
try {
await sdkForProject.storage.updateBucket(
$bucket.$id,
$bucket.name,
$bucket.$permissions,
bucketFileSecurity
);
invalidate(Dependencies.BUCKET);
arePermsDisabled = true;
addNotification({
message: 'Security has been updated',
type: 'success'
});
trackEvent('submit_bucket_update_file_security');
} catch (error) {
addNotification({
message: error.message,
type: 'error'
});
}

function updatePermissions() {
updateBucket(
{
$permissions: bucketPermissions
},
{
successMessage: 'Permissions have been updated',
trackEventName: 'submit_bucket_update_permissions',
arePermsDisabled: true
}
);
}

async function updateSecurity() {
try {
await sdkForProject.storage.updateBucket(
$bucket.$id,
$bucket.name,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
function updateFileSecurity() {
updateBucket(
{
fileSecurity: bucketFileSecurity
},
{
successMessage: 'Security has been updated',
trackEventName: 'submit_bucket_update_file_security',
arePermsDisabled: true
}
);
}

function updateSecurity() {
updateBucket(
{
encryption,
antivirus
);
invalidate(Dependencies.BUCKET);
addNotification({
message: `${$bucket.name} has been updated`,
type: 'success'
});
trackEvent('submit_bucket_update_security');
} catch (error) {
addNotification({
message: error.message,
type: 'error'
});
}
},
{
trackEventName: 'submit_bucket_update_security'
}
);
}
async function updateMaxSize() {
let size = sizeToBytes(maxSize, $byteUnit);
try {
await sdkForProject.storage.updateBucket(
$bucket.$id,
$bucket.name,
undefined,
undefined,
undefined,
size
);
invalidate(Dependencies.BUCKET);
addNotification({
message: `${$bucket.name} has been updated`,
type: 'success'
});
trackEvent('submit_bucket_update_size');
} catch (error) {
addNotification({
message: error.message,
type: 'error'
});
}

function updateMaxSize() {
const size = sizeToBytes(maxSize, $byteUnit);
updateBucket(
{
maximumFileSize: size
},
{
trackEventName: 'submit_bucket_update_size'
}
);
}

async function updateAllowedExtensions() {
try {
await sdkForProject.storage.updateBucket(
$bucket.$id,
$bucket.name,
undefined,
undefined,
undefined,
undefined,
extensions
);
invalidate(Dependencies.BUCKET);
addNotification({
message: `${$bucket.name} has been updated`,
type: 'success'
});
trackEvent('submit_bucket_update_extensions');
} catch (error) {
addNotification({
message: error.message,
type: 'error'
});
}
function updateAllowedExtensions() {
updateBucket(
{
allowedFileExtensions: extensions
},
{
trackEventName: 'submit_bucket_update_extensions'
}
);
}

byteUnit.subscribe((b) => {
Expand Down Expand Up @@ -403,7 +380,9 @@
<Button
disabled={encryption === $bucket.encryption &&
antivirus === $bucket.antivirus}
submit>Update</Button>
submit
>Update
</Button>
</svelte:fragment>
</CardGrid>
</Form>
Expand All @@ -425,7 +404,8 @@

<svelte:fragment slot="actions">
<Button disabled={sizeInBytes === $bucket.maximumFileSize} submit
>Update</Button>
>Update
</Button>
</svelte:fragment>
</CardGrid>
</Form>
Expand Down