Files
photoprism/internal/api/download/register.go
2025-04-11 18:41:54 +02:00

34 lines
1.1 KiB
Go

package download
import (
"errors"
"github.com/photoprism/photoprism/internal/event"
"github.com/photoprism/photoprism/pkg/authn"
"github.com/photoprism/photoprism/pkg/fs"
"github.com/photoprism/photoprism/pkg/rnd"
)
// Register generated an event to make the specified file available
// for download until the cache expires, or the server is restarted.
func Register(fileUuid, fileName string) error {
if !rnd.IsUUID(fileUuid) {
event.AuditWarn([]string{"api", "create download token", "%s", authn.Failed}, fileName)
return errors.New("invalid file uuid")
}
if fileName = fs.Abs(fileName); !fs.FileExists(fileName) {
event.AuditWarn([]string{"api", "create download token", "%s", authn.Failed}, fileName)
return errors.New("file not found")
} else if Deny(fileName) {
event.AuditErr([]string{"api", "create download token", "%s", authn.Denied}, fileName)
return errors.New("forbidden file path")
}
event.AuditInfo([]string{"api", "create download token", "%s", authn.Succeeded}, fileName, expires.String())
cache.SetDefault(fileUuid, fileName)
return nil
}