|
| 1 | +/** |
| 2 | +
|
| 3 | + Plik upload server |
| 4 | +
|
| 5 | +The MIT License (MIT) |
| 6 | +
|
| 7 | +Copyright (c) <2015> |
| 8 | + - Mathieu Bodjikian <[email protected]> |
| 9 | + - Charles-Antoine Mathieu <[email protected]> |
| 10 | +
|
| 11 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | +of this software and associated documentation files (the "Software"), to deal |
| 13 | +in the Software without restriction, including without limitation the rights |
| 14 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | +copies of the Software, and to permit persons to whom the Software is |
| 16 | +furnished to do so, subject to the following conditions: |
| 17 | +
|
| 18 | +The above copyright notice and this permission notice shall be included in |
| 19 | +all copies or substantial portions of the Software. |
| 20 | +
|
| 21 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | +THE SOFTWARE. |
| 28 | +**/ |
| 29 | + |
| 30 | +package main |
| 31 | + |
| 32 | +import ( |
| 33 | + "flag" |
| 34 | + "fmt" |
| 35 | + "io/ioutil" |
| 36 | + "os" |
| 37 | + |
| 38 | + "github.com/root-gg/plik/server/Godeps/_workspace/src/github.com/root-gg/juliet" |
| 39 | + "github.com/root-gg/plik/server/metadataBackend/bolt" |
| 40 | + "github.com/root-gg/plik/server/metadataBackend/file" |
| 41 | +) |
| 42 | + |
| 43 | +// This script migrate upload metadata from file backend to Bolt backend |
| 44 | +// |
| 45 | +// go run file2bolt.go --directory ../files --db ../plik.db |
| 46 | +// [02/01/2016 22:00:48][INFO][bolt.go:164 Create] Upload metadata successfully saved |
| 47 | +// [02/01/2016 22:00:48][INFO][bolt.go:164 Create] Upload metadata successfully saved |
| 48 | +// 2 upload imported |
| 49 | +// |
| 50 | +// Some .config "no such file or directory" errors are normal if you already switched to Bolt metadata backend |
| 51 | +// while using the file data backend as it will create upload directories but not .config files. |
| 52 | + |
| 53 | +func main() { |
| 54 | + // Parse command line arguments |
| 55 | + var directoryPath = flag.String("directory", "../files", "File metadatabackend base path") |
| 56 | + var dbPath = flag.String("db", "../plik.db", "Bold db path") |
| 57 | + flag.Parse() |
| 58 | + |
| 59 | + if *directoryPath == "" || *dbPath == "" { |
| 60 | + fmt.Println("usage : file2bolt --directory path --db path") |
| 61 | + os.Exit(1) |
| 62 | + } |
| 63 | + |
| 64 | + // Initialize File metadata backend |
| 65 | + fileConfig := map[string]interface{}{"Directory": *directoryPath} |
| 66 | + fmb := file.NewFileMetadataBackend(fileConfig) |
| 67 | + |
| 68 | + // Initialize Bolt metadata backend |
| 69 | + boltConfig := map[string]interface{}{"Path": *dbPath} |
| 70 | + bmb := bolt.NewBoltMetadataBackend(boltConfig) |
| 71 | + |
| 72 | + counter := 0 |
| 73 | + |
| 74 | + // upload ids are the name of the second level of directories of the file metadata backend |
| 75 | + dirs1, err := ioutil.ReadDir(*directoryPath) |
| 76 | + if err != nil { |
| 77 | + fmt.Printf("Unable to open directory %s : %s\n", *directoryPath, err) |
| 78 | + os.Exit(1) |
| 79 | + } |
| 80 | + for _, dir1 := range dirs1 { |
| 81 | + if dir1.IsDir() { |
| 82 | + path := *directoryPath + "/" + dir1.Name() |
| 83 | + dirs2, err := ioutil.ReadDir(path) |
| 84 | + if err != nil { |
| 85 | + fmt.Printf("Unable to open directory %s : %s\n", path, err) |
| 86 | + os.Exit(1) |
| 87 | + } |
| 88 | + for _, dir2 := range dirs2 { |
| 89 | + if dir2.IsDir() { |
| 90 | + uploadID := dir2.Name() |
| 91 | + |
| 92 | + // Load upload from file metadata backend |
| 93 | + upload, err := fmb.Get(juliet.NewContext(), uploadID) |
| 94 | + if err != nil { |
| 95 | + fmt.Printf("Unable to load upload %s : %s\n", uploadID, err) |
| 96 | + continue |
| 97 | + } |
| 98 | + |
| 99 | + // Save upload to bolt metadata backend |
| 100 | + err = bmb.Create(juliet.NewContext(), upload) |
| 101 | + if err != nil { |
| 102 | + fmt.Printf("Unable to save upload %s : %s\n", uploadID, err) |
| 103 | + continue |
| 104 | + } |
| 105 | + |
| 106 | + counter++ |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + fmt.Printf("%d upload imported\n", counter) |
| 113 | +} |
0 commit comments