50 lines
1.1 KiB
Go
50 lines
1.1 KiB
Go
package files
|
|
|
|
import (
|
|
"errors"
|
|
"gis/app"
|
|
"gis/server/httputil"
|
|
"net/http"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/minio/minio-go/v7"
|
|
)
|
|
|
|
func deleteFileRoute(application *app.App) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
id := r.PathValue("file_id")
|
|
|
|
var storageKey string
|
|
err := application.Db.QueryRow(r.Context(),
|
|
"SELECT storage_key FROM files WHERE id=$1",
|
|
id,
|
|
).Scan(&storageKey)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
httputil.WriteJSON(w, http.StatusNotFound, map[string]string{"error": "not found"})
|
|
return
|
|
}
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
if err := application.S3.RemoveObject(
|
|
r.Context(),
|
|
application.Cfg.S3Bucket,
|
|
storageKey,
|
|
minio.RemoveObjectOptions{},
|
|
); err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
_, err = application.Db.Exec(r.Context(), "DELETE FROM files WHERE id=$1", id)
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
}
|