2020-10-04 14:21:40 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2021-12-14 18:34:52 +01:00
|
|
|
|
2020-10-04 14:21:40 +02:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/photoprism/photoprism/internal/acl"
|
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
|
|
|
"github.com/photoprism/photoprism/internal/i18n"
|
|
|
|
"github.com/photoprism/photoprism/internal/photoprism"
|
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
|
|
|
"github.com/photoprism/photoprism/internal/service"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DELETE /api/v1/photos/:uid/files/:file_uid
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// uid: string Photo UID as returned by the API
|
|
|
|
// file_uid: string File UID as returned by the API
|
|
|
|
func DeleteFile(router *gin.RouterGroup) {
|
|
|
|
router.DELETE("/photos/:uid/files/:file_uid", func(c *gin.Context) {
|
|
|
|
s := Auth(SessionID(c), acl.ResourceFiles, acl.ActionDelete)
|
|
|
|
|
|
|
|
if s.Invalid() {
|
|
|
|
AbortUnauthorized(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
conf := service.Config()
|
|
|
|
|
|
|
|
if conf.ReadOnly() || !conf.Settings().Features.Edit {
|
|
|
|
Abort(c, http.StatusForbidden, i18n.ErrReadOnly)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-15 09:42:07 +02:00
|
|
|
photoUID := clean.IdString(c.Param("uid"))
|
|
|
|
fileUID := clean.IdString(c.Param("file_uid"))
|
2020-10-04 14:21:40 +02:00
|
|
|
|
|
|
|
file, err := query.FileByUID(fileUID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("photo: %s (delete file)", err)
|
|
|
|
AbortEntityNotFound(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if file.FilePrimary {
|
2022-01-05 11:40:44 +01:00
|
|
|
log.Errorf("photo: cannot delete primary file")
|
2020-10-04 14:21:40 +02:00
|
|
|
AbortDeleteFailed(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fileName := photoprism.FileName(file.FileRoot, file.FileName)
|
|
|
|
baseName := filepath.Base(fileName)
|
|
|
|
|
|
|
|
mediaFile, err := photoprism.NewMediaFile(fileName)
|
|
|
|
|
|
|
|
if err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Errorf("photo: %s (delete %s)", err, clean.Log(baseName))
|
2020-10-04 14:21:40 +02:00
|
|
|
AbortEntityNotFound(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := mediaFile.Remove(); err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Errorf("photo: %s (delete %s from folder)", err, clean.Log(baseName))
|
2020-10-04 14:21:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := file.Delete(true); err != nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Errorf("photo: %s (delete %s from index)", err, clean.Log(baseName))
|
2020-10-04 14:21:40 +02:00
|
|
|
AbortDeleteFailed(c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify clients by publishing events.
|
|
|
|
PublishPhotoEvent(EntityUpdated, photoUID, c)
|
|
|
|
|
|
|
|
event.SuccessMsg(i18n.MsgFileDeleted)
|
|
|
|
|
|
|
|
if p, err := query.PhotoPreloadByUID(photoUID); err != nil {
|
|
|
|
AbortEntityNotFound(c)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
c.JSON(http.StatusOK, p)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|