dev
truthhun 6 months ago
parent dd8dd11043
commit 1dc153e5df

@ -0,0 +1,66 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"moredoc/service"
"strings"
"github.com/spf13/cobra"
)
var (
ext string // 指定的文件后缀png、jpg、webp
documentId int64 // 指定的文档ID, 0表示全部大于0表示指定文档
)
// reconvertCmd represents the reconvert command
var reconvertCmd = &cobra.Command{
Use: "reconvert",
Short: "文档重转",
Long: `pngjpgwebp便
pngjpgwebp
`,
Run: func(cmd *cobra.Command, args []string) {
// ext必须是png、jpg、webp中的一种
ext = strings.ToLower(ext)
if ext != "png" && ext != "jpg" && ext != "webp" {
ext = "webp"
}
// 必须指定documentId
if documentId < 0 {
fmt.Println("\n请用--id指定的文档ID, 0表示全部大于0表示指定文档。如需重转全部文档建议先指定一个文档进行测试查验效果是否符合需求。")
fmt.Println("\n按回车键退出...")
fmt.Scanln()
return
}
// 提示用户输入Y确认否则退出
fmt.Println("\n请确认是否重转文档")
if documentId == 0 {
fmt.Print("转换文档:全部文档")
} else {
fmt.Print("转换文档ID", documentId)
}
fmt.Println(";文档预览格式:", ext)
fmt.Println("按 Y 确认,按其他键取消和退出...")
var confirm string
fmt.Scanln(&confirm)
if confirm != "Y" && confirm != "y" {
fmt.Println("\n已取消重转文档。")
return
}
service.Reconvert(cfg, logger, ext, documentId)
},
}
func init() {
rootCmd.AddCommand(reconvertCmd)
reconvertCmd.Flags().StringVarP(&ext, "ext", "e", "webp", "指定的文档预览格式png、jpg、webp")
reconvertCmd.Flags().Int64VarP(&documentId, "id", "d", -1, "指定的文档ID, 0表示全部大于0表示指定文档。如需重转全部文档建议先指定一个文档进行测试查验效果是否符合需求。")
}

@ -39,13 +39,8 @@ var (
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "moredoc",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Short: "魔豆文库,文库系统解决方案",
Long: `魔豆文库使用Go语言开发的类似百度文库、新浪爱问文库的文库系统解决方案支持 TXT、PDF、EPUB、MOBI、Office 等格式文档的在线预览与管理,为 dochub文库的重构版本。`,
}
// Execute adds all child commands to the root command and sets flags appropriately.

@ -30,8 +30,8 @@ import (
// serveCmd represents the serve command
var serveCmd = &cobra.Command{
Use: "serve",
Short: "start a server",
Long: `start a server`,
Short: "启动服务",
Long: `启动魔豆文库程序服务,提供文档管理与预览。`,
Run: func(cmd *cobra.Command, args []string) {
util.Version = Version
util.Hash = GitHash

@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
@ -24,8 +24,8 @@ import (
// syncdbCmd represents the syncdb command
var syncdbCmd = &cobra.Command{
Use: "syncdb",
Short: "sync database scheme",
Long: `sync database scheme.`,
Short: "同步数据库",
Long: `同步数据库表结构以及初始化数据。`,
Run: func(cmd *cobra.Command, args []string) {
service.SyncDB(cfg, logger)
},

@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
@ -27,11 +27,11 @@ var (
BuildAt = "unknown"
)
//versionCmd represents the version command
// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
Short: "show current version information.",
Long: `show current version information.`,
Short: "查看版本信息",
Long: `查看魔豆文库版本信息如版本号、构建时间、Git提交哈希值等。`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("")
fmt.Println("AppName: ", "moredoc")

@ -2,6 +2,7 @@ package model
import (
"bytes"
"errors"
"fmt"
"moredoc/util/sitemap"
"os"
@ -15,8 +16,13 @@ import (
"gorm.io/gorm"
)
type reconvertDocument struct {
Id int64 `json:"id"`
}
var (
isCreatingSitemap bool
cacheReconvert = "cache/reconvert"
)
// UpdateSitemap 更新站点地图
@ -325,3 +331,63 @@ func (m *DBModel) loopCovertDocument() {
}
}
}
func (m *DBModel) ReconvertDocoument(documentId int64, ext string) {
os.RemoveAll(cacheReconvert)
os.MkdirAll(cacheReconvert, os.ModePerm)
if documentId > 0 {
doc, err := m.GetDocument(documentId)
if err != nil {
m.logger.Error("ReconvertDocoument", zap.Error(err))
return
}
if doc.Status != DocumentStatusConverted {
m.logger.Error("ReconvertDocoument", zap.Error(errors.New("文档不是已转换的文档,不能重转")))
return
}
m.reconvertDocument(&doc, ext)
} else {
m.reconvertAllDocument(ext)
}
}
func (m *DBModel) reconvertDocument(doc *Document, ext string) {
if doc.PreviewExt == ext {
m.logger.Info("reconvertDocument", zap.String("msg", "文档预览文件格式与指定格式一致,无需重转"), zap.String("document", doc.Title+"."+doc.PreviewExt))
return
}
// 1. 下载文档预览文件
attachment := m.GetAttachmentByTypeAndTypeId(AttachmentTypeDocument, doc.Id, "hash")
if attachment.Id == 0 {
m.logger.Error("reconvertDocument", zap.String("msg", "文档预览文件不存在"), zap.String("document", doc.Title+"."+doc.PreviewExt))
return
}
// 2. 转换文档预览文件
// 3. 上传文档预览文件
// 4. 更新数据库表的预览后缀
// 5. 删除缓存文件,删除原预览文件
}
func (m *DBModel) reconvertAllDocument(ext string) {
var (
cfg reconvertDocument
doc Document
)
bytes, _ := os.ReadFile("cache/reconvert.json")
json.Unmarshal(bytes, &cfg)
for {
m.db.Where("id > ?", cfg.Id).Where("status = ?", DocumentStatusConverted).Order("id asc").Find(&doc)
if doc.Id == 0 {
break
}
m.reconvertDocument(&doc, ext)
cfg.Id = doc.Id
bytes, _ = json.Marshal(cfg)
os.WriteFile("cache/reconvert.json", bytes, os.ModePerm)
}
}

@ -0,0 +1,12 @@
package service
import (
"fmt"
"moredoc/conf"
"go.uber.org/zap"
)
func Reconvert(cfg *conf.Config, logger *zap.Logger, ext string, documentId int64) {
fmt.Println("reconvert called")
}
Loading…
Cancel
Save