Sharing: Delete expired files #225

Signed-off-by: Michael Mayer <michael@liquidbytes.net>
This commit is contained in:
Michael Mayer 2020-04-04 13:53:52 +02:00
parent 2e5840f3b0
commit 3de78e3124
7 changed files with 87 additions and 17 deletions

View file

@ -11,7 +11,6 @@ import (
"github.com/photoprism/photoprism/internal/event" "github.com/photoprism/photoprism/internal/event"
"github.com/photoprism/photoprism/internal/form" "github.com/photoprism/photoprism/internal/form"
"github.com/photoprism/photoprism/internal/query" "github.com/photoprism/photoprism/internal/query"
"github.com/photoprism/photoprism/internal/service/webdav"
"github.com/photoprism/photoprism/pkg/txt" "github.com/photoprism/photoprism/pkg/txt"
) )
@ -138,13 +137,6 @@ func ShareWithAccount(router *gin.RouterGroup, conf *config.Config) {
return 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 { for _, file := range files {
dstFileName := dst + "/" + file.ShareFileName() dstFileName := dst + "/" + file.ShareFileName()

View file

@ -29,10 +29,10 @@ type Account struct {
RetryLimit int RetryLimit int
SharePath string `gorm:"type:varbinary(256);"` SharePath string `gorm:"type:varbinary(256);"`
ShareSize string `gorm:"type:varbinary(16);"` ShareSize string `gorm:"type:varbinary(16);"`
ShareExpires uint ShareExpires int
SyncPath string `gorm:"type:varbinary(256);"` SyncPath string `gorm:"type:varbinary(256);"`
SyncStatus string `gorm:"type:varbinary(16);"` SyncStatus string `gorm:"type:varbinary(16);"`
SyncInterval uint SyncInterval int
SyncUpload bool SyncUpload bool
SyncDownload bool SyncDownload bool
SyncDelete 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. // Directories returns a list of directories or albums in an account.
func (m *Account) Directories() (result fs.FileInfos, err error) { 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) c := webdav.New(m.AccURL, m.AccUser, m.AccPass)
result, err = c.Directories("/", true) result, err = c.Directories("/", true)
} }

View file

@ -41,6 +41,7 @@ func NewFileShare(fileID, accountID uint, remoteName string) *FileShare {
RemoteName: remoteName, RemoteName: remoteName,
Status: "new", Status: "new",
Error: "", Error: "",
Errors: 0,
} }
return result return result

View file

@ -19,12 +19,12 @@ type Account struct {
AccError string `json:"AccError"` AccError string `json:"AccError"`
AccShare bool `json:"AccShare"` AccShare bool `json:"AccShare"`
AccSync bool `json:"AccSync"` AccSync bool `json:"AccSync"`
RetryLimit uint `json:"RetryLimit"` RetryLimit int `json:"RetryLimit"`
SharePath string `json:"SharePath"` SharePath string `json:"SharePath"`
ShareSize string `json:"ShareSize"` ShareSize string `json:"ShareSize"`
ShareExpires uint `json:"ShareExpires"` ShareExpires int `json:"ShareExpires"`
SyncPath string `json:"SyncPath"` SyncPath string `json:"SyncPath"`
SyncInterval uint `json:"SyncInterval"` SyncInterval int `json:"SyncInterval"`
SyncUpload bool `json:"SyncUpload"` SyncUpload bool `json:"SyncUpload"`
SyncDownload bool `json:"SyncDownload"` SyncDownload bool `json:"SyncDownload"`
SyncDelete bool `json:"SyncDelete"` SyncDelete bool `json:"SyncDelete"`

View file

@ -3,6 +3,7 @@ package photoprism
import ( import (
"fmt" "fmt"
"os" "os"
"path/filepath"
"github.com/photoprism/photoprism/internal/config" "github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/entity" "github.com/photoprism/photoprism/internal/entity"
@ -56,12 +57,23 @@ func (s *Share) Start() (err error) {
} }
if len(files) == 0 { if len(files) == 0 {
// No files to upload
continue continue
} }
client := webdav.New(a.AccURL, a.AccUser, a.AccPass) client := webdav.New(a.AccURL, a.AccUser, a.AccPass)
existingDirs := make(map[string]string)
for _, file := range files { 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 srcFileName := s.conf.OriginalsPath() + string(os.PathSeparator) + file.File.FileName
if a.ShareSize != "" { 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 return err
} }

View file

@ -1,8 +1,12 @@
package query 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) { func (q *Query) FileShares(accountId uint, status string) (result []entity.FileShare, err error) {
s := q.db.Where(&entity.FileShare{}) s := q.db.Where(&entity.FileShare{})
@ -25,3 +29,29 @@ func (q *Query) FileShares(accountId uint, status string) (result []entity.FileS
return result, nil 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
}

View file

@ -167,7 +167,7 @@ func Discover(rawUrl, user, pass string) (result Account, err error) {
result.AccName = serviceUrl.Host result.AccName = serviceUrl.Host
} }
result.AccType = string(h.ServiceType) result.AccType = h.ServiceType
result.AccURL = serviceUrl.String() result.AccURL = serviceUrl.String()
return result, nil return result, nil