Exemple #1
0
func (s *Storage) CopyToStorage(attachment *tenpu.Attachment, toBlob tenpu.BlobStorage) (err error) {
	session := s.database.GetOrDialSession().Copy()
	defer session.Close()
	db := session.DB(s.database.DatabaseName)
	reader, err := db.GridFS("fs").OpenId(bson.ObjectIdHex(attachment.Id))
	if err == nil {
		defer reader.Close()
	} else {
		// log.Println("open file error: ", attachment.Id, attachment.Filename)
		return
	}
	err = toBlob.Put(attachment.Filename, attachment.ContentType, reader, attachment)

	return
}
Exemple #2
0
func (s *Storage) DeleteThumbnails(parentAttId string, blob tenpu.BlobStorage, meta tenpu.MetaStorage) (err error) {
	thumbs := s.ThumbnailByParentId(parentAttId)
	// log.Println("Delete thumbnail num:", len(thumbs))
	var thumbAttIds []string
	for _, thumb := range thumbs {
		thumbAttIds = append(thumbAttIds, thumb.BodyId)
	}

	for _, thumbAttId := range thumbAttIds {

		err = blob.Delete(thumbAttId)
		if err != nil && err != mgo.ErrNotFound {
			return
		}

		err = meta.Remove(thumbAttId)
		if err != nil {
			return
		}
	}
	err = s.RemoveAll(parentAttId)
	return
}
Exemple #3
0
func resizeAndStore(storage tenpu.BlobStorage, meta tenpu.MetaStorage, thumbnailStorage *Storage, att *tenpu.Attachment, spec *ThumbnailSpec, thumbName string, id string) (thumb *Thumbnail, err error) {

	var buf bytes.Buffer
	storage.Copy(att, &buf)

	if buf.Len() == 0 {
		return
	}
	thumbAtt := &tenpu.Attachment{}

	body, width, height, err := resizeThumbnail(&buf, spec)

	if err != nil {
		return
	}

	err = storage.Put(att.Filename, att.ContentType, body, thumbAtt)
	if err != nil {
		return
	}

	err = meta.Put(thumbAtt)
	if err != nil {
		return
	}

	thumb = &Thumbnail{
		Name:     thumbName,
		ParentId: id,
		BodyId:   thumbAtt.Id,
		Width:    int64(width),
		Height:   int64(height),
	}
	err = thumbnailStorage.Put(thumb)
	return
}