From a5c81c8e2e5eae1dc4352b14263ea470e7b60973 Mon Sep 17 00:00:00 2001 From: Graham Steffaniak <42989099+gtsteffaniak@users.noreply.github.com> Date: Thu, 30 Apr 2026 13:55:14 -0300 Subject: [PATCH] Beta/v1.3.8 (#2363) --- CHANGELOG.md | 11 ++ backend/auth/totp.go | 7 +- backend/cmd/user.go | 10 +- backend/common/settings/structs.go | 2 +- backend/database/dbindex/indexing.go | 5 + backend/database/sql/index.go | 169 +++++++++--------- backend/http/preview.go | 2 +- backend/indexing/getDirInfoCore_test.go | 54 +++--- backend/indexing/indexingFiles.go | 120 +++++-------- backend/indexing/indexingScanner.go | 12 +- backend/indexing/indexingSchedule.go | 7 +- backend/indexing/iteminfo/fileinfo.go | 2 + backend/indexing/iteminfo/mediaMeta.go | 3 + backend/indexing/iteminfo/mediaMeta_test.go | 69 +++++++ backend/indexing/mutate.go | 28 ++- backend/swagger/docs/docs.go | 2 +- backend/swagger/docs/swagger.json | 2 +- backend/swagger/docs/swagger.yaml | 4 +- frontend/public/config.generated.yaml | 1 + frontend/src/api/resources.js | 13 +- frontend/src/components/ContextMenu.vue | 10 +- frontend/src/components/files/Breadcrumbs.vue | 4 +- frontend/src/components/files/ListingItem.vue | 8 +- frontend/src/components/prompts/Upload.vue | 7 +- frontend/src/components/prompts/UserEdit.vue | 23 +-- frontend/src/i18n/ar.json | 1 - frontend/src/i18n/cz.json | 1 - frontend/src/i18n/de.json | 1 - frontend/src/i18n/el.json | 1 - frontend/src/i18n/en.json | 1 - frontend/src/i18n/es.json | 1 - frontend/src/i18n/fr.json | 1 - frontend/src/i18n/he.json | 1 - frontend/src/i18n/hu.json | 1 - frontend/src/i18n/it.json | 1 - frontend/src/i18n/ja.json | 1 - frontend/src/i18n/ko.json | 1 - frontend/src/i18n/nl-be.json | 1 - frontend/src/i18n/nl.json | 1 - frontend/src/i18n/pl.json | 1 - frontend/src/i18n/pt-br.json | 1 - frontend/src/i18n/pt.json | 1 - frontend/src/i18n/ro.json | 1 - frontend/src/i18n/ru.json | 1 - frontend/src/i18n/sk.json | 1 - frontend/src/i18n/sv-se.json | 1 - frontend/src/i18n/tr.json | 1 - frontend/src/i18n/ua.json | 1 - frontend/src/i18n/zh-cn.json | 1 - frontend/src/i18n/zh-tw.json | 1 - frontend/src/utils/upload.js | 48 +++++ frontend/src/views/files/ListingView.vue | 8 +- 52 files changed, 379 insertions(+), 277 deletions(-) create mode 100644 backend/indexing/iteminfo/mediaMeta_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 02cae60b41..320ab53661 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,17 @@ All notable changes to this project will be documented in this file. For commit guidelines, please refer to [Standard Version](https://github.com/conventional-changelog/standard-version). +## v1.3.8 + + **BugFixes**: + - Quick download icon style after icon change. + - Missing error popup for resource creatoin actions (upload/create) + - EnforcedOtp login failure until restart (#2330) + - Thumbnails for Folders only display sporadically (#2353) + - Unwanted user scope change for users with non-default scopes (#2347) + - Fix sidebar source info totals (#2321) (#2322) (#982) + - Error uploading a large number of photos -- only 100 items get uploaded (#2348) + ## v1.3.7 **Notes**: diff --git a/backend/auth/totp.go b/backend/auth/totp.go index 7ea298017b..6e28db2c08 100644 --- a/backend/auth/totp.go +++ b/backend/auth/totp.go @@ -146,14 +146,14 @@ func decryptSecret(b64Ciphertext, b64Nonce string) (string, error) { } func VerifyTotpCode(user *users.User, code string, userStore *users.Storage) error { - // get data from cache cachedSecret, found := TotpCache.Get(user.Username) if !found && user.TOTPSecret == "" { return fmt.Errorf("OTP token not found in cache, please generate a new one") } + useCache := found && user.TOTPSecret == "" totpSecret := user.TOTPSecret // The encrypted or plaintext secret totpNonce := user.TOTPNonce // The nonce if encrypted, or empty if plaintext - if found { + if useCache { splitSecret := strings.Split(cachedSecret, "||") if len(splitSecret) < 2 { return fmt.Errorf("invalid cached OTP token format") @@ -173,9 +173,7 @@ func VerifyTotpCode(user *users.User, code string, userStore *users.Storage) err } secretToValidate = decryptedSecret } - // --- END: ADD THIS DECRYPTION LOGIC --- - // Validate the token using the (now plaintext) secret. valid, err := totp.ValidateCustom(code, secretToValidate, time.Now().UTC(), totp.ValidateOpts{ Period: TOTPPeriod, Skew: TOTPSkew, @@ -198,6 +196,7 @@ func VerifyTotpCode(user *users.User, code string, userStore *users.Storage) err logger.Error("error updating user with OTP token:", err) return fmt.Errorf("error updating user with OTP token: %w", err) } + TotpCache.Delete(user.Username) } else { return fmt.Errorf("opt secret is empty, cannot enable TOTP") } diff --git a/backend/cmd/user.go b/backend/cmd/user.go index d67ac61c96..95fc2cafee 100644 --- a/backend/cmd/user.go +++ b/backend/cmd/user.go @@ -46,8 +46,7 @@ func validateUserInfo(newDB bool) { } adminUser := settings.Config.Auth.AdminUsername adminPass := settings.Config.Auth.AdminPassword - passwordEnabled := settings.Config.Auth.Methods.PasswordAuth.Enabled - if user.Username == adminUser && adminPass != "" && passwordEnabled { + if user.Username == adminUser && adminPass != "" && user.LoginMethod == users.LoginMethodPassword { logger.Info("Resetting admin user to default username and password.") user.Permissions.Admin = true user.Password = settings.Config.Auth.AdminPassword @@ -63,7 +62,11 @@ func validateUserInfo(newDB bool) { logger.Fatalf("Unable to create automatic backup of database due to error: %v", err) } } - err := store.Users.Save(user, changePass, true) + fields := []string{"Scopes", "SidebarLinks", "Tokens", "Permissions", "Preview", "ShowFirstLogin", "LoginMethod", "Version"} + if changePass { + fields = append(fields, "Password") + } + err := store.Users.Update(user, true, fields...) if err != nil { logger.Errorf("could not update user: %v", err) } @@ -94,7 +97,6 @@ func updateUserScopes(user *users.User) bool { } else { continue } - newScopes = append(newScopes, users.SourceScope{ Name: src.Path, Scope: existingScope.Scope, diff --git a/backend/common/settings/structs.go b/backend/common/settings/structs.go index 98bac67e4a..864f13d9e5 100644 --- a/backend/common/settings/structs.go +++ b/backend/common/settings/structs.go @@ -210,7 +210,7 @@ type SourceConfig struct { Rules []ConditionalRule `json:"rules"` // list of item rules to apply to specific paths DefaultUserScope string `json:"defaultUserScope"` // defaults to root of index "/" should match folders under path DefaultEnabled bool `json:"defaultEnabled"` // should be added as a default source for new users? - CreateUserDir bool `json:"createUserDir"` // deprecated: create a user directory for each user under defaultUserScope + username (always true now) + CreateUserDir bool `json:"createUserDir"` // create a user directory for each user under defaultUserScope + username UseLogicalSize bool `json:"useLogicalSize"` // calculate sizes based on logical size instead of disk utilization (du -sh), folders will be 0 bytes when empty. // hidden but used internally - optimized map lookups for conditional rules ResolvedRules ResolvedRulesConfig `json:"-"` diff --git a/backend/database/dbindex/indexing.go b/backend/database/dbindex/indexing.go index dfe34b9cf8..c164a91cd5 100644 --- a/backend/database/dbindex/indexing.go +++ b/backend/database/dbindex/indexing.go @@ -17,6 +17,11 @@ type IndexInfo struct { NumDirs uint64 `json:"numDirs"` NumFiles uint64 `json:"numFiles"` + // Disk usage (cached from periodic GetIndexInfo / partition probes; aligns with ReducedIndex Stats) + UsedAsIndexed uint64 `json:"used"` + UsedDisk uint64 `json:"usedAlt"` + DiskTotal uint64 `json:"total"` + // Scanner information - map of scanner path to scanner stats Scanners map[string]*PersistedScannerInfo `json:"scanners"` } diff --git a/backend/database/sql/index.go b/backend/database/sql/index.go index 4bccde1681..26dd629f6c 100644 --- a/backend/database/sql/index.go +++ b/backend/database/sql/index.go @@ -298,28 +298,70 @@ func (db *IndexDB) InsertItem(source, path string, info *iteminfo.FileInfo) erro return err } +// dbTransientOpAttempts is how many times to run a bulk DB op that may hit SQLITE_BUSY before giving up. +const dbTransientOpAttempts = 2 + +const dbTransientRetryBackoff = 50 * time.Millisecond + // BulkInsertItems inserts multiple items in a single transaction for a specific source. // Database errors (busy/locked) are treated as soft failures - the filesystem is the source of truth. // Returns nil on success or soft failure (busy/locked), error only on hard failures. +// Transient busy/locked errors retry once before giving up (see dbTransientOpAttempts). func (db *IndexDB) BulkInsertItems(source string, items []*iteminfo.FileInfo) error { + if len(items) == 0 { + return nil + } startTime := time.Now() + for attempt := 1; attempt <= dbTransientOpAttempts; attempt++ { + ok, transient, err := db.attemptBulkInsert(source, items) + if err != nil { + return err + } + if ok { + if attempt > 1 { + logger.Infof("[DB_TX] BulkInsertItems: succeeded on retry (%d items, attempt %d, %v)", + len(items), attempt, time.Since(startTime)) + } + return nil + } + if transient && attempt < dbTransientOpAttempts { + logger.Warningf("[DB_TX] BulkInsertItems: transient DB error (attempt %d/%d, %d items), retrying after %v", + attempt, dbTransientOpAttempts, len(items), dbTransientRetryBackoff) + time.Sleep(dbTransientRetryBackoff) + continue + } + if transient { + logger.Warningf("[DB_TX] BulkInsertItems: still failing after %d attempts (%d items, %v) — batch dropped", + dbTransientOpAttempts, len(items), time.Since(startTime)) + } + return nil + } + return nil // defensive if dbTransientOpAttempts is ever 0 and loop never runs +} +// attemptBulkInsert runs one transactional bulk insert. +// Returns success=true when committed; transient=true when SQLITE_BUSY-like errors allow a retry (mutex released). +func (db *IndexDB) attemptBulkInsert(source string, items []*iteminfo.FileInfo) (success bool, transient bool, err error) { tx, err := db.BeginTransaction() if err != nil { - // Soft failure: DB is busy or locked, skip this update if isBusyError(err) || isTransactionError(err) { - db.mu.Unlock() // Release mutex on error - logger.Debugf("[DB_TX] BulkInsertItems: BeginTransaction failed (DB busy/locked), skipping - took %v", time.Since(startTime)) - return nil // Non-fatal: filesystem will be used as fallback + db.mu.Unlock() + logger.Debugf("[DB_TX] BulkInsertItems: BeginTransaction failed (DB busy/locked): %v", err) + return false, true, nil } - db.mu.Unlock() // Release mutex on error - return err // Hard failure: something is wrong with the DB + db.mu.Unlock() + return false, false, err } - defer db.mu.Unlock() // Ensure mutex is always released - // Always update all fields including last_updated, even if nothing changed - // This ensures last_updated stays current for stale entry detection + committed := false + defer func() { + if !committed { + _ = tx.Rollback() + } + db.mu.Unlock() + }() + stmt, err := tx.Prepare(` INSERT INTO index_items (source, path, parent_path, name, size, mod_time, type, is_dir, is_hidden, has_preview, last_updated) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) @@ -336,14 +378,12 @@ func (db *IndexDB) BulkInsertItems(source string, items []*iteminfo.FileInfo) er `) if err != nil { if isBusyError(err) || isTransactionError(err) { - // With EXCLUSIVE locking and mutex, this shouldn't happen. - // Log as warning to surface potential issues (another process accessing DB, bug, etc.) - logger.Errorf("[DB] BulkInsertItems: Unexpected busy/lock error during Prepare: %v", err) - return nil + logger.Debugf("[DB_TX] BulkInsertItems: Prepare busy/locked: %v", err) + return false, true, nil } - return err + return false, false, err } - defer stmt.Close() // Ensure statement is always closed + defer stmt.Close() nowUnix := time.Now().Unix() for _, info := range items { @@ -363,28 +403,23 @@ func (db *IndexDB) BulkInsertItems(source string, items []*iteminfo.FileInfo) er ) if err != nil { if isBusyError(err) || isTransactionError(err) { - // With EXCLUSIVE locking and mutex, this shouldn't happen. - // Log as warning to surface potential issues (another process accessing DB, bug, etc.) - logger.Errorf("[DB] BulkInsertItems: Unexpected busy/lock error during Exec: %v", err) - return nil + logger.Debugf("[DB_TX] BulkInsertItems: Exec busy/locked during insert: %v", err) + return false, true, nil } logger.Errorf("[DB] BulkInsertItems: Exec failed for path=%s, source=%s, error=%v", info.Path, source, err) - return err + return false, false, err } } - // Try to commit if err := tx.Commit(); err != nil { if isBusyError(err) || isTransactionError(err) { - // With EXCLUSIVE locking and mutex, this shouldn't happen. - // Log as warning to surface potential issues (another process accessing DB, bug, etc.) - logger.Errorf("[DB] BulkInsertItems: Unexpected busy/lock error during Commit: %v", err) - return nil + logger.Debugf("[DB_TX] BulkInsertItems: Commit busy/locked: %v", err) + return false, true, nil } - return err + return false, false, err } - - return nil + committed = true + return true, false, nil } // isBusyError checks if an error is SQLITE_BUSY (error code 5) @@ -474,51 +509,6 @@ func (db *IndexDB) GetItemsByPaths(source string, paths []string) (map[string]*i return result, nil } -// GetHasPreviewBatch retrieves hasPreview status for multiple directory paths in a single query. -// This is optimized for the N+1 query pattern in GetDirInfo where we need hasPreview for all subdirectories. -// Returns map[path]hasPreview. Missing paths in the result indicate they're not in the database yet. -func (db *IndexDB) GetHasPreviewBatch(source string, paths []string) (map[string]bool, error) { - if len(paths) == 0 { - return make(map[string]bool), nil - } - - // Build query with IN clause - only select path and has_preview for efficiency - placeholders := make([]string, len(paths)) - args := make([]interface{}, len(paths)+1) - args[0] = source - for i, path := range paths { - placeholders[i] = "?" - args[i+1] = path - } - - query := fmt.Sprintf(` - SELECT path, has_preview - FROM index_items WHERE source = ? AND path IN (%s) AND is_dir = 1 - `, strings.Join(placeholders, ",")) - - rows, err := db.Query(query, args...) - if err != nil { - // Soft failure: DB is busy or locked, return empty map - if isBusyError(err) || isTransactionError(err) { - return make(map[string]bool), nil - } - return nil, err - } - defer rows.Close() - - result := make(map[string]bool) - for rows.Next() { - var path string - var hasPreview bool - if err := rows.Scan(&path, &hasPreview); err != nil { - return nil, err - } - result[path] = hasPreview - } - - return result, nil -} - // GetDirectoryChildren retrieves all children of a directory for a specific source. func (db *IndexDB) GetDirectoryChildren(source, dirPath string) ([]*iteminfo.FileInfo, error) { query := ` @@ -1077,17 +1067,31 @@ func (db *IndexDB) UpdateFolderSizesIfChanged(source string, pathSizes map[strin args = append(args, path) } - result, err := db.Exec(query, args...) - if err != nil { - if isBusyError(err) || isTransactionError(err) { - logger.Debugf("[DB_TX] UpdateFolderSizesIfChanged: DB busy/locked, skipping update") - return 0, nil // Non-fatal: sizes will be synced on next scan + startTime := time.Now() + for attempt := 1; attempt <= dbTransientOpAttempts; attempt++ { + result, err := db.Exec(query, args...) + if err != nil { + if isBusyError(err) || isTransactionError(err) { + if attempt < dbTransientOpAttempts { + logger.Warningf("[DB_TX] UpdateFolderSizesIfChanged: busy/locked (attempt %d/%d), retrying after %v: %v", + attempt, dbTransientOpAttempts, dbTransientRetryBackoff, err) + time.Sleep(dbTransientRetryBackoff) + continue + } + logger.Warningf("[DB_TX] UpdateFolderSizesIfChanged: still busy after %d attempts (%d paths, %v) — skipped", + dbTransientOpAttempts, len(pathSizes), time.Since(startTime)) + return 0, nil + } + return 0, err } - return 0, err + n, _ := result.RowsAffected() + if attempt > 1 { + logger.Infof("[DB_TX] UpdateFolderSizesIfChanged: succeeded on retry (%d rows, attempt %d, %v)", + n, attempt, time.Since(startTime)) + } + return n, nil } - - rowsAffected, _ := result.RowsAffected() - return rowsAffected, nil + return 0, nil // defensive: unreachable when dbTransientOpAttempts >= 1 } // LoadFolderSizes loads all folder sizes for a source from the database @@ -1320,6 +1324,7 @@ func scanItem(scanner interface{ Scan(...interface{}) error }) (*iteminfo.FileIn return nil, err } info.ModTime = time.Unix(modTime, 0) + info.IsDir = isDir return &info, nil } diff --git a/backend/http/preview.go b/backend/http/preview.go index cc4c76de9a..fc836e05d9 100644 --- a/backend/http/preview.go +++ b/backend/http/preview.go @@ -105,7 +105,7 @@ func getDirectoryPreview(r *http.Request, d *requestContext, frameIndex int) (*i // Build list of previewable item names in stable order (same as d.fileInfo.Files) var previewableNames []string for _, item := range d.fileInfo.Files { - if !item.HasPreview || !iteminfo.ShouldBubbleUpToFolderPreview(item.ItemInfo) { + if !iteminfo.ShouldBubbleUpToFolderPreview(item.ItemInfo) { continue } previewableNames = append(previewableNames, item.Name) diff --git a/backend/indexing/getDirInfoCore_test.go b/backend/indexing/getDirInfoCore_test.go index 7c17adf693..7ec2eada24 100644 --- a/backend/indexing/getDirInfoCore_test.go +++ b/backend/indexing/getDirInfoCore_test.go @@ -16,44 +16,26 @@ func TestFetchExtendedAttributes(t *testing.T) { tests := []struct { name string adjustedPath string - combinedPath string - files []os.FileInfo opts Options - expectHasPreview bool expectMapNotEmpty bool }{ { - name: "SkipExtendedAttrs returns empty", - adjustedPath: "/test/", - combinedPath: "/test/", - files: []os.FileInfo{}, - opts: Options{ - SkipExtendedAttrs: true, - Recursive: false, - }, - expectHasPreview: false, + name: "SkipExtendedAttrs returns empty", + adjustedPath: "/test/", + opts: Options{SkipExtendedAttrs: true, Recursive: false}, expectMapNotEmpty: false, }, { - name: "Recursive returns empty", - adjustedPath: "/test/", - combinedPath: "/test/", - files: []os.FileInfo{}, - opts: Options{ - SkipExtendedAttrs: false, - Recursive: true, - }, - expectHasPreview: false, + name: "Recursive returns empty", + adjustedPath: "/test/", + opts: Options{SkipExtendedAttrs: false, Recursive: true}, expectMapNotEmpty: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - hasPreview, subdirMap := idx.fetchExtendedAttributes(tt.adjustedPath, tt.files, tt.opts) - if hasPreview != tt.expectHasPreview { - t.Errorf("expected hasPreview=%v, got %v", tt.expectHasPreview, hasPreview) - } + subdirMap := idx.fetchExtendedAttributes(tt.adjustedPath, tt.opts) if (len(subdirMap) > 0) != tt.expectMapNotEmpty { t.Errorf("expected mapNotEmpty=%v, got %v", tt.expectMapNotEmpty, len(subdirMap) > 0) } @@ -61,6 +43,28 @@ func TestFetchExtendedAttributes(t *testing.T) { } } +// TestFetchExtendedAttributes_subdirHasPreviewFromMetadata checks folder hasPreview comes from one GetMetadataInfo (non-shallow). +func TestFetchExtendedAttributes_subdirHasPreviewFromMetadata(t *testing.T) { + idx, _, cleanup := setupTestIndex(t) + defer cleanup() + + deepdir, err := idx.db.GetItem("test", "/subdir/deepdir/") + if err != nil || deepdir == nil { + t.Fatalf("deepdir row: err=%v", err) + } + deepdir.HasPreview = true + _ = idx.db.InsertItem("test", "/subdir/deepdir/", deepdir) + + subdirMap := idx.fetchExtendedAttributes("/subdir/", Options{ + SkipExtendedAttrs: false, + Recursive: false, + }) + + if !subdirMap["/subdir/deepdir/"] { + t.Errorf("expected hasPreview map for /subdir/deepdir/, got %v", subdirMap) + } +} + // TestGetDirectoryName tests the directory name extraction logic func TestGetDirectoryName(t *testing.T) { tests := []struct { diff --git a/backend/indexing/indexingFiles.go b/backend/indexing/indexingFiles.go index 37ea56310f..c897f44a0b 100644 --- a/backend/indexing/indexingFiles.go +++ b/backend/indexing/indexingFiles.go @@ -734,50 +734,26 @@ func (idx *Index) GetFsInfoViewableOnly(adjustedPath string, followSymlinks bool }) } -// fetchExtendedAttributes fetches hasPreview for the current directory and batch fetches for subdirectories -func (idx *Index) fetchExtendedAttributes(indexPath string, files []os.FileInfo, opts Options) (bool, map[string]bool) { - hasPreview := false +// fetchExtendedAttributes loads indexed subfolder has_preview for the current directory from GetMetadataInfo (database). +// The listed directory's own HasPreview is derived only in GetDirInfoCore from children (never from a stale parent DB row). +func (idx *Index) fetchExtendedAttributes(indexPath string, opts Options) map[string]bool { subdirHasPreviewMap := make(map[string]bool) if opts.SkipExtendedAttrs || opts.Recursive { - return hasPreview, subdirHasPreviewMap + return subdirHasPreviewMap } - // Fetch hasPreview for current directory - realDirInfo, exists := idx.GetMetadataInfo(indexPath, true, true) - if exists { - hasPreview = realDirInfo.HasPreview + realDirInfo, exists := idx.GetMetadataInfo(indexPath, true, false) + if !exists { + return subdirHasPreviewMap } - // Batch fetch hasPreview for all subdirectories - var subdirPaths []string - for _, file := range files { - if !iteminfo.IsDirectory(file) { - continue - } - baseName := file.Name() - if indexPath == "/" { - if !idx.shouldInclude(baseName) { - continue - } - } - if omitList[baseName] { - continue - } - dirPath := indexPath + baseName - childIndexPath := utils.AddTrailingSlashIfNotExists(dirPath) - subdirPaths = append(subdirPaths, childIndexPath) + for _, folder := range realDirInfo.Folders { + key := utils.AddTrailingSlashIfNotExists(indexPath + folder.Name) + subdirHasPreviewMap[key] = folder.HasPreview } - if len(subdirPaths) > 0 { - var err error - subdirHasPreviewMap, err = idx.db.GetHasPreviewBatch(idx.Name, subdirPaths) - if err != nil { - subdirHasPreviewMap = make(map[string]bool) - } - } - - return hasPreview, subdirHasPreviewMap + return subdirHasPreviewMap } // processDirectoryItem processes a directory item and returns the itemInfo, size, and whether it should be counted @@ -825,11 +801,7 @@ func (idx *Index) processDirectoryItem(file os.FileInfo, indexPath string, subdi if !opts.SkipExtendedAttrs && subdirHasPreviewMap != nil { if hasPreviewValue, exists := subdirHasPreviewMap[childIndexPath]; exists { itemInfo.HasPreview = hasPreviewValue - } else { - itemInfo.HasPreview = false } - } else { - itemInfo.HasPreview = false } return itemInfo, itemInfo.Size, true @@ -875,9 +847,7 @@ func (idx *Index) processFileItem(file os.FileInfo, indexPath string, opts Optio if !usedCachedPreview { setFilePreviewFlags(itemInfo, fullCombined) } - if itemInfo.HasPreview && iteminfo.ShouldBubbleUpToFolderPreview(*itemInfo) { - bubblesUpHasPreview = true - } + bubblesUpHasPreview = iteminfo.ShouldBubbleUpToFolderPreview(*itemInfo) } else { itemInfo.HasPreview = false } @@ -894,8 +864,7 @@ func (idx *Index) GetDirInfoCore(dirInfo *os.File, stat os.FileInfo, indexPath s if err != nil { return nil, err } - // Fetch extended attributes (hasPreview for current dir and subdirs) - hasPreview, subdirHasPreviewMap := idx.fetchExtendedAttributes(indexPath, files, opts) + subdirHasPreviewMap := idx.fetchExtendedAttributes(indexPath, opts) baseName := filepath.Base(indexPath) if indexPath == "/" { @@ -906,6 +875,7 @@ func (idx *Index) GetDirInfoCore(dirInfo *os.File, stat os.FileInfo, indexPath s fileInfos := []iteminfo.ExtendedItemInfo{} dirInfos := []iteminfo.ItemInfo{} processedCount := 0 + hasPreview := false for _, file := range files { subfileBaseName := file.Name() @@ -960,12 +930,12 @@ func (idx *Index) GetDirInfoCore(dirInfo *os.File, stat os.FileInfo, indexPath s } } else { itemInfo, size, shouldCount, bubblesUp := idx.processFileItem(file, indexPath, opts, scanner) - if shouldCount { - totalSize += size - } if bubblesUp { hasPreview = true } + if shouldCount { + totalSize += size + } fileInfos = append(fileInfos, iteminfo.ExtendedItemInfo{ItemInfo: *itemInfo}) if opts.IsRoutineScan { idx.incrementScannerFiles() @@ -1132,6 +1102,7 @@ func (idx *Index) updateFolderSizeAndParents(path string, newSize uint64, previo if parentDir == "" { break // Reached the top } + // Keys in folderSizes match indexing paths (trailing slash for non-root dirs; root stays "/"). if parentDir != "/" { parentDir = utils.AddTrailingSlashIfNotExists(parentDir) } @@ -1188,20 +1159,6 @@ func (idx *Index) getFileSizeForDisplay(file os.FileInfo, realPath string) int64 return getDiskUsage(file, realPath, false) } -// RecursiveUpdateDirSizes is now a wrapper for backwards compatibility -// This maintains API compatibility while using the new optimized approach -func (idx *Index) RecursiveUpdateDirSizes(path string, previousSize uint64) { - currentSize, exists := idx.GetFolderSize(path) - if !exists { - logger.Debugf("[FOLDER_SIZE] Path %s not found in folderSizes map", path) - return - } - - if currentSize != previousSize { - idx.updateFolderSizeAndParents(path, currentSize, previousSize, false) // updateTarget=false, only update parents - } -} - // SyncFolderSizesToDB syncs in-memory folder sizes to the database after a scan completes // Only syncs folders marked as dirty (changed) and processes in batches to avoid large SQL statements func (idx *Index) SyncFolderSizesToDB() error { @@ -1609,14 +1566,13 @@ func (idx *Index) shouldInclude(baseName string) bool { return false } -// Save persists the index and scanner information to the database -func (idx *Index) Save() error { +// writePersistedIndexInfo writes scanners, stats, and disk totals to Bolt (no SSE broadcast). +func (idx *Index) writePersistedIndexInfo() error { if indexingStorage == nil { - return nil // No storage available, skip persistence + return nil } idx.mu.RLock() - // Collect scanner information scanners := make(map[string]*dbindex.PersistedScannerInfo) for path, scanner := range idx.scanners { scanners[path] = &dbindex.PersistedScannerInfo{ @@ -1631,32 +1587,37 @@ func (idx *Index) Save() error { } } - // Get current index stats complexity := idx.getComplexityUnlocked() numDirs := idx.getNumDirsUnlocked() numFiles := idx.getNumFilesUnlocked() + usedAsIndexed := idx.UsedAsIndexed + usedDisk := idx.UsedDisk + diskTotal := idx.DiskTotal idx.mu.RUnlock() - // Create IndexInfo for persistence info := &dbindex.IndexInfo{ - Path: idx.Path, // Use real filesystem path as key - Source: idx.Name, - Complexity: complexity, - NumDirs: numDirs, - NumFiles: numFiles, - Scanners: scanners, + Path: idx.Path, + Source: idx.Name, + Complexity: complexity, + NumDirs: numDirs, + NumFiles: numFiles, + UsedAsIndexed: usedAsIndexed, + UsedDisk: usedDisk, + DiskTotal: diskTotal, + Scanners: scanners, } - err := indexingStorage.Save(info) - if err != nil { + return indexingStorage.Save(info) +} + +// Save persists the index and scanner information to the database and notifies clients via SSE. +func (idx *Index) Save() error { + if err := idx.writePersistedIndexInfo(); err != nil { return err } - - // Send SSE update event after successful save if err := idx.SendSourceUpdateEvent(); err != nil { logger.Errorf("[%s] Failed to send source update event: %v", idx.Name, err) } - return nil } @@ -1684,6 +1645,9 @@ func (idx *Index) Load() error { idx.Complexity = info.Complexity idx.NumDirs = info.NumDirs idx.NumFiles = info.NumFiles + idx.UsedAsIndexed = info.UsedAsIndexed + idx.UsedDisk = info.UsedDisk + idx.DiskTotal = info.DiskTotal // Restore scanner information (will be applied when scanners are created) // Store in a temporary map that setupMultiScanner can use diff --git a/backend/indexing/indexingScanner.go b/backend/indexing/indexingScanner.go index c3f688b2dc..f08499d548 100644 --- a/backend/indexing/indexingScanner.go +++ b/backend/indexing/indexingScanner.go @@ -502,7 +502,6 @@ func (s *Scanner) calculateSleepTime() time.Duration { // updateSchedule adjusts the scanner's schedule based on whether files changed func (s *Scanner) updateSchedule() { s.statsMu.Lock() - defer s.statsMu.Unlock() // Calculate sleep time BEFORE updating schedule (based on current/old schedule) s.nextSleepTime = scanSchedule[s.currentSchedule] + s.smartModifier @@ -539,7 +538,10 @@ func (s *Scanner) updateSchedule() { s.currentSchedule = len(scanSchedule) - 1 } - // Persist index after schedule update (happens after each scan) + s.statsMu.Unlock() + + // Save must NOT run while holding statsMu (see deadlock: GetScannerStatus = idx.mu then + // scanner stats; Save = idx.mu RL while GetIndexInfo may queue idx.mu LW). if err := s.idx.Save(); err != nil { logger.Errorf("Failed to save index after schedule update: %v", err) } @@ -549,8 +551,6 @@ func (s *Scanner) updateSchedule() { // 0: unknown func (s *Scanner) updateComplexity() { s.statsMu.Lock() - defer s.statsMu.Unlock() - s.complexity = calculateComplexity(s.fullScanTime, s.numDirs) // Set smartModifier based on complexity level @@ -559,7 +559,9 @@ func (s *Scanner) updateComplexity() { } else { s.smartModifier = 0 } - // Persist index after complexity update (happens after full scans) + s.statsMu.Unlock() + + // Persist after full scans — release statsMu before Save (same ordering as updateSchedule). if err := s.idx.Save(); err != nil { logger.Errorf("Failed to save index after complexity update: %v", err) } diff --git a/backend/indexing/indexingSchedule.go b/backend/indexing/indexingSchedule.go index 3220e6d380..29ef3b5af9 100644 --- a/backend/indexing/indexingSchedule.go +++ b/backend/indexing/indexingSchedule.go @@ -205,7 +205,9 @@ func (idx *Index) SendSourceUpdateEvent() error { if idx.mock { return nil } - reducedIndex, err := GetIndexInfo(idx.Name, true) + // Avoid force=true: otherwise GetIndexInfo invalidates DiskUsageCache, recomputes, and writePersistedIndexInfo + // nests under Save recursion; false uses cached probe / current idx totals for the broadcast payload. + reducedIndex, err := GetIndexInfo(idx.Name, false) if err != nil { logger.Errorf("[%s] Error getting index info: %v", idx.Name, err) return err @@ -226,7 +228,8 @@ func (idx *Index) SendSourceUpdateEvent() error { // setupMultiScanner creates and starts the multi-scanner system // isNewDb: if true, skip loading persisted complexity values (database is new or recreated) func (idx *Index) setupMultiScanner(isNewDb bool) { - // Load persisted index and scanner information + // Load persisted index-level stats from Bolt (complexity, numDirs/Files, used/usedAlt/total, etc.) + // Scanner-level stats below use a second GetByPath only for the Scanners map. if err := idx.Load(); err != nil { logger.Errorf("Failed to load persisted index data for [%v]: %v", idx.Name, err) } diff --git a/backend/indexing/iteminfo/fileinfo.go b/backend/indexing/iteminfo/fileinfo.go index 3a00406476..4ce8fb813e 100644 --- a/backend/indexing/iteminfo/fileinfo.go +++ b/backend/indexing/iteminfo/fileinfo.go @@ -30,6 +30,8 @@ type FileInfo struct { Files []ExtendedItemInfo `json:"files,omitempty"` // files in the directory with optional metadata Folders []ItemInfo `json:"folders,omitempty"` // folders in the directory Path string `json:"path,omitempty"` // path scoped to the associated index + // IsDir is the index DB is_dir column; Type alone can disagree (legacy or malformed rows). + IsDir bool `json:"-"` } // MediaMetadata contains metadata extracted from audio and video files diff --git a/backend/indexing/iteminfo/mediaMeta.go b/backend/indexing/iteminfo/mediaMeta.go index a67df1bfa2..5f6336fc40 100644 --- a/backend/indexing/iteminfo/mediaMeta.go +++ b/backend/indexing/iteminfo/mediaMeta.go @@ -405,6 +405,9 @@ func IsDirectory(fileInfo os.FileInfo) bool { // Text files, office documents, and PDFs should NOT bubble up to folder previews. // This ensures consistency between indexing and preview generation. func ShouldBubbleUpToFolderPreview(item ItemInfo) bool { + if item.Type == "directory" || !item.HasPreview { + return false + } // Get the simple type (e.g., "image", "video", "text") simpleType := strings.Split(item.Type, "/")[0] // Text files should NOT bubble up diff --git a/backend/indexing/iteminfo/mediaMeta_test.go b/backend/indexing/iteminfo/mediaMeta_test.go new file mode 100644 index 0000000000..e6e554acc3 --- /dev/null +++ b/backend/indexing/iteminfo/mediaMeta_test.go @@ -0,0 +1,69 @@ +package iteminfo + +import "testing" + +func TestShouldBubbleUpToFolderPreview(t *testing.T) { + tests := []struct { + name string + item ItemInfo + want bool + }{ + { + name: "directory type never bubbles", + item: ItemInfo{Name: "sub", Type: "directory"}, + want: false, + }, + { + name: "text markdown does not bubble", + item: ItemInfo{Name: "README.md", Type: "text/markdown"}, + want: false, + }, + { + name: "plain text does not bubble", + item: ItemInfo{Name: "LICENSE", Type: "text/plain; charset=utf-8"}, + want: false, + }, + { + name: "image does not bubble without HasPreview", + item: ItemInfo{Name: "a.png", Type: "image/png", HasPreview: false}, + want: false, + }, + { + name: "image bubbles when HasPreview", + item: ItemInfo{Name: "a.png", Type: "image/png", HasPreview: true}, + want: true, + }, + { + name: "video bubbles when HasPreview", + item: ItemInfo{Name: "clip.mp4", Type: "video/mp4", HasPreview: true}, + want: true, + }, + { + name: "audio bubbles when HasPreview (e.g. album art)", + item: ItemInfo{Name: "track.mp3", Type: "audio/mpeg", HasPreview: true}, + want: true, + }, + { + name: "OnlyOffice-backed extension does not bubble", + item: ItemInfo{Name: "report.docx", Type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"}, + want: false, + }, + { + name: "unknown blob type does not bubble", + item: ItemInfo{Name: "data.bin", Type: "application/octet-stream"}, + want: false, + }, + { + name: "empty type does not bubble", + item: ItemInfo{Name: "weird", Type: ""}, + want: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := ShouldBubbleUpToFolderPreview(tt.item); got != tt.want { + t.Fatalf("ShouldBubbleUpToFolderPreview() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/backend/indexing/mutate.go b/backend/indexing/mutate.go index cd53828c75..ada753bc6a 100644 --- a/backend/indexing/mutate.go +++ b/backend/indexing/mutate.go @@ -12,8 +12,8 @@ import ( "github.com/gtsteffaniak/go-logger/logger" ) -// UpdateFileMetadata updates the FileInfo for the specified directory in the index. -// scanner parameter is optional - if nil (API refresh), directly inserts to database +// UpdateMetadata persists a completed directory listing to the index. The directory row's has_preview +// scanner is optional — if nil (API refresh), inserts run immediately; otherwise items batch until flush. func (idx *Index) UpdateMetadata(info *iteminfo.FileInfo, scanner *Scanner) bool { items := make([]*iteminfo.FileInfo, 0, len(info.Files)+len(info.Folders)+1) dirItem := *info @@ -297,7 +297,8 @@ func GetIndexInfo(sourceName string, forceCacheRefresh bool) (ReducedIndex, erro } // Only update disk total if cache is missing or explicitly forced. - // Indexed "used" bytes for the UI come from the SQLite root row (see below), not in-memory scan state. + // Indexed "used" sums root-level files from SQLite; top-level folders use idx.folderSizes (scanned rollup) + // because directory size columns in SQLite can lag folderSizes/SyncFolderSizesToDB. sourcePath := idx.Path cacheKey := "usageCache-" + sourceName if forceCacheRefresh { @@ -313,11 +314,21 @@ func GetIndexInfo(sourceName string, forceCacheRefresh bool) (ReducedIndex, erro } itemInfo, dbErr := idx.db.GetDirectoryChildren(idx.Name, "/") if dbErr != nil { - logger.Debugf("GetIndexInfo: could not read root folder size from index DB for %s: %v", sourceName, dbErr) + logger.Debugf("GetIndexInfo: could not read root listing from index DB for %s: %v", sourceName, dbErr) } - indexedSizeFromDB := uint64(0) + var indexedSizeFromDB uint64 for _, item := range itemInfo { - indexedSizeFromDB += uint64(item.Size) + isFolder := item.IsDir || item.Type == "directory" + if !isFolder { + indexedSizeFromDB += uint64(item.Size) + continue + } + folderKey := utils.AddTrailingSlashIfNotExists(item.Path) + if agg, ok := idx.GetFolderSize(folderKey); ok { + indexedSizeFromDB += agg + } else { + indexedSizeFromDB += uint64(item.Size) + } } // Also fetch OS-reported partition used space (total - free) partitionUsed, err := fileutils.GetPartitionUsed(sourcePath) @@ -326,6 +337,11 @@ func GetIndexInfo(sourceName string, forceCacheRefresh bool) (ReducedIndex, erro } idx.SetUsage(totalPartitionSize, partitionUsed, indexedSizeFromDB) utils.DiskUsageCache.Set(cacheKey, true) + if indexingStorage != nil { + if err := idx.writePersistedIndexInfo(); err != nil { + logger.Warningf("GetIndexInfo: failed to persist index disk stats for %s: %v", sourceName, err) + } + } } // Build scanner info for client diff --git a/backend/swagger/docs/docs.go b/backend/swagger/docs/docs.go index 230fa39910..73de85928e 100644 --- a/backend/swagger/docs/docs.go +++ b/backend/swagger/docs/docs.go @@ -5775,7 +5775,7 @@ const docTemplate = `{ ] }, "createUserDir": { - "description": "deprecated: create a user directory for each user under defaultUserScope + username (always true now)", + "description": "create a user directory for each user under defaultUserScope + username", "type": "boolean" }, "defaultEnabled": { diff --git a/backend/swagger/docs/swagger.json b/backend/swagger/docs/swagger.json index 7f96ad17b7..7ecb28761c 100644 --- a/backend/swagger/docs/swagger.json +++ b/backend/swagger/docs/swagger.json @@ -5764,7 +5764,7 @@ ] }, "createUserDir": { - "description": "deprecated: create a user directory for each user under defaultUserScope + username (always true now)", + "description": "create a user directory for each user under defaultUserScope + username", "type": "boolean" }, "defaultEnabled": { diff --git a/backend/swagger/docs/swagger.yaml b/backend/swagger/docs/swagger.yaml index ebe1a2135a..6b3e90dd26 100644 --- a/backend/swagger/docs/swagger.yaml +++ b/backend/swagger/docs/swagger.yaml @@ -1177,8 +1177,8 @@ definitions: - $ref: '#/definitions/settings.ConditionalFilter' description: 'deprecated: use source.rules instead' createUserDir: - description: 'deprecated: create a user directory for each user under defaultUserScope - + username (always true now)' + description: create a user directory for each user under defaultUserScope + + username type: boolean defaultEnabled: description: should be added as a default source for new users? diff --git a/frontend/public/config.generated.yaml b/frontend/public/config.generated.yaml index 676d41d47e..342bc83f41 100644 --- a/frontend/public/config.generated.yaml +++ b/frontend/public/config.generated.yaml @@ -43,6 +43,7 @@ server: ignoreSymlinks: false # Excludes symbolic links defaultUserScope: "/" # defaults to root of index "/" should match folders under path defaultEnabled: true # should be added as a default source for new users? + createUserDir: false # create a user directory for each user under defaultUserScope + username useLogicalSize: false # calculate sizes based on logical size instead of disk utilization (du -sh), folders will be 0 bytes when empty. externalUrl: "" # used by share links if set (eg. http://mydomain.com) internalUrl: "" # used by integrations if set, this is the base domain that an integration service will use to communicate with filebrowser (eg. http://localhost:8080) diff --git a/frontend/src/api/resources.js b/frontend/src/api/resources.js index dc3282d7f1..375a545a16 100644 --- a/frontend/src/api/resources.js +++ b/frontend/src/api/resources.js @@ -462,7 +462,18 @@ export function post( error.response = { status: request.status, responseText: request.responseText }; reject(error); } else { - reject(new Error(request.responseText || "Upload failed")); + let errorMessage = "Upload failed"; + try { + const errorData = JSON.parse(request.responseText); + errorMessage = errorData.message || errorMessage; + } catch (e) { + errorMessage = request.responseText || errorMessage; + } + const error = new Error(errorMessage); + error.status = request.status; + error.response = { status: request.status, responseText: request.responseText }; + notify.showError(errorMessage); + reject(error); } }; diff --git a/frontend/src/components/ContextMenu.vue b/frontend/src/components/ContextMenu.vue index b5aa6076fe..d594eb3d6c 100644 --- a/frontend/src/components/ContextMenu.vue +++ b/frontend/src/components/ContextMenu.vue @@ -35,19 +35,19 @@