Skip to content

Commit c114309

Browse files
committed
附件管理
1 parent ab9aaf3 commit c114309

File tree

7 files changed

+40
-54
lines changed

7 files changed

+40
-54
lines changed

change.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
- [x] 支持音频和视频播放管理
3737
- [x] 管理后台,恢复和优化附件管理功能
3838
- [ ] 如果管理员或者用户是书籍项目所有人,则音视频链接支持直链播放,否则对音视频链接进行一定的防盗链处理
39+
- [ ] 内容阅读页面音频视频播放功能
3940
- [x] 增加和升级API,使小程序和APP支持音频和视频播放,以及图片放大预览
4041
- [x] 优化`html2json`仓库,解析`HTML`内容,使小程序支持音频和视频播放功能,以及图片放大预览功能
4142
- [ ] 检测和隐藏相关功能

commands/command.go

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424

2525
var (
2626
ConfigurationFile = "./conf/app.conf"
27-
WorkingDirectory = "./"
2827
LogFile = "./logs"
2928
)
3029

@@ -226,49 +225,40 @@ func RegisterFunction() {
226225

227226
func ResolveCommand(args []string) {
228227
flagSet := flag.NewFlagSet("MinDoc command: ", flag.ExitOnError)
229-
flagSet.StringVar(&ConfigurationFile, "config", "", "MinDoc configuration file.")
230-
flagSet.StringVar(&WorkingDirectory, "dir", "", "MinDoc working directory.")
231-
flagSet.StringVar(&LogFile, "log", "", "MinDoc log file path.")
228+
flagSet.StringVar(&ConfigurationFile, "config", "", "BookStack configuration file.")
229+
flagSet.StringVar(&LogFile, "log", "", "BookStack log file path.")
232230

233231
flagSet.Parse(args)
234232

235-
if WorkingDirectory == "" {
236-
if p, err := filepath.Abs(os.Args[0]); err == nil {
237-
WorkingDirectory = filepath.Dir(p)
238-
}
239-
}
240-
if LogFile == "" {
241-
LogFile = filepath.Join(WorkingDirectory, "logs")
242-
}
243233
if ConfigurationFile == "" {
244-
ConfigurationFile = filepath.Join(WorkingDirectory, "conf", "app.conf")
245-
config := filepath.Join(WorkingDirectory, "conf", "app.conf.example")
234+
ConfigurationFile = filepath.Join("conf", "app.conf")
235+
config := filepath.Join("conf", "app.conf.example")
246236
if !utils.FileExists(ConfigurationFile) && utils.FileExists(config) {
247237
utils.CopyFile(ConfigurationFile, config)
248238
}
249239
}
250-
gocaptcha.ReadFonts(filepath.Join(WorkingDirectory, "static", "fonts"), ".ttf")
240+
gocaptcha.ReadFonts(filepath.Join("static", "fonts"), ".ttf")
251241

252242
err := beego.LoadAppConfig("ini", ConfigurationFile)
253243

254244
if err != nil {
255245
log.Println("An error occurred:", err)
256246
os.Exit(1)
257247
}
258-
uploads := filepath.Join(WorkingDirectory, "uploads")
248+
uploads := filepath.Join("uploads")
259249

260250
os.MkdirAll(uploads, 0666)
261251

262-
beego.BConfig.WebConfig.StaticDir["/static"] = filepath.Join(WorkingDirectory, "static")
252+
beego.BConfig.WebConfig.StaticDir["/static"] = filepath.Join("static")
263253
beego.BConfig.WebConfig.StaticDir["/uploads"] = uploads
264-
beego.BConfig.WebConfig.ViewsPath = filepath.Join(WorkingDirectory, "views")
254+
beego.BConfig.WebConfig.ViewsPath = filepath.Join("views")
265255

266-
fonts := filepath.Join(WorkingDirectory, "static", "fonts")
256+
fonts := filepath.Join("static", "fonts")
267257

268258
if !utils.FileExists(fonts) {
269259
log.Fatal("Font path not exist.")
270260
}
271-
gocaptcha.ReadFonts(filepath.Join(WorkingDirectory, "static", "fonts"), ".ttf")
261+
gocaptcha.ReadFonts(filepath.Join("static", "fonts"), ".ttf")
272262

273263
RegisterDataBase()
274264
RegisterModel()
@@ -280,8 +270,4 @@ func init() {
280270

281271
gocaptcha.ReadFonts("./static/fonts", ".ttf")
282272
gob.Register(models.Member{})
283-
284-
if p, err := filepath.Abs(os.Args[0]); err == nil {
285-
WorkingDirectory = filepath.Dir(p)
286-
}
287273
}

commands/daemon/daemon.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ type Daemon struct {
2020
func NewDaemon() *Daemon {
2121

2222
config := &service.Config{
23-
Name: "BookStackd", //服务显示名称
24-
DisplayName: "BookStack Service", //服务名称
25-
Description: "A document online management program.", //服务描述
26-
WorkingDirectory: commands.WorkingDirectory,
27-
Arguments: os.Args[1:],
23+
Name: "BookStackd", //服务显示名称
24+
DisplayName: "BookStack Service", //服务名称
25+
Description: "A document online management program.", //服务描述
26+
Arguments: os.Args[1:],
2827
}
2928

3029
return &Daemon{

controllers/BookController.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"github.com/TruthHun/BookStack/models/store"
2020
"github.com/russross/blackfriday"
2121

22-
"github.com/TruthHun/BookStack/commands"
2322
"github.com/TruthHun/BookStack/conf"
2423
"github.com/TruthHun/BookStack/models"
2524
"github.com/TruthHun/BookStack/utils"
@@ -414,7 +413,7 @@ func (this *BookController) UploadCover() {
414413
this.JsonResult(500, "图片剪切")
415414
}
416415

417-
filePath = filepath.Join(commands.WorkingDirectory, "uploads", time.Now().Format("200601"), fileName+ext)
416+
filePath = filepath.Join("uploads", time.Now().Format("200601"), fileName+ext)
418417

419418
//生成缩略图并保存到磁盘
420419
err = graphics.ImageResizeSaveFile(subImg, 175, 230, filePath)
@@ -423,7 +422,7 @@ func (this *BookController) UploadCover() {
423422
this.JsonResult(500, "保存图片失败")
424423
}
425424

426-
url := "/" + strings.Replace(strings.TrimPrefix(filePath, commands.WorkingDirectory), "\\", "/", -1)
425+
url := "/" + strings.Replace(filePath, "\\", "/", -1)
427426
if strings.HasPrefix(url, "//") {
428427
url = string(url[1:])
429428
}

controllers/DocumentController.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"fmt"
2222

2323
"github.com/PuerkitoBio/goquery"
24-
"github.com/TruthHun/BookStack/commands"
2524
"github.com/TruthHun/BookStack/conf"
2625
"github.com/TruthHun/BookStack/models"
2726
"github.com/TruthHun/BookStack/models/store"
@@ -632,10 +631,12 @@ func (this *DocumentController) Upload() {
632631
}
633632

634633
name := "editormd-file-file"
635-
if fileType != "" && !strings.Contains(fileType, "/") {
636-
name = "editormd-" + fileType + "-file"
637-
} else {
638-
fileType = strings.Split(fileType, "/")[0]
634+
if docId == 0 {
635+
if fileType != "" && !strings.Contains(fileType, "/") {
636+
name = "editormd-" + fileType + "-file"
637+
} else {
638+
fileType = strings.Split(fileType, "/")[0]
639+
}
639640
}
640641

641642
file, moreFile, err := this.GetFile(name)
@@ -725,19 +726,21 @@ func (this *DocumentController) Upload() {
725726
attachment.FileName = moreFile.Filename
726727
attachment.CreateAt = this.Member.MemberId
727728
attachment.FileExt = ext
728-
attachment.FilePath = strings.TrimPrefix(filePath, commands.WorkingDirectory)
729+
attachment.FilePath = filePath
729730
attachment.DocumentId = docId
730731

732+
// 非附件
733+
if name != "editormd-file-file" {
734+
attachment.DocumentId = 0
735+
}
736+
731737
if fileInfo, err := os.Stat(filePath); err == nil {
732738
attachment.FileSize = float64(fileInfo.Size())
733739
}
734-
if docId > 0 {
735-
attachment.DocumentId = docId
736-
}
737740

738741
if strings.EqualFold(ext, ".jpg") || strings.EqualFold(ext, ".jpeg") || strings.EqualFold(ext, ".png") || strings.EqualFold(ext, ".gif") {
739742

740-
attachment.HttpPath = "/" + strings.Replace(strings.TrimPrefix(filePath, commands.WorkingDirectory), "\\", "/", -1)
743+
attachment.HttpPath = "/" + strings.Replace(filePath, "\\", "/", -1)
741744
if strings.HasPrefix(attachment.HttpPath, "//") {
742745
attachment.HttpPath = string(attachment.HttpPath[1:])
743746
}
@@ -783,7 +786,7 @@ func (this *DocumentController) Upload() {
783786
beego.Error(err.Error())
784787
}
785788
attachment.HttpPath = "/" + osspath
786-
attachment.FilePath = filepath.Join(commands.WorkingDirectory, osspath)
789+
attachment.FilePath = attachment.HttpPath
787790
} else {
788791
if err := store.ModelStoreLocal.MoveToStore(filePath, osspath); err != nil {
789792
beego.Error(err.Error())
@@ -796,7 +799,7 @@ func (this *DocumentController) Upload() {
796799
}
797800
}
798801

799-
attachment.HttpPath = "/" + attachment.FilePath
802+
attachment.HttpPath = "/" + strings.TrimLeft(attachment.FilePath, "./")
800803

801804
result := map[string]interface{}{
802805
"errcode": 0,
@@ -859,7 +862,7 @@ func (this *DocumentController) DownloadAttachment() {
859862
if attachment.BookId != bookId {
860863
this.Abort("404")
861864
}
862-
this.Ctx.Output.Download(filepath.Join(commands.WorkingDirectory, attachment.FilePath), attachment.FileName)
865+
this.Ctx.Output.Download(strings.TrimLeft(attachment.FilePath, "./"), attachment.FileName)
863866

864867
this.StopRun()
865868
}
@@ -899,7 +902,7 @@ func (this *DocumentController) RemoveAttachment() {
899902
this.JsonResult(6005, "删除失败")
900903
}
901904

902-
os.Remove(filepath.Join(commands.WorkingDirectory, attach.FilePath))
905+
os.Remove(strings.TrimLeft(attach.FilePath, "./"))
903906
this.JsonResult(0, "ok", attach)
904907
}
905908

controllers/ManagerController.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919

2020
"os"
2121

22-
"github.com/TruthHun/BookStack/commands"
2322
"github.com/TruthHun/BookStack/conf"
2423
"github.com/TruthHun/BookStack/models"
2524
"github.com/TruthHun/BookStack/models/store"
@@ -776,7 +775,6 @@ func (this *ManagerController) AttachDetailed() {
776775
this.Abort("404")
777776
}
778777

779-
attach.FilePath = filepath.Join(commands.WorkingDirectory, attach.FilePath)
780778
attach.HttpPath = this.BaseUrl() + attach.HttpPath
781779
attach.IsExist = utils.FileExists(attach.FilePath)
782780
this.Data["Model"] = attach

controllers/SettingController.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212

1313
"fmt"
1414

15-
"github.com/TruthHun/BookStack/commands"
1615
"github.com/TruthHun/BookStack/conf"
1716
"github.com/TruthHun/BookStack/models"
1817
"github.com/TruthHun/BookStack/utils"
@@ -256,7 +255,7 @@ func (this *SettingController) Upload() {
256255

257256
fileName := strconv.FormatInt(time.Now().UnixNano(), 16)
258257

259-
filePath := filepath.Join(commands.WorkingDirectory, "uploads", time.Now().Format("2006/01"), fileName+ext)
258+
filePath := filepath.Join("uploads", time.Now().Format("2006/01"), fileName+ext)
260259

261260
path := filepath.Dir(filePath)
262261

@@ -278,7 +277,7 @@ func (this *SettingController) Upload() {
278277
}
279278
os.Remove(filePath)
280279

281-
filePath = filepath.Join(commands.WorkingDirectory, "uploads", time.Now().Format("200601"), fileName+ext)
280+
filePath = filepath.Join("uploads", time.Now().Format("200601"), fileName+ext)
282281

283282
err = graphics.ImageResizeSaveFile(subImg, 120, 120, filePath)
284283
err = graphics.SaveImage(filePath, subImg)
@@ -288,7 +287,7 @@ func (this *SettingController) Upload() {
288287
this.JsonResult(500, "保存文件失败")
289288
}
290289

291-
url := "/" + strings.Replace(strings.TrimPrefix(filePath, commands.WorkingDirectory), "\\", "/", -1)
290+
url := "/" + strings.Replace(filePath, "\\", "/", -1)
292291
if strings.HasPrefix(url, "//") {
293292
url = string(url[1:])
294293
}
@@ -300,8 +299,9 @@ func (this *SettingController) Upload() {
300299
if err != nil {
301300
this.JsonResult(60001, "保存头像失败")
302301
}
303-
if strings.HasPrefix(avatar, "/uploads/") {
304-
os.Remove(filepath.Join(commands.WorkingDirectory, avatar))
302+
avatar = strings.TrimLeft(avatar, "./")
303+
if strings.HasPrefix(avatar, "uploads/") {
304+
os.Remove(avatar)
305305
}
306306
this.SetMember(*member)
307307
}

0 commit comments

Comments
 (0)