2021-01-24 17:46:18 +01:00
package commands
import (
"context"
"time"
2021-10-02 14:24:44 +02:00
"github.com/dustin/go-humanize/english"
2021-09-22 19:33:41 +02:00
"github.com/urfave/cli"
2021-01-24 17:46:18 +01:00
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/photoprism"
"github.com/photoprism/photoprism/internal/service"
)
// CleanUpCommand registers the cleanup command.
var CleanUpCommand = cli . Command {
Name : "cleanup" ,
2021-02-06 16:30:30 +01:00
Usage : "Removes orphan index entries and thumbnails" ,
2021-01-24 17:46:18 +01:00
Flags : cleanUpFlags ,
Action : cleanUpAction ,
}
var cleanUpFlags = [ ] cli . Flag {
cli . BoolFlag {
Name : "dry" ,
Usage : "dry run, don't actually remove anything" ,
} ,
}
2021-02-06 16:30:30 +01:00
// cleanUpAction removes orphan index entries and thumbnails.
2021-01-24 17:46:18 +01:00
func cleanUpAction ( ctx * cli . Context ) error {
start := time . Now ( )
conf := config . NewConfig ( ctx )
service . SetConfig ( conf )
_ , cancel := context . WithCancel ( context . Background ( ) )
defer cancel ( )
if err := conf . Init ( ) ; err != nil {
return err
}
conf . InitDb ( )
if conf . ReadOnly ( ) {
log . Infof ( "cleanup: read-only mode enabled" )
}
2021-08-13 20:04:59 +02:00
w := service . CleanUp ( )
2021-01-24 17:46:18 +01:00
opt := photoprism . CleanUpOptions {
Dry : ctx . Bool ( "dry" ) ,
}
2021-08-13 20:04:59 +02:00
if thumbs , orphans , err := w . Start ( opt ) ; err != nil {
2021-01-24 17:46:18 +01:00
return err
} else {
2021-10-02 15:34:41 +02:00
log . Infof ( "removed %s and %s in %s" , english . Plural ( orphans , "index entry" , "index entries" ) , english . Plural ( thumbs , "thumbnail" , "thumbnails" ) , time . Since ( start ) )
2021-01-24 17:46:18 +01:00
}
conf . Shutdown ( )
return nil
}