diff --git a/README.md b/README.md index 4ed91d0..c4c9bff 100644 --- a/README.md +++ b/README.md @@ -506,6 +506,17 @@ set. A secure API key can be generated by any password generator. - `POST /internal/import` for uploading an image while preserving the filename as the first alias. The upload format and response format are the same as the `POST /image` endpoint. +- `POST /internal/delete?alias={alias}` Delete an alias without requiring a delete token. + Available source arguments are + - `?alias={alias}` Purge a file by it's alias + - `?proxy={url}` Purge a proxied file by its URL + + This endpoint returns the following JSON + ```json + { + "msg": "ok", + } + ``` - `POST /internal/purge?alias={alias}` Purge a file by it's alias. This removes all aliases and files associated with the query. diff --git a/src/lib.rs b/src/lib.rs index 6ccb89e..7748481 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1485,6 +1485,37 @@ async fn purge( }))) } +#[tracing::instrument(name = "Deleting alias", skip(repo, config))] +async fn delete_alias( + web::Query(alias_query): web::Query, + repo: web::Data, + config: web::Data, +) -> Result { + if config.server.read_only { + return Err(UploadError::ReadOnly.into()); + } + + let alias = match alias_query { + AliasQuery::Alias { alias } => Serde::into_inner(alias), + AliasQuery::Proxy { proxy } => { + let Some(alias) = repo.related(proxy).await? else { + return Ok(HttpResponse::NotFound().finish()); + }; + alias + } + }; + + if let Some(token) = repo.delete_token(&alias).await? { + queue::cleanup_alias(&repo, alias, token).await?; + } else { + return Ok(HttpResponse::NotFound().finish()); + } + + Ok(HttpResponse::Ok().json(&serde_json::json!({ + "msg": "ok", + }))) +} + #[tracing::instrument(name = "Fetching aliases", skip(repo))] async fn aliases( web::Query(alias_query): web::Query, @@ -1645,6 +1676,7 @@ fn configure_endpoints( .service(web::resource("/import").route(web::post().to(import::))) .service(web::resource("/variants").route(web::delete().to(clean_variants))) .service(web::resource("/purge").route(web::post().to(purge))) + .service(web::resource("/delete").route(web::post().to(delete_alias))) .service(web::resource("/aliases").route(web::get().to(aliases))) .service(web::resource("/identifier").route(web::get().to(identifier::))) .service(web::resource("/set_not_found").route(web::post().to(set_not_found)))