Beispiel #1
0
// delete image
func (this *FileService) DeleteImage(userId, fileId string) (bool, string) {
	file := info.File{}
	db.GetByIdAndUserId(db.Files, fileId, userId, &file)

	if file.FileId != "" {
		if db.DeleteByIdAndUserId(db.Files, fileId, userId) {
			// delete image
			// TODO
			file.Path = strings.TrimLeft(file.Path, "/")
			var err error
			if strings.HasPrefix(file.Path, "upload") {
				Log(file.Path)
				err = os.Remove(revel.BasePath + "/public/" + file.Path)
			} else {
				err = os.Remove(revel.BasePath + "/" + file.Path)
			}
			if err == nil {
				return true, ""
			}
			return false, "delete file error!"
		}
		return false, "db error"
	}
	return false, "no such item"
}
// 新建一个note, 不需要添加历史记录
// 添加历史
func (this *NoteContentHistoryService) AddHistory(noteId, userId string, eachHistory info.EachHistory) {
	// 检查是否是空
	if eachHistory.Content == "" {
		return
	}

	// 先查是否存在历史记录, 没有则添加之
	history := info.NoteContentHistory{}
	db.GetByIdAndUserId(db.NoteContentHistories, noteId, userId, &history)
	if history.NoteId == "" {
		this.newHistory(noteId, userId, eachHistory)
	} else {
		// 判断是否超出 maxSize, 如果超出则pop最后一个, 再push之, 不用那么麻烦, 直接update吧, 虽然影响性能
		// TODO
		l := len(history.Histories)
		if l >= maxSize {
			// history.Histories = history.Histories[l-maxSize:] // BUG, 致使都是以前的
			history.Histories = history.Histories[:maxSize]
		}
		newHistory := []info.EachHistory{eachHistory}
		newHistory = append(newHistory, history.Histories...) // 在开头加了, 最近的在最前
		history.Histories = newHistory

		// 更新之
		db.UpdateByIdAndUserId(db.NoteContentHistories, noteId, userId, history)
	}
	return
}
Beispiel #3
0
// 复制共享的笔记时, 复制其中的图片到我本地
// 复制图片
func (this *FileService) CopyImage(userId, fileId, toUserId string) (bool, string) {
	// 是否已经复制过了
	file2 := info.File{}
	db.GetByQ(db.Files, bson.M{"UserId": bson.ObjectIdHex(toUserId), "FromFileId": bson.ObjectIdHex(fileId)}, &file2)
	if file2.FileId != "" {
		return true, file2.FileId.Hex()
	}

	// 复制之
	file := info.File{}
	db.GetByIdAndUserId(db.Files, fileId, userId, &file)

	if file.FileId == "" || file.UserId.Hex() != userId {
		return false, ""
	}

	_, ext := SplitFilename(file.Name)
	guid := NewGuid()
	newFilename := guid + ext

	// TODO 统一目录格式
	// dir := "files/" + toUserId + "/images"
	dir := "files/" + GetRandomFilePath(toUserId, guid) + "/images"
	filePath := dir + "/" + newFilename
	err := os.MkdirAll(revel.BasePath+dir, 0755)
	if err != nil {
		return false, ""
	}

	_, err = CopyFile(revel.BasePath+"/"+file.Path, revel.BasePath+"/"+filePath)
	if err != nil {
		return false, ""
	}

	fileInfo := info.File{Name: newFilename,
		Title:      file.Title,
		Path:       filePath,
		Size:       file.Size,
		FromFileId: file.FileId}
	id := bson.NewObjectId()
	fileInfo.FileId = id
	fileId = id.Hex()
	Ok, _ := this.AddImage(fileInfo, "", toUserId, false)

	if Ok {
		return Ok, id.Hex()
	}
	return false, ""
}
Beispiel #4
0
// delete image
func (this *FileService) DeleteImage(userId, fileId string) (bool, string) {
	file := info.File{}
	db.GetByIdAndUserId(db.Files, fileId, userId, &file)

	if file.FileId != "" {
		if db.DeleteByIdAndUserId(db.Files, fileId, userId) {
			// delete image
			err := os.Remove(revel.BasePath + "/public/" + file.Path)
			if err == nil {
				return true, ""
			}
			return false, "delete file error!"
		}
		return false, "db error"
	}
	return false, "no such item"
}
// 得到某notebook
func (this *NotebookService) GetNotebook(notebookId, userId string) info.Notebook {
	notebook := info.Notebook{}
	db.GetByIdAndUserId(db.Notebooks, notebookId, userId, &notebook)
	return notebook
}
Beispiel #6
0
// 通过id, userId得到noteContent
func (this *NoteService) GetNoteContent(noteContentId, userId string) (noteContent info.NoteContent) {
	noteContent = info.NoteContent{}
	db.GetByIdAndUserId(db.NoteContents, noteContentId, userId, &noteContent)
	return
}
Beispiel #7
0
// 通过id, userId得到note
func (this *NoteService) GetNote(noteId, userId string) (note info.Note) {
	note = info.Note{}
	db.GetByIdAndUserId(db.Notes, noteId, userId, &note)
	return
}
Beispiel #8
0
// 得到分组, shareService用
func (this *GroupService) GetGroup(userId, groupId string) info.Group {
	// 得到分组s
	group := info.Group{}
	db.GetByIdAndUserId(db.Groups, groupId, userId, &group)
	return group
}
// 列表展示
func (this *NoteContentHistoryService) ListHistories(noteId, userId string) []info.EachHistory {
	histories := info.NoteContentHistory{}
	db.GetByIdAndUserId(db.NoteContentHistories, noteId, userId, &histories)
	return histories.Histories
}