mirror of
https://github.com/go-vikunja/vikunja.git
synced 2026-04-29 11:00:07 -05:00
feat: add repair-file-mime-types CLI command
Add a CLI command that finds all files in the database with no MIME type set, detects it from the stored file content, and updates the database. This is useful for backfilling MIME types on files created before MIME detection was added on upload.
This commit is contained in:
81
pkg/cmd/repair_file_mime_types.go
Normal file
81
pkg/cmd/repair_file_mime_types.go
Normal file
@@ -0,0 +1,81 @@
|
||||
// Vikunja is a to-do list application to facilitate your life.
|
||||
// Copyright 2018-present Vikunja and contributors. All rights reserved.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"code.vikunja.io/api/pkg/db"
|
||||
"code.vikunja.io/api/pkg/files"
|
||||
"code.vikunja.io/api/pkg/initialize"
|
||||
"code.vikunja.io/api/pkg/log"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(repairFileMimeTypesCmd)
|
||||
}
|
||||
|
||||
var repairFileMimeTypesCmd = &cobra.Command{
|
||||
Use: "repair-file-mime-types",
|
||||
Short: "Detect and set MIME types for all files that have none",
|
||||
Long: `Scans all files in the database that have no MIME type set,
|
||||
detects the type from the stored file content, and updates the database.
|
||||
|
||||
This is useful after upgrading from a version that did not store MIME types
|
||||
on file creation. Only files with an empty or NULL mime column are affected.`,
|
||||
PreRun: func(_ *cobra.Command, _ []string) {
|
||||
initialize.FullInitWithoutAsync()
|
||||
},
|
||||
Run: func(_ *cobra.Command, _ []string) {
|
||||
s := db.NewSession()
|
||||
defer s.Close()
|
||||
|
||||
if err := s.Begin(); err != nil {
|
||||
log.Errorf("Failed to start transaction: %s", err)
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
_ = s.Rollback()
|
||||
}()
|
||||
|
||||
result, err := files.RepairFileMimeTypes(s)
|
||||
if err != nil {
|
||||
log.Errorf("Failed to repair file MIME types: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := s.Commit(); err != nil {
|
||||
log.Errorf("Failed to commit changes: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Infof("Repair complete:")
|
||||
log.Infof(" Files scanned: %d", result.Total)
|
||||
log.Infof(" Files updated: %d", result.Updated)
|
||||
|
||||
if len(result.Errors) > 0 {
|
||||
log.Errorf("Errors encountered (%d):", len(result.Errors))
|
||||
for _, e := range result.Errors {
|
||||
log.Errorf(" - %s", e)
|
||||
}
|
||||
}
|
||||
|
||||
if result.Total == 0 {
|
||||
log.Infof("No files with missing MIME types found - all files are healthy!")
|
||||
}
|
||||
},
|
||||
}
|
||||
92
pkg/files/repair.go
Normal file
92
pkg/files/repair.go
Normal file
@@ -0,0 +1,92 @@
|
||||
// Vikunja is a to-do list application to facilitate your life.
|
||||
// Copyright 2018-present Vikunja and contributors. All rights reserved.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package files
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"code.vikunja.io/api/pkg/log"
|
||||
|
||||
"github.com/gabriel-vasile/mimetype"
|
||||
"github.com/schollz/progressbar/v3"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
// RepairMimeTypesResult holds the summary of a MIME type repair run.
|
||||
type RepairMimeTypesResult struct {
|
||||
Total int
|
||||
Updated int
|
||||
Errors []string
|
||||
}
|
||||
|
||||
// RepairFileMimeTypes finds all files with no MIME type set, detects it from
|
||||
// the stored file content, and updates the database.
|
||||
func RepairFileMimeTypes(s *xorm.Session) (*RepairMimeTypesResult, error) {
|
||||
var files []*File
|
||||
err := s.Where("mime = '' OR mime IS NULL").Find(&files)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to query files with missing mime type: %w", err)
|
||||
}
|
||||
|
||||
result := &RepairMimeTypesResult{
|
||||
Total: len(files),
|
||||
}
|
||||
|
||||
if len(files) == 0 {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
bar := progressbar.Default(int64(len(files)), "Detecting MIME types")
|
||||
|
||||
for _, f := range files {
|
||||
file, err := afs.Open(f.getAbsoluteFilePath())
|
||||
if err != nil {
|
||||
msg := fmt.Sprintf("file %d: failed to open: %s", f.ID, err)
|
||||
log.Errorf("file %d: failed to open: %s", f.ID, err)
|
||||
result.Errors = append(result.Errors, msg)
|
||||
_ = bar.Add(1)
|
||||
continue
|
||||
}
|
||||
|
||||
mime, err := mimetype.DetectReader(file)
|
||||
_ = file.Close()
|
||||
if err != nil {
|
||||
msg := fmt.Sprintf("file %d: failed to detect mime type: %s", f.ID, err)
|
||||
log.Errorf("file %d: failed to detect mime type: %s", f.ID, err)
|
||||
result.Errors = append(result.Errors, msg)
|
||||
_ = bar.Add(1)
|
||||
continue
|
||||
}
|
||||
|
||||
f.Mime = mime.String()
|
||||
_, err = s.ID(f.ID).Cols("mime").Update(f)
|
||||
if err != nil {
|
||||
msg := fmt.Sprintf("file %d: failed to update mime type: %s", f.ID, err)
|
||||
log.Errorf("file %d: failed to update mime type: %s", f.ID, err)
|
||||
result.Errors = append(result.Errors, msg)
|
||||
_ = bar.Add(1)
|
||||
continue
|
||||
}
|
||||
|
||||
result.Updated++
|
||||
_ = bar.Add(1)
|
||||
}
|
||||
|
||||
_ = bar.Finish()
|
||||
|
||||
return result, nil
|
||||
}
|
||||
Reference in New Issue
Block a user