Skip to content

Commit

Permalink
b3log#32
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Dec 7, 2017
1 parent 5e29a28 commit de5fa65
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
47 changes: 39 additions & 8 deletions controller/console/exportctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
package console

import (
"github.com/gin-gonic/gin"
"github.com/b3log/pipe/service"
"github.com/b3log/pipe/util"
"github.com/gin-gonic/gin"
"io/ioutil"
"net/http"
"github.com/b3log/pipe/service"
"os"
"path/filepath"
)
Expand All @@ -37,19 +38,49 @@ func ExportMarkdownAction(c *gin.Context) {
return
}

mdFiles:=service.Export.ExportMarkdowns(session.BID)

tempDir := os.TempDir()
logger.Trace("temp dir path is [" + tempDir + "]")
zipFilePath := filepath.Join(tempDir, session.UName+"-export-md.zip")
zipFile, err := os.Create(zipFilePath)
zipFile, err := util.Zip.Create(zipFilePath)
if nil != err {
logger.Errorf("create temp file [" + zipFilePath + "] failed: " + err.Error())
logger.Errorf("create zip file [" + zipFilePath + "] failed: " + err.Error())
result.Code = -1
result.Msg = "create temp file failed"
result.Msg = "create zip file failed"

return
}
util.Zip.Create()
defer zipFile.Close()

c.Header("Content-Disposition", "attachment; filename="+session.UName+"-export-md.zip")
c.Header("Content-Type", "application/zip")
http.ServeFile(c.Writer, c.Request, zipFilePath)

mdFiles := service.Export.ExportMarkdowns(session.BID)
if 1 > len(mdFiles) {
return
}

zipPath := filepath.Join(tempDir, session.UName+"-export-md")
if err = os.RemoveAll(zipPath); nil != err {
logger.Errorf("remove temp dir [" + zipPath + "] failed: " + err.Error())
result.Code = -1
result.Msg = "remove temp dir failed"

return
}
if err = os.Mkdir(zipPath, 0755); nil != err {
logger.Errorf("make temp dir [" + zipPath + "] failed: " + err.Error())
result.Code = -1
result.Msg = "make temp dir failed"

return
}
for _, mdFile := range mdFiles {
filename := filepath.Join(zipPath, mdFile.Name+".md")
if err := ioutil.WriteFile(filename, []byte(mdFile.Content), 0644); nil != err {
logger.Errorf("write file [" + filename + "] failed: " + err.Error())
}
}

zipFile.AddDirectory(session.UName+"-export-md", zipPath)
}
2 changes: 1 addition & 1 deletion controller/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func MapRoutes() *gin.Engine {
consoleGroup.GET("/thumbs", console.GetArticleThumbsAction)
consoleGroup.POST("/markdown", console.MarkdownAction)
consoleGroup.POST("/import/md", console.ImportMarkdownAction)
consoleGroup.POST("/export/md", console.ExportMarkdownAction)
consoleGroup.GET("/export/md", console.ExportMarkdownAction)
// consoleGroup.POST("/blogs/switch/:id", console.BlogSwitchAction)

consoleSettingsGroup := consoleGroup.Group("/settings")
Expand Down
9 changes: 6 additions & 3 deletions service/exportsrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
package service

import (
"regexp"

"github.com/b3log/pipe/model"
"github.com/kennygrant/sanitize"
)

var Export = &exportService{}
Expand All @@ -39,7 +40,7 @@ func (srv *exportService) ExportMarkdowns(blogID uint) (ret []*MarkdownFile) {

for _, article := range articles {
mdFile := &MarkdownFile{
Name: sanitize.Name(article.Title),
Name: sanitizeFilename(article.Title),
Content: article.Content,
}

Expand All @@ -49,6 +50,8 @@ func (srv *exportService) ExportMarkdowns(blogID uint) (ret []*MarkdownFile) {
return ret
}

func sanitizeFilename(filename string) string {
func sanitizeFilename(unsanitized string) string {
unsanitized = regexp.MustCompile("[\\?\\\\/:|<>\\*]").ReplaceAllString(unsanitized, " ") // filter out ? \ / : | < > *

return regexp.MustCompile("\\s+").ReplaceAllString(unsanitized, "_") // white space as underscores
}

0 comments on commit de5fa65

Please sign in to comment.