143 lines
3.1 KiB
Go
143 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
|
|
"gorm.io/driver/mysql"
|
|
"gorm.io/gorm"
|
|
"sketchlib.bing89.com/backend/api"
|
|
"sketchlib.bing89.com/backend/utils/reader"
|
|
)
|
|
|
|
type sketchFile struct {
|
|
Dir string
|
|
Name string
|
|
Path string
|
|
}
|
|
|
|
func getFiles(input, parent string) ([]sketchFile, error) {
|
|
res := []sketchFile{}
|
|
dirs, err := os.ReadDir(input)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
for _, d := range dirs {
|
|
if d.IsDir() {
|
|
p := d.Name()
|
|
if parent != "" {
|
|
p = path.Join(parent, d.Name())
|
|
}
|
|
r, err := getFiles(path.Join(input, d.Name()), p)
|
|
if err == nil {
|
|
res = append(res, r...)
|
|
}
|
|
} else {
|
|
if fileExt := strings.ToLower(path.Ext(d.Name())); fileExt == ".sketch" {
|
|
f := sketchFile{Dir: parent, Name: d.Name(), Path: path.Join(input, d.Name())}
|
|
res = append(res, f)
|
|
}
|
|
}
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
func copy(src, dest string) (int64, error) {
|
|
state, err := os.Stat(src)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
srcf, err := os.Open(src)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
defer srcf.Close()
|
|
destf, err := os.Create(dest)
|
|
if err != nil {
|
|
if os.IsExist(err) {
|
|
return state.Size(), nil
|
|
}
|
|
return 0, err
|
|
}
|
|
defer destf.Close()
|
|
return io.Copy(destf, srcf)
|
|
}
|
|
|
|
func process(db *gorm.DB, output string, files ...sketchFile) ([]sketchFile, error) {
|
|
res := []sketchFile{}
|
|
sketch := reader.Sketch{}
|
|
for _, file := range files {
|
|
log.Println("process file", file.Name, " path:", file.Path, " dir:", file.Dir)
|
|
destDir := path.Join(output, file.Dir)
|
|
if _, err := os.Stat(destDir); err != nil {
|
|
err := os.MkdirAll(destDir, os.ModePerm)
|
|
if err != nil {
|
|
res = append(res, file)
|
|
log.Println("create dir failed:", destDir, err)
|
|
continue
|
|
}
|
|
}
|
|
dest := path.Join(destDir, file.Name)
|
|
n, err := copy(file.Path, dest)
|
|
if err != nil {
|
|
log.Println("copyfile failed:", err)
|
|
res = append(res, file)
|
|
continue
|
|
}
|
|
doc := api.Doc{
|
|
Name: file.Name,
|
|
File: path.Join(file.Dir, file.Name),
|
|
Preview: path.Join(file.Dir, file.Name) + ".png",
|
|
Permission: -1,
|
|
Size: n,
|
|
Labels: "sketch",
|
|
State: 1,
|
|
}
|
|
err = db.Create(&doc).Error
|
|
if err != nil {
|
|
log.Println("insert data failed:", err)
|
|
res = append(res, file)
|
|
continue
|
|
}
|
|
err = sketch.SavePreview(dest)
|
|
if err != nil {
|
|
log.Println("save preview failed:", err)
|
|
res = append(res, file)
|
|
}
|
|
}
|
|
|
|
return res, nil
|
|
}
|
|
|
|
func main() {
|
|
var uri, input, output string
|
|
var init bool
|
|
flag.BoolVar(&init, "init", false, "init database")
|
|
flag.StringVar(&uri, "uri", "root:Hello2022@tcp(192.168.0.134:3306)/sketchlib?charset=utf8mb4&parseTime=True&loc=Local", "db uri")
|
|
flag.StringVar(&input, "input", "", "scketch file")
|
|
flag.StringVar(&output, "output", "", "output to")
|
|
flag.Parse()
|
|
db, err := gorm.Open(mysql.Open(uri), &gorm.Config{})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
files, err := getFiles(input, "/")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
if output == "" {
|
|
log.Fatal("output not set")
|
|
}
|
|
fs, err := process(db, output, files...)
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
for _, f := range fs {
|
|
log.Println("file process failed:", f)
|
|
}
|
|
}
|