mirror of
https://github.com/photoprism/photoprism.git
synced 2025-12-12 00:34:13 +01:00
34 lines
1.1 KiB
Go
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
|
|
}
|