2023-08-03 18:48:21 +03:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2023-09-17 17:55:13 +03:00
|
|
|
|
2023-08-03 18:48:21 +03:00
|
|
|
"github.com/usememos/memos/store"
|
2023-10-05 18:11:29 +03:00
|
|
|
"github.com/usememos/memos/store/db/sqlite"
|
2023-08-03 18:48:21 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
mvrssCmdFlagFrom = "from"
|
|
|
|
mvrssCmdFlagTo = "to"
|
|
|
|
mvrssCmd = &cobra.Command{
|
|
|
|
Use: "mvrss", // `mvrss` is a shortened for 'means move resource'
|
|
|
|
Short: "Move resource between storage",
|
|
|
|
Run: func(cmd *cobra.Command, _ []string) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
from, err := cmd.Flags().GetString(mvrssCmdFlagFrom)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("failed to get from storage, error: %+v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
to, err := cmd.Flags().GetString(mvrssCmdFlagTo)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("failed to get to storage, error: %+v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if from != "local" || to != "db" {
|
|
|
|
fmt.Printf("only local=>db be supported currently\n")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-05 18:11:29 +03:00
|
|
|
driver, err := sqlite.NewDB(profile)
|
2023-09-27 06:56:20 +03:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("failed to create db driver, error: %+v\n", err)
|
2023-08-03 18:48:21 +03:00
|
|
|
return
|
|
|
|
}
|
2023-09-27 06:56:20 +03:00
|
|
|
if err := driver.Migrate(ctx); err != nil {
|
2023-08-26 02:33:45 +03:00
|
|
|
fmt.Printf("failed to migrate db, error: %+v\n", err)
|
|
|
|
return
|
|
|
|
}
|
2023-08-03 18:48:21 +03:00
|
|
|
|
2023-09-27 04:27:31 +03:00
|
|
|
s := store.New(driver, profile)
|
2023-08-03 18:48:21 +03:00
|
|
|
resources, err := s.ListResources(ctx, &store.FindResource{})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("failed to list resources, error: %+v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var emptyString string
|
|
|
|
for _, res := range resources {
|
|
|
|
if res.InternalPath == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := os.ReadFile(res.InternalPath)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Resource %5d failed to read file: %s\n", res.ID, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(buf) != int(res.Size) {
|
|
|
|
fmt.Printf("Resource %5d size of file %d != %d\n", res.ID, len(buf), res.Size)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
update := store.UpdateResource{
|
|
|
|
ID: res.ID,
|
|
|
|
Blob: buf,
|
|
|
|
InternalPath: &emptyString,
|
|
|
|
}
|
|
|
|
_, err = s.UpdateResource(ctx, &update)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Resource %5d failed to update: %s\n", res.ID, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Resource %5d copy %12d bytes from %s\n", res.ID, len(buf), res.InternalPath)
|
|
|
|
}
|
2023-09-17 17:55:13 +03:00
|
|
|
println("done")
|
2023-08-03 18:48:21 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
mvrssCmd.Flags().String(mvrssCmdFlagFrom, "local", "From storage")
|
|
|
|
mvrssCmd.Flags().String(mvrssCmdFlagTo, "db", "To Storage")
|
|
|
|
|
|
|
|
rootCmd.AddCommand(mvrssCmd)
|
|
|
|
}
|