relative image url/fix conflict when upload attachment again

This commit is contained in:
lealife
2016-12-24 15:16:23 +08:00
parent f49624d3eb
commit a8dd578624
24 changed files with 130 additions and 106 deletions

View File

@ -8,8 +8,9 @@ import (
"gopkg.in/mgo.v2/bson"
"os"
"os/exec"
"strings"
// "strings"
"time"
"regexp"
// "github.com/leanote/leanote/app/types"
// "io/ioutil"
// "fmt"
@ -158,11 +159,31 @@ func (c ApiNote) fixPostNotecontent(noteOrContent *info.ApiNote) {
if noteOrContent.Content == "" {
return
}
files := noteOrContent.Files
if files != nil && len(files) > 0 {
for _, file := range files {
if file.LocalFileId != "" {
noteOrContent.Content = strings.Replace(noteOrContent.Content, "fileId="+file.LocalFileId, "fileId="+file.FileId, -1)
LogJ(file)
if !file.IsAttach {
reg, _ := regexp.Compile(`"https*://[^/]*?/api/file/getImage\?fileId=`+file.LocalFileId)
// Log(reg)
noteOrContent.Content = reg.ReplaceAllString(noteOrContent.Content, `"/api/file/getImage?fileId=`+file.FileId)
// // "http://a.com/api/file/getImage?fileId=localId" => /api/file/getImage?fileId=serverId
// noteOrContent.Content = strings.Replace(noteOrContent.Content,
// baseUrl + "/api/file/getImage?fileId="+file.LocalFileId,
// "/api/file/getImage?fileId="+file.FileId, -1)
} else {
reg, _ := regexp.Compile(`"https*://[^/]*?/api/file/getAttach\?fileId=`+file.LocalFileId)
Log(reg)
noteOrContent.Content = reg.ReplaceAllString(noteOrContent.Content, `"/api/file/getAttach?fileId=`+file.FileId)
/*
noteOrContent.Content = strings.Replace(noteOrContent.Content,
baseUrl + "/api/file/getAttach?fileId="+file.LocalFileId,
"/api/file/getAttach?fileId="+file.FileId, -1)
*/
}
}
}
}
@ -352,6 +373,7 @@ func (c ApiNote) UpdateNote(noteOrContent info.ApiNote) revel.Result {
Log("conflict")
return c.RenderJson(re)
}
Log("没有冲突")
// 如果传了files
// TODO 测试

View File

@ -119,6 +119,7 @@ func (this *AttachService) DeleteAllAttachs(noteId, userId string) bool {
}
// delete attach
// 删除附件为什么要incrNoteUsn ? 因为可能没有内容要修改的
func (this *AttachService) DeleteAttach(attachId, userId string) (bool, string) {
attach := info.Attach{}
db.Get(db.Attachs, attachId, &attach)

View File

@ -415,9 +415,15 @@ func (this *NoteService) UpdateNote(updatedUserId, noteId string, needUpdate bso
}
}
/*
// 这里不再判断, 因为controller已经判断了, 删除附件会新增, 所以不用判断
if usn > 0 && note.Usn != usn {
Log("有冲突!!")
Log(note.Usn)
Log(usn)
return false, "conflict", 0
}
*/
// 是否已自定义
if note.IsBlog && note.HasSelfDefined {
@ -1017,6 +1023,8 @@ func (this *NoteService) FixContentBad(content string, isMarkdown bool) string {
return content
}
// 得到笔记的内容, 此时将笔记内的链接转成标准的Leanote Url
// 将笔记的图片, 附件链接转换成 site.url/file/getImage?fileId=xxx, site.url/file/getAttach?fileId=xxxx
// 性能更好, 5倍的差距
func (this *NoteService) FixContent(content string, isMarkdown bool) string {
baseUrl := configService.GetSiteUrl()
@ -1029,12 +1037,19 @@ func (this *NoteService) FixContent(content string, isMarkdown bool) string {
} else {
baseUrlPattern = strings.Replace(baseUrl, "http://", "https*://", 1)
}
baseUrlPattern = "(?:" + baseUrlPattern + ")*"
Log(baseUrlPattern)
patterns := []map[string]string{
map[string]string{"src": "src", "middle": "/api/file/getImage", "param": "fileId", "to": "getImage?fileId="},
map[string]string{"src": "src", "middle": "/file/outputImage", "param": "fileId", "to": "getImage?fileId="},
map[string]string{"src": "href", "middle": "/attach/download", "param": "attachId", "to": "getAttach?fileId="},
map[string]string{"src": "href", "middle": "/api/file/getAtach", "param": "fileId", "to": "getAttach?fileId="},
// 该链接已失效, 不再支持
map[string]string{"src": "href", "middle": "/attach/downloadAll", "param": "noteId", "to": "getAllAttachs?noteId="},
// map[string]string{"src": "href", "middle": "/attach/downloadAll", "param": "noteId", "to": "getAllAttachs?noteId="},
}
for _, eachPattern := range patterns {
@ -1056,6 +1071,8 @@ func (this *NoteService) FixContent(content string, isMarkdown bool) string {
reg2, _ = regexp.Compile("<a(?:[^>]+?)(" + eachPattern["src"] + `=['"]*` + baseUrlPattern + eachPattern["middle"] + `\?` + eachPattern["param"] + `=([a-z0-9A-Z]{24})["']*)[^>]*>`)
}
Log(reg2)
content = reg.ReplaceAllStringFunc(content, func(str string) string {
// str=这样的
// <img src="http://localhost:9000/file/outputImage?fileId=563d706e99c37b48e0000001" alt="" data-mce-src="http://localhost:9000/file/outputImage?fileId=563d706e99c37b48e0000002">

View File

@ -0,0 +1,32 @@
package tests
import (
"github.com/leanote/leanote/app/db"
"github.com/revel/revel"
"testing"
// . "github.com/leanote/leanote/app/lea"
"github.com/leanote/leanote/app/service"
// "regexp"
// "gopkg.in/mgo.v2"
// "fmt"
// "strings"
)
// 可在server端调试
func init() {
revel.Init("dev", "github.com/leanote/leanote", "/Users/life/Documents/Go/package_base/src")
db.Init("mongodb://localhost:27017/leanote", "leanote")
service.InitService()
service.ConfigS.InitGlobalConfigs()
}
func TestApiFixNoteContent2(t *testing.T) {
note2 := service.NoteS.GetNote("585df83771c1b17e8a000000", "585df81199c37b6176000004")
note := service.NoteS.GetNoteContent("585df83771c1b17e8a000000", "585df81199c37b6176000004")
contentFixed := service.NoteS.FixContent(note.Content, false)
t.Log(note2.Title)
t.Log(note.Content)
t.Log(contentFixed)
}

26
app/tests/reg_test.go Normal file
View File

@ -0,0 +1,26 @@
package tests
import (
// "github.com/leanote/leanote/app/db"
"testing"
// . "github.com/leanote/leanote/app/lea"
// "github.com/leanote/leanote/app/service"
// "gopkg.in/mgo.v2"
// "fmt"
"regexp"
)
// 测试登录
func TestReg(t *testing.T) {
a := `<a href="http://localhost:9000/api/file/getAttach?fileId=585e0e9c270a35609300000c" target="_blank" data-mce-href="http://localhost:9000/api/file/getAttach?fileId=585e0e9c270a35609300000c">proxy.go</a>`
reg, _ := regexp.Compile(`"https*://[^/]*?/api/file/getAttach\?fileId=585e0e9c270a35609300000c`)
a2 := reg.ReplaceAllString(a, `"`)
t.Log(a2)
}

View File

@ -161,6 +161,6 @@ var UrlPrefix = '{{.siteUrl}}';
<script src="/public/album/js/main.js"></script>
-->
<script src="/public/album/js/main.all.js"></script>
<script src="/public/album/js/main.all.js?i=2"></script>
</html>