Skip to content

Commit

Permalink
refactor(useS3Object): Resolve key at upload function
Browse files Browse the repository at this point in the history
  • Loading branch information
becem-gharbi committed Sep 28, 2023
1 parent b638e14 commit 9d63e9c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
2 changes: 1 addition & 1 deletion playground/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const url = ref(
async function handleChange(files: File[]) {
url.value = await upload(files[0], {
url: url.value,
key: "my_folder/my_file"
// key: "my_folder/my_file"
});
}
</script>
17 changes: 8 additions & 9 deletions src/runtime/composables/useS3Object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ export default function () {
const { callHook } = useNuxtApp();
const config = useRuntimeConfig();

async function create(file: File, key?: string) {
key ||= uuidv4();

async function create(file: File, key: string) {
const formData = new FormData();

formData.append("file", file);

const headers = { authorization: "" };
Expand All @@ -24,9 +23,7 @@ export default function () {
return getURL(key);
}

async function update(url: string, file: File, newKey?: string) {
newKey ||= uuidv4();

async function update(url: string, file: File, key: string) {
const headers = { authorization: "" };

await callHook("s3:auth", headers);
Expand All @@ -35,7 +32,7 @@ export default function () {

await callHook("s3:auth", headers);

return create(file, newKey);
return create(file, key);
}

/**
Expand All @@ -62,13 +59,15 @@ export default function () {
async function upload(file: File, opts?: { url?: string; key?: string }) {
checkType(file.type);

const _key = opts?.key || uuidv4();

if (opts?.url) {
if (isValidURL(opts.url)) {
return update(opts.url, file, opts.key);
return update(opts.url, file, _key);
}
}

return create(file, opts?.key);
return create(file, _key);
}

/**
Expand Down

0 comments on commit 9d63e9c

Please sign in to comment.