コード例 #1
0
// 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, "/")
			//modified by JacobXie
			var err error
			if qiniuService.IsUseQiniu() {
				err = qiniuService.DeleteOnQiniu(attach.Path)
			} else {
				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"
}
コード例 #2
0
// 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
}
コード例 #3
0
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()

	// 生成上传路径
	filePath := "files/" + c.GetUserId() + "/attachs"

	// 生成新的文件名
	filename := handel.Filename
	_, ext := SplitFilename(filename) // .doc
	filename = NewGuid() + ext

	// > 5M?
	maxFileSize := configService.GetUploadSize("uploadAttachSize")
	if maxFileSize <= 0 {
		maxFileSize = 1000
	}
	//modified by JacobXie
	//filesize = int64(len(data))
	var filesize int64
	// 获取文件大小的接口
	type Sizer interface {
		Size() int64
	}

	if sizeInterface, ok := file.(Sizer); ok {
		filesize = sizeInterface.Size()
	}

	if float64(filesize) > maxFileSize*float64(1024*1024) {
		resultMsg = fmt.Sprintf("The file's size is bigger than %vM", maxFileSize)
		return re
	}

	if qiniuService.IsUseQiniu() {
		toPath := filePath + "/" + filename
		err := qiniuService.Upload2Qiniu(toPath, file, filesize)
		if err != nil {
			return re
		}
	} else {
		data, err := ioutil.ReadAll(file)
		if err != nil {
			return re
		}
		dir := revel.BasePath + "/" + filePath
		err = os.MkdirAll(dir, 0755)
		if err != nil {
			return re
		}

		toPath := dir + "/" + filename
		err = ioutil.WriteFile(toPath, data, 0777)
		if err != nil {
			return re
		}
		filesize = GetFilesize(toPath)
	}

	// add File to db
	fileType := ""
	if ext != "" {
		fileType = strings.ToLower(ext[1:])
	}

	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
}