From 36919b69a0db534245d7feff715499b884e7a961 Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Wed, 15 Jul 2020 10:13:54 +0200 Subject: [PATCH] Backend: Improve logging and enforcement of file size limit Signed-off-by: Michael Mayer --- internal/photoprism/import_worker.go | 22 ++++++++++++++++------ internal/photoprism/index_related.go | 24 ++++++++++++++++++------ 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/internal/photoprism/import_worker.go b/internal/photoprism/import_worker.go index 314485de6..8bb3752c4 100644 --- a/internal/photoprism/import_worker.go +++ b/internal/photoprism/import_worker.go @@ -121,18 +121,21 @@ func ImportWorker(jobs <-chan ImportJob) { done := make(map[string]bool) ind := imp.index + sizeLimit := ind.conf.OriginalsLimit() if related.Main != nil { + f := related.Main + // Enforce file size limit for originals. - if ind.conf.OriginalsLimit() > 0 && related.Main.FileSize() > ind.conf.OriginalsLimit() { - log.Warnf("import: %s exceeds file size limit (%d / %d MB)", filepath.Base(related.Main.FileName()), related.Main.FileSize()/(1024*1024), ind.conf.OriginalsLimit()/(1024*1024)) + if sizeLimit > 0 && f.FileSize() > sizeLimit { + log.Warnf("import: %s exceeds file size limit (%d / %d MB)", txt.Quote(f.BaseName()), f.FileSize()/(1024*1024), sizeLimit/(1024*1024)) continue } - res := ind.MediaFile(related.Main, indexOpt, originalName) + res := ind.MediaFile(f, indexOpt, originalName) - log.Infof("import: %s main %s file %s", res, related.Main.FileType(), txt.Quote(related.Main.RelName(ind.originalsPath()))) - done[related.Main.FileName()] = true + log.Infof("import: %s main %s file %s", res, f.FileType(), txt.Quote(f.RelName(ind.originalsPath()))) + done[f.FileName()] = true if res.Success() { if err := entity.AddPhotoToAlbums(res.PhotoUID, opt.Albums); err != nil { @@ -154,9 +157,16 @@ func ImportWorker(jobs <-chan ImportJob) { continue } - res := ind.MediaFile(f, indexOpt, "") done[f.FileName()] = true + // Enforce file size limit for originals. + if sizeLimit > 0 && f.FileSize() > sizeLimit { + log.Warnf("import: %s exceeds file size limit (%d / %d MB)", txt.Quote(f.BaseName()), f.FileSize()/(1024*1024), sizeLimit/(1024*1024)) + continue + } + + res := ind.MediaFile(f, indexOpt, "") + log.Infof("import: %s related %s file %s", res, f.FileType(), txt.Quote(f.RelName(ind.originalsPath()))) } diff --git a/internal/photoprism/index_related.go b/internal/photoprism/index_related.go index e44a44bec..7948aa53e 100644 --- a/internal/photoprism/index_related.go +++ b/internal/photoprism/index_related.go @@ -2,7 +2,6 @@ package photoprism import ( "fmt" - "path/filepath" "github.com/photoprism/photoprism/internal/query" "github.com/photoprism/photoprism/pkg/fs" @@ -18,15 +17,16 @@ func IndexMain(related *RelatedFiles, ind *Index, opt IndexOptions) (result Inde return result } + f := related.Main + sizeLimit := ind.conf.OriginalsLimit() + // Enforce file size limit for originals. - if ind.conf.OriginalsLimit() > 0 && related.Main.FileSize() > ind.conf.OriginalsLimit() { - result.Err = fmt.Errorf("index: %s exceeds file size limit for originals [%d / %d MB]", filepath.Base(related.Main.FileName()), related.Main.FileSize()/(1024*1024), ind.conf.OriginalsLimit()/(1024*1024)) + if sizeLimit > 0 && f.FileSize() > sizeLimit { + result.Err = fmt.Errorf("index: %s exceeds file size limit (%d / %d MB)", txt.Quote(f.BaseName()), f.FileSize()/(1024*1024), sizeLimit/(1024*1024)) result.Status = IndexFailed return result } - f := related.Main - if opt.Convert && !f.HasJpeg() { if jpegFile, err := ind.convert.ToJpeg(f); err != nil { result.Err = fmt.Errorf("index: creating jpeg failed (%s)", err.Error()) @@ -72,6 +72,7 @@ func IndexMain(related *RelatedFiles, ind *Index, opt IndexOptions) (result Inde // IndexMain indexes a group of related files and returns the result. func IndexRelated(related RelatedFiles, ind *Index, opt IndexOptions) (result IndexResult) { done := make(map[string]bool) + sizeLimit := ind.conf.OriginalsLimit() result = IndexMain(&related, ind, opt) @@ -86,13 +87,24 @@ func IndexRelated(related RelatedFiles, ind *Index, opt IndexOptions) (result In done[related.Main.FileName()] = true for _, f := range related.Files { + if f == nil { + continue + } + if done[f.FileName()] { continue } - res := ind.MediaFile(f, opt, "") done[f.FileName()] = true + // Enforce file size limit for originals. + if sizeLimit > 0 && f.FileSize() > sizeLimit { + log.Warnf("index: %s exceeds file size limit (%d / %d MB)", txt.Quote(f.BaseName()), f.FileSize()/(1024*1024), sizeLimit/(1024*1024)) + continue + } + + res := ind.MediaFile(f, opt, "") + if res.Indexed() && f.IsJpeg() { if err := f.ResampleDefault(ind.thumbPath(), false); err != nil { log.Errorf("index: could not create default thumbnails (%s)", err.Error())