copy shared note must copy images

This commit is contained in:
life
2014-09-20 15:20:36 +08:00
parent 9f89a3c717
commit 81c2254cfb
4 changed files with 62 additions and 17 deletions

View File

@ -110,7 +110,7 @@ func (this *FileService) UpdateImage(userId, fileId, title string) bool {
// 要判断是否具有权限 // 要判断是否具有权限
// userId是否具有fileId的访问权限 // userId是否具有fileId的访问权限
func (this *FileService) GetFile(userId, fileId string) string { func (this *FileService) GetFile(userId, fileId string) string {
if userId == "" || fileId == "" { if fileId == "" {
return "" return ""
} }
@ -124,7 +124,7 @@ func (this *FileService) GetFile(userId, fileId string) string {
// 1. 判断权限 // 1. 判断权限
// 是否是我的文件 // 是否是我的文件
if file.UserId.Hex() == userId { if userId != "" && file.UserId.Hex() == userId {
return path return path
} }

View File

@ -39,8 +39,6 @@ func (this *NoteImageService) UpdateNoteImages(userId, noteId, content string) b
// 删除旧的 // 删除旧的
db.DeleteAll(db.NoteImages, bson.M{"NoteId": bson.ObjectIdHex(noteId)}) db.DeleteAll(db.NoteImages, bson.M{"NoteId": bson.ObjectIdHex(noteId)})
Log("--------ii--")
// 添加新的 // 添加新的
var fileId string var fileId string
noteImage := info.NoteImage{NoteId: bson.ObjectIdHex(noteId)} noteImage := info.NoteImage{NoteId: bson.ObjectIdHex(noteId)}
@ -54,8 +52,6 @@ func (this *NoteImageService) UpdateNoteImages(userId, noteId, content string) b
Log(fileId) Log(fileId)
// 判断是否是我的文件 // 判断是否是我的文件
if fileService.IsMyFile(userId, fileId) { if fileService.IsMyFile(userId, fileId) {
Log("?????")
Log("<><><>")
noteImage.ImageId = bson.ObjectIdHex(fileId) noteImage.ImageId = bson.ObjectIdHex(fileId)
db.Insert(db.NoteImages, noteImage) db.Insert(db.NoteImages, noteImage)
} }
@ -66,4 +62,41 @@ func (this *NoteImageService) UpdateNoteImages(userId, noteId, content string) b
} }
return true return true
} }
// 复制图片, 把note的图片都copy给我, 且修改noteContent图片路径
func (this *NoteImageService) CopyNoteImages(fromNoteId, fromUserId, newNoteId, content, toUserId string) string {
// 得到fromNoteId的noteImages, 如果为空, 则直接返回content
noteImages := []info.NoteImage{}
db.ListByQWithFields(db.NoteImages, bson.M{"NoteId": bson.ObjectIdHex(fromNoteId)}, []string{"ImageId"}, &noteImages)
if len(noteImages) == 0 {
return content;
}
// <img src="/file/outputImage?fileId=12323232" />
// 把fileId=1232替换成新的
replaceMap := map[string]string{}
for _, noteImage := range noteImages {
imageId := noteImage.ImageId.Hex()
ok, newImageId := fileService.CopyImage(fromUserId, imageId, toUserId)
if ok {
replaceMap[imageId] = newImageId
}
}
if len(replaceMap) > 0 {
// 替换之
reg, _ := regexp.Compile("outputImage\\?fileId=([a-z0-9A-Z]{24})")
content = reg.ReplaceAllStringFunc(content, func(each string) string {
// each=outputImage?fileId=541bd2f599c37b4f3r000003
fileId := each[len(each)-24:] // 得到后24位, 也即id
if replaceFileId, ok := replaceMap[fileId]; ok {
return "outputImage?fileId=" + replaceFileId
}
return each
});
}
return content;
}

View File

@ -338,9 +338,12 @@ func (this *NoteService) CopyNote(noteId, notebookId, userId string) info.Note {
} }
// 复制别人的共享笔记给我 // 复制别人的共享笔记给我
// TODO 判断是否共享了给我 // 将别人可用的图片转为我的图片, 复制图片
func (this *NoteService) CopySharedNote(noteId, notebookId, fromUserId, myUserId string) info.Note { func (this *NoteService) CopySharedNote(noteId, notebookId, fromUserId, myUserId string) info.Note {
if notebookService.IsMyNotebook(notebookId, myUserId) { Log(shareService.HasSharedNote(noteId, myUserId) || shareService.HasSharedNotebook(noteId, myUserId, fromUserId))
// 判断是否共享了给我
if notebookService.IsMyNotebook(notebookId, myUserId) &&
(shareService.HasSharedNote(noteId, myUserId) || shareService.HasSharedNotebook(noteId, myUserId, fromUserId)) {
note := this.GetNote(noteId, fromUserId) note := this.GetNote(noteId, fromUserId)
if note.NoteId == "" { if note.NoteId == "" {
return info.Note{} return info.Note{}
@ -354,10 +357,15 @@ func (this *NoteService) CopySharedNote(noteId, notebookId, fromUserId, myUserId
note.IsTop = false note.IsTop = false
note.IsBlog = false // 别人的可能是blog note.IsBlog = false // 别人的可能是blog
note.ImgSrc = "" // 为什么清空, 因为图片需要复制, 先清空
// content // content
noteContent.NoteId = note.NoteId noteContent.NoteId = note.NoteId
noteContent.UserId = note.UserId noteContent.UserId = note.UserId
// 复制图片, 把note的图片都copy给我, 且修改noteContent图片路径
noteContent.Content = noteImageService.CopyNoteImages(noteId, fromUserId, note.NoteId.Hex(), noteContent.Content, myUserId)
// 添加之 // 添加之
note = this.AddNoteAndContent(note, noteContent, note.UserId); note = this.AddNoteAndContent(note, noteContent, note.UserId);
@ -375,8 +383,10 @@ func (this *NoteService) CopySharedNote(noteId, notebookId, fromUserId, myUserId
// shareService call // shareService call
// [ok] // [ok]
func (this *NoteService) GetNotebookId(noteId string) bson.ObjectId { func (this *NoteService) GetNotebookId(noteId string) bson.ObjectId {
note := &info.Note{} note := info.Note{}
db.Get(db.Notes, noteId, note) // db.Get(db.Notes, noteId, &note)
// LogJ(note)
db.GetByQWithFields(db.Notes, bson.M{"_id": bson.ObjectIdHex(noteId)}, []string{"NotebookId"}, &note)
return note.NotebookId return note.NotebookId
} }

View File

@ -3,7 +3,7 @@ package service
import ( import (
"github.com/leanote/leanote/app/info" "github.com/leanote/leanote/app/info"
"github.com/leanote/leanote/app/db" "github.com/leanote/leanote/app/db"
// . "github.com/leanote/leanote/app/lea" . "github.com/leanote/leanote/app/lea"
"gopkg.in/mgo.v2/bson" "gopkg.in/mgo.v2/bson"
"time" "time"
"sort" "sort"
@ -338,10 +338,12 @@ func (this *ShareService) HasSharedNote(noteId, myUserId string) bool {
return db.Has(db.ShareNotes, bson.M{"ToUserId": bson.ObjectIdHex(myUserId), "NoteId": bson.ObjectIdHex(noteId)}) return db.Has(db.ShareNotes, bson.M{"ToUserId": bson.ObjectIdHex(myUserId), "NoteId": bson.ObjectIdHex(noteId)})
} }
// noteId的notebook是否共享了给我 // noteId的notebook是否共享了给我
func (this *ShareService) hasSharedNotebook(noteId, myUserId, sharedUserId string) bool { func (this *ShareService) HasSharedNotebook(noteId, myUserId, sharedUserId string) bool {
note := noteService.GetNote(noteId, sharedUserId) notebookId := noteService.GetNotebookId(noteId)
if note.NoteId != "" { Log(noteId)
return db.Has(db.ShareNotebooks, bson.M{"NotebookId": note.NotebookId, Log(notebookId)
if notebookId != "" {
return db.Has(db.ShareNotebooks, bson.M{"NotebookId": notebookId,
"UserId": bson.ObjectIdHex(sharedUserId), "UserId": bson.ObjectIdHex(sharedUserId),
"ToUserId": bson.ObjectIdHex(myUserId), "ToUserId": bson.ObjectIdHex(myUserId),
}) })
@ -355,7 +357,7 @@ func (this *ShareService) GetShareNoteContent(noteId, myUserId, sharedUserId str
noteContent = info.NoteContent{} noteContent = info.NoteContent{}
// 是否单独共享了该notebook // 是否单独共享了该notebook
// 或者, 其notebook共享了我 // 或者, 其notebook共享了我
if this.HasSharedNote(noteId, myUserId) || this.hasSharedNotebook(noteId, myUserId, sharedUserId) { if this.HasSharedNote(noteId, myUserId) || this.HasSharedNotebook(noteId, myUserId, sharedUserId) {
db.Get(db.NoteContents, noteId, &noteContent) db.Get(db.NoteContents, noteId, &noteContent)
} else { } else {
} }