// delete attach func (this *AttachService) DeleteAttach(attachId, userId string) (bool, string) { attach := info.Attach{} db.Get(db.Attachs, attachId, &attach) if(attach.AttachId != "") { // 判断是否有权限为笔记添加附件 if !shareService.HasUpdateNotePerm(attach.NoteId.Hex(), userId) { return false, "No Perm" } if db.Delete(db.Attachs, bson.M{"_id": bson.ObjectIdHex(attachId)}) { this.updateNoteAttachNum(attach.NoteId, -1) attach.Path = strings.TrimLeft(attach.Path, "/") err := os.Remove(revel.BasePath + "/" + attach.Path) if err == nil { // userService.UpdateAttachSize(note.UserId.Hex(), -attach.Size) // 修改note Usn noteService.IncrNoteUsn(attach.NoteId.Hex(), userId) return true, "delete file success" } return false, "delete file error" } return false, "db error" } return false, "no such item" }
// add attach // api调用时, 添加attach之前是没有note的 // fromApi表示是api添加的, updateNote传过来的, 此时不要incNote's usn, 因为updateNote会inc的 func (this *AttachService) AddAttach(attach info.Attach, fromApi bool) (ok bool, msg string) { attach.CreatedTime = time.Now() ok = db.Insert(db.Attachs, attach) note := noteService.GetNoteById(attach.NoteId.Hex()) // api调用时, 添加attach之前是没有note的 var userId string if note.NoteId != "" { userId = note.UserId.Hex() } else { userId = attach.UploadUserId.Hex() } if ok { // 更新笔记的attachs num this.updateNoteAttachNum(attach.NoteId, 1) } if !fromApi { // 增长note's usn noteService.IncrNoteUsn(attach.NoteId.Hex(), userId) } return }
// add attach func (this *AttachService) AddAttach(attach info.Attach) (ok bool, msg string) { attach.CreatedTime = time.Now() ok = db.Insert(db.Attachs, attach) if ok { // 更新笔记的attachs num this.updateNoteAttachNum(attach.NoteId, 1) } return }
func (c Attach) uploadAttach(noteId string) (re info.Re) { var fileId = "" var resultMsg = "error" // 错误信息 var Ok = false var fileInfo info.Attach re = info.NewRe() defer func() { re.Id = fileId // 只是id, 没有其它信息 re.Msg = resultMsg re.Ok = Ok re.Item = fileInfo }() // 判断是否有权限为笔记添加附件 if !shareService.HasUpdateNotePerm(noteId, c.GetUserId()) { return re } file, handel, err := c.Request.FormFile("file") if err != nil { return re } defer file.Close() data, err := ioutil.ReadAll(file) if err != nil { return re } // > 5M? maxFileSize := configService.GetUploadSize("uploadAttachSize") if maxFileSize <= 0 { maxFileSize = 1000 } if float64(len(data)) > maxFileSize*float64(1024*1024) { resultMsg = fmt.Sprintf("The file's size is bigger than %vM", maxFileSize) return re } // 生成上传路径 // filePath := "files/" + c.GetUserId() + "/attachs" newGuid := NewGuid() filePath := "files/" + GetRandomFilePath(c.GetUserId(), newGuid) + "/attachs" dir := revel.BasePath + "/" + filePath err = os.MkdirAll(dir, 0755) if err != nil { return re } // 生成新的文件名 filename := handel.Filename _, ext := SplitFilename(filename) // .doc filename = newGuid + ext toPath := dir + "/" + filename err = ioutil.WriteFile(toPath, data, 0777) if err != nil { return re } // add File to db fileType := "" if ext != "" { fileType = strings.ToLower(ext[1:]) } filesize := GetFilesize(toPath) fileInfo = info.Attach{Name: filename, Title: handel.Filename, NoteId: bson.ObjectIdHex(noteId), UploadUserId: c.GetObjectUserId(), Path: filePath + "/" + filename, Type: fileType, Size: filesize} id := bson.NewObjectId() fileInfo.AttachId = id fileId = id.Hex() Ok, resultMsg = attachService.AddAttach(fileInfo, false) if resultMsg != "" { resultMsg = c.Message(resultMsg) } fileInfo.Path = "" // 不要返回 if Ok { resultMsg = "success" } return re }