Skip to content

Commit

Permalink
Merge pull request #281 from yavuz/main
Browse files Browse the repository at this point in the history
  • Loading branch information
Intevel authored Nov 17, 2024
2 parents 5d4af61 + f805a9e commit f1e7057
Showing 1 changed file with 63 additions and 1 deletion.
64 changes: 63 additions & 1 deletion src/runtime/composables/useDirectusFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,67 @@ export const useDirectusFiles = () => {
return url.href
}

return { getFiles, getThumbnail }
const uploadFiles = async (fileData, options = {}) => {
try {
const formData = new FormData();

if (Array.isArray(fileData)) {
fileData.forEach((file) => {
formData.append('file[]', file);
});
} else {
formData.append('file', fileData);
}

const uploadResponse = await directus("/files", {
method: "POST",
body: formData
});

const fileId = uploadResponse.data.id;

if (options.title || options.tags) {
const updateData = {};
if (options.title) updateData.title = options.title;
if (options.tags) updateData.tags = options.tags;

await directus(`/files/${fileId}`, {
method: "PATCH",
body: updateData
});

const updatedFile = await directus(`/files/${fileId}`, {
method: "GET"
});

return updatedFile.data;
}

return uploadResponse.data;

} catch (error) {
console.error('File upload error:', error);
throw error;
}
};

const deleteFiles = async (fileId) => {
try {
if (Array.isArray(fileId)) {
await directus("/files", {
method: "DELETE",
body: fileId
});
} else {
await directus(`/files/${fileId}`, {
method: "DELETE"
});
}
} catch (error) {
console.error('File upload error:', error);
throw error;
}
};

return { getFiles, getThumbnail, uploadFiles, deleteFiles }
}

0 comments on commit f1e7057

Please sign in to comment.