Sharing: Delete expired files #225
Signed-off-by: Michael Mayer <michael@liquidbytes.net>
This commit is contained in:
parent
2e5840f3b0
commit
3de78e3124
|
@ -11,7 +11,6 @@ import (
|
|||
"github.com/photoprism/photoprism/internal/event"
|
||||
"github.com/photoprism/photoprism/internal/form"
|
||||
"github.com/photoprism/photoprism/internal/query"
|
||||
"github.com/photoprism/photoprism/internal/service/webdav"
|
||||
"github.com/photoprism/photoprism/pkg/txt"
|
||||
)
|
||||
|
||||
|
@ -138,13 +137,6 @@ func ShareWithAccount(router *gin.RouterGroup, conf *config.Config) {
|
|||
return
|
||||
}
|
||||
|
||||
w := webdav.New(m.AccURL, m.AccUser, m.AccPass)
|
||||
|
||||
if err := w.CreateDir(dst); err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": txt.UcFirst(err.Error())})
|
||||
return
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
dstFileName := dst + "/" + file.ShareFileName()
|
||||
|
||||
|
|
|
@ -29,10 +29,10 @@ type Account struct {
|
|||
RetryLimit int
|
||||
SharePath string `gorm:"type:varbinary(256);"`
|
||||
ShareSize string `gorm:"type:varbinary(16);"`
|
||||
ShareExpires uint
|
||||
ShareExpires int
|
||||
SyncPath string `gorm:"type:varbinary(256);"`
|
||||
SyncStatus string `gorm:"type:varbinary(16);"`
|
||||
SyncInterval uint
|
||||
SyncInterval int
|
||||
SyncUpload bool
|
||||
SyncDownload bool
|
||||
SyncDelete bool
|
||||
|
@ -85,7 +85,7 @@ func (m *Account) Delete(db *gorm.DB) error {
|
|||
|
||||
// Directories returns a list of directories or albums in an account.
|
||||
func (m *Account) Directories() (result fs.FileInfos, err error) {
|
||||
if m.AccType == string(service.TypeWebDAV) {
|
||||
if m.AccType == service.TypeWebDAV {
|
||||
c := webdav.New(m.AccURL, m.AccUser, m.AccPass)
|
||||
result, err = c.Directories("/", true)
|
||||
}
|
||||
|
|
|
@ -41,6 +41,7 @@ func NewFileShare(fileID, accountID uint, remoteName string) *FileShare {
|
|||
RemoteName: remoteName,
|
||||
Status: "new",
|
||||
Error: "",
|
||||
Errors: 0,
|
||||
}
|
||||
|
||||
return result
|
||||
|
|
|
@ -19,12 +19,12 @@ type Account struct {
|
|||
AccError string `json:"AccError"`
|
||||
AccShare bool `json:"AccShare"`
|
||||
AccSync bool `json:"AccSync"`
|
||||
RetryLimit uint `json:"RetryLimit"`
|
||||
RetryLimit int `json:"RetryLimit"`
|
||||
SharePath string `json:"SharePath"`
|
||||
ShareSize string `json:"ShareSize"`
|
||||
ShareExpires uint `json:"ShareExpires"`
|
||||
ShareExpires int `json:"ShareExpires"`
|
||||
SyncPath string `json:"SyncPath"`
|
||||
SyncInterval uint `json:"SyncInterval"`
|
||||
SyncInterval int `json:"SyncInterval"`
|
||||
SyncUpload bool `json:"SyncUpload"`
|
||||
SyncDownload bool `json:"SyncDownload"`
|
||||
SyncDelete bool `json:"SyncDelete"`
|
||||
|
|
|
@ -3,6 +3,7 @@ package photoprism
|
|||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/photoprism/photoprism/internal/config"
|
||||
"github.com/photoprism/photoprism/internal/entity"
|
||||
|
@ -56,12 +57,23 @@ func (s *Share) Start() (err error) {
|
|||
}
|
||||
|
||||
if len(files) == 0 {
|
||||
// No files to upload
|
||||
continue
|
||||
}
|
||||
|
||||
client := webdav.New(a.AccURL, a.AccUser, a.AccPass)
|
||||
existingDirs := make(map[string]string)
|
||||
|
||||
for _, file := range files {
|
||||
dir := filepath.Dir(file.RemoteName)
|
||||
|
||||
if _, ok := existingDirs[dir]; ok == false && dir != "/" && dir != "." {
|
||||
if err := client.CreateDir(dir); err != nil {
|
||||
log.Errorf("share: could not create directory %s", dir)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
srcFileName := s.conf.OriginalsPath() + string(os.PathSeparator) + file.File.FileName
|
||||
|
||||
if a.ShareSize != "" {
|
||||
|
@ -100,5 +112,40 @@ func (s *Share) Start() (err error) {
|
|||
}
|
||||
}
|
||||
|
||||
for _, a := range accounts {
|
||||
if a.AccType != service.TypeWebDAV {
|
||||
continue
|
||||
}
|
||||
|
||||
files, err := q.ExpiredFileShares(a)
|
||||
|
||||
if err != nil {
|
||||
log.Errorf("share: %s", err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
if len(files) == 0 {
|
||||
// No files to remove
|
||||
continue
|
||||
}
|
||||
|
||||
client := webdav.New(a.AccURL, a.AccUser, a.AccPass)
|
||||
|
||||
for _, file := range files {
|
||||
if err := client.Delete(file.RemoteName); err != nil {
|
||||
file.Errors++
|
||||
file.Error = err.Error()
|
||||
} else {
|
||||
file.Errors = 0
|
||||
file.Error = ""
|
||||
file.Status = entity.FileShareRemoved
|
||||
}
|
||||
|
||||
if err := db.Save(&file).Error; err != nil {
|
||||
log.Errorf("share: %s", err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
package query
|
||||
|
||||
import "github.com/photoprism/photoprism/internal/entity"
|
||||
import (
|
||||
"time"
|
||||
|
||||
// FileShares
|
||||
"github.com/photoprism/photoprism/internal/entity"
|
||||
)
|
||||
|
||||
// FileShares returns up to 100 file shares for a given account id and status.
|
||||
func (q *Query) FileShares(accountId uint, status string) (result []entity.FileShare, err error) {
|
||||
s := q.db.Where(&entity.FileShare{})
|
||||
|
||||
|
@ -25,3 +29,29 @@ func (q *Query) FileShares(accountId uint, status string) (result []entity.FileS
|
|||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// ExpiredFileShares returns up to 100 expired file shares for a given account.
|
||||
func (q *Query) ExpiredFileShares(account entity.Account) (result []entity.FileShare, err error) {
|
||||
if account.ShareExpires <= 0 {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
s := q.db.Where(&entity.FileShare{})
|
||||
|
||||
exp := time.Now().Add(time.Duration(account.ShareExpires)*time.Second)
|
||||
|
||||
s = s.Where("account_id = ?", account.ID)
|
||||
s = s.Where("status = ?", entity.FileShareShared)
|
||||
s = s.Where("updated_at < ?", exp)
|
||||
|
||||
s = s.Order("updated_at ASC")
|
||||
s = s.Limit(100).Offset(0)
|
||||
|
||||
s = s.Preload("File")
|
||||
|
||||
if err := s.Find(&result).Error; err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
|
|
@ -167,7 +167,7 @@ func Discover(rawUrl, user, pass string) (result Account, err error) {
|
|||
result.AccName = serviceUrl.Host
|
||||
}
|
||||
|
||||
result.AccType = string(h.ServiceType)
|
||||
result.AccType = h.ServiceType
|
||||
result.AccURL = serviceUrl.String()
|
||||
|
||||
return result, nil
|
||||
|
|
Loading…
Reference in a new issue