Add server-side tus delete handler

This commit is contained in:
Oleg Lobanov 2024-04-25 01:21:49 +02:00
parent e34c1d5845
commit e48235e395
No known key found for this signature in database
3 changed files with 4 additions and 24 deletions

View File

@ -200,35 +200,14 @@ function calcProgress(filePath: string) {
fileData.lastProgressTimestamp = Date.now(); fileData.lastProgressTimestamp = Date.now();
} }
export async function abortAllUploads() { export function abortAllUploads() {
const deletePromises = [];
for (const filePath in CURRENT_UPLOAD_LIST) { for (const filePath in CURRENT_UPLOAD_LIST) {
if (CURRENT_UPLOAD_LIST[filePath].interval) { if (CURRENT_UPLOAD_LIST[filePath].interval) {
clearInterval(CURRENT_UPLOAD_LIST[filePath].interval); clearInterval(CURRENT_UPLOAD_LIST[filePath].interval);
} }
if (CURRENT_UPLOAD_LIST[filePath].upload) { if (CURRENT_UPLOAD_LIST[filePath].upload) {
//setting to false since the current tus method tries to delete against a non-existent endpoint CURRENT_UPLOAD_LIST[filePath].upload.abort(true);
CURRENT_UPLOAD_LIST[filePath].upload.abort(false); //TODO figure out how to do this properly through tus
} }
// Make a DELETE request to remove the file from the server
// TODO figure out how to do this properly through tus
const deleteUrl = `${baseURL}/api/resources/${filePath}`;
const deletePromise = fetchURL(deleteUrl, {
method: "DELETE"
}).then(response => {
if (response.status !== 200) {
console.error(`Failed to delete file: ${response.status} ${response.statusText}`);
}
}).catch(error => {
console.error(`Error deleting file: ${error.message}`);
});
deletePromises.push(deletePromise);
delete CURRENT_UPLOAD_LIST[filePath]; delete CURRENT_UPLOAD_LIST[filePath];
} }
await Promise.all(deletePromises);
} }

View File

@ -69,6 +69,7 @@ func NewHandler(
api.PathPrefix("/tus").Handler(monkey(tusPostHandler(), "/api/tus")).Methods("POST") api.PathPrefix("/tus").Handler(monkey(tusPostHandler(), "/api/tus")).Methods("POST")
api.PathPrefix("/tus").Handler(monkey(tusHeadHandler(), "/api/tus")).Methods("HEAD", "GET") api.PathPrefix("/tus").Handler(monkey(tusHeadHandler(), "/api/tus")).Methods("HEAD", "GET")
api.PathPrefix("/tus").Handler(monkey(tusPatchHandler(), "/api/tus")).Methods("PATCH") api.PathPrefix("/tus").Handler(monkey(tusPatchHandler(), "/api/tus")).Methods("PATCH")
api.PathPrefix("/tus").Handler(monkey(resourceDeleteHandler(fileCache), "/api/tus")).Methods("DELETE")
api.PathPrefix("/usage").Handler(monkey(diskUsage, "/api/usage")).Methods("GET") api.PathPrefix("/usage").Handler(monkey(diskUsage, "/api/usage")).Methods("GET")

View File

@ -87,7 +87,7 @@ func resourceDeleteHandler(fileCache FileCache) handleFunc {
return errToStatus(err), err return errToStatus(err), err
} }
return http.StatusOK, nil return http.StatusNoContent, nil
}) })
} }