2020-10-04 14:21:40 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-03-20 16:18:27 +01:00
|
|
|
|
2020-10-04 14:21:40 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2022-10-15 21:54:11 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/get"
|
2020-10-04 14:21:40 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
2023-03-20 16:18:27 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2020-10-04 14:21:40 +02:00
|
|
|
)
|
|
|
|
|
2022-07-22 19:18:42 +02:00
|
|
|
// DeleteFile removes a file from storage.
|
2020-10-04 14:21:40 +02:00
|
|
|
// DELETE /api/v1/photos/:uid/files/:file_uid
|
|
|
|
//
|
|
|
|
// Parameters:
|
2022-08-10 16:09:21 +02:00
|
|
|
//
|
|
|
|
// uid: string Photo UID as returned by the API
|
|
|
|
// file_uid: string File UID as returned by the API
|
2020-10-04 14:21:40 +02:00
|
|
|
func DeleteFile(router *gin.RouterGroup) {
|
|
|
|
router.DELETE("/photos/:uid/files/:file_uid", func(c *gin.Context) {
|
2022-09-28 09:01:17 +02:00
|
|
|
s := Auth(c, acl.ResourceFiles, acl.ActionDelete)
|
2020-10-04 14:21:40 +02:00
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
if s.Abort(c) {
|
2020-10-04 14:21:40 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
conf := get.Config()
|
2020-10-04 14:21:40 +02:00
|
|
|
|
|
|
|
if conf.ReadOnly() || !conf.Settings().Features.Edit {
|
|
|
|
Abort(c, http.StatusForbidden, i18n.ErrReadOnly)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
photoUid := clean.UID(c.Param("uid"))
|
|
|
|
fileUid := clean.UID(c.Param("file_uid"))
|
2020-10-04 14:21:40 +02:00
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
file, err := query.FileByUID(fileUid)
|
2020-10-04 14:21:40 +02:00
|
|
|
|
2022-07-22 19:18:42 +02:00
|
|
|
// Found?
|
2020-10-04 14:21:40 +02:00
|
|
|
if err != nil {
|
2022-07-22 19:18:42 +02:00
|
|
|
log.Errorf("files: %s (delete)", err)
|
2020-10-04 14:21:40 +02:00
|
|
|
AbortEntityNotFound(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-22 19:18:42 +02:00
|
|
|
// Primary file?
|
2020-10-04 14:21:40 +02:00
|
|
|
if file.FilePrimary {
|
2022-07-22 19:18:42 +02:00
|
|
|
log.Errorf("files: cannot delete primary file")
|
2020-10-04 14:21:40 +02:00
|
|
|
AbortDeleteFailed(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-22 19:18:42 +02:00
|
|
|
// Compose storage filename.
|
2020-10-04 14:21:40 +02:00
|
|
|
fileName := photoprism.FileName(file.FileRoot, file.FileName)
|
|
|
|
baseName := filepath.Base(fileName)
|
|
|
|
|
|
|
|
mediaFile, err := photoprism.NewMediaFile(fileName)
|
|
|
|
|
|
|
|
if err != nil {
|
2022-07-22 19:18:42 +02:00
|
|
|
log.Errorf("files: %s (delete %s)", err, clean.Log(baseName))
|
2020-10-04 14:21:40 +02:00
|
|
|
AbortEntityNotFound(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-19 21:11:17 +01:00
|
|
|
// Report file deletion.
|
|
|
|
event.AuditWarn([]string{ClientIP(c), s.UserName, "delete", file.FileName})
|
|
|
|
|
2022-07-22 19:18:42 +02:00
|
|
|
// Remove file from storage.
|
2022-07-21 20:23:00 +02:00
|
|
|
if err = mediaFile.Remove(); err != nil {
|
2022-07-22 19:18:42 +02:00
|
|
|
log.Errorf("files: %s (delete %s from folder)", err, clean.Log(baseName))
|
|
|
|
} else {
|
|
|
|
log.Infof("files: deleted %s", clean.Log(baseName))
|
2020-10-04 14:21:40 +02:00
|
|
|
}
|
|
|
|
|
2022-07-22 19:18:42 +02:00
|
|
|
// Remove file from index.
|
2022-07-21 20:23:00 +02:00
|
|
|
if err = file.Delete(true); err != nil {
|
2022-07-22 19:18:42 +02:00
|
|
|
log.Errorf("files: %s (delete %s from index)", err, clean.Log(baseName))
|
2020-10-04 14:21:40 +02:00
|
|
|
AbortDeleteFailed(c)
|
|
|
|
return
|
2022-07-22 19:18:42 +02:00
|
|
|
} else {
|
|
|
|
log.Debugf("files: removed %s from index", clean.Log(baseName))
|
2020-10-04 14:21:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Notify clients by publishing events.
|
2022-10-02 11:38:30 +02:00
|
|
|
PublishPhotoEvent(EntityUpdated, photoUid, c)
|
2020-10-04 14:21:40 +02:00
|
|
|
|
2022-07-22 19:18:42 +02:00
|
|
|
// Show translated success message.
|
2020-10-04 14:21:40 +02:00
|
|
|
event.SuccessMsg(i18n.MsgFileDeleted)
|
|
|
|
|
2022-10-02 11:38:30 +02:00
|
|
|
if p, err := query.PhotoPreloadByUID(photoUid); err != nil {
|
2020-10-04 14:21:40 +02:00
|
|
|
AbortEntityNotFound(c)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
c.JSON(http.StatusOK, p)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|