dbutil: abstract out the last mongo type

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2017-04-07 11:56:05 -04:00
parent efa8ac6511
commit b80d34f423
Signed by: vbatts
GPG Key ID: 10937E57733F1362
3 changed files with 31 additions and 23 deletions

View File

@ -1,8 +1,9 @@
package dbutil package dbutil
import ( import (
"io"
"github.com/vbatts/imgsrv/types" "github.com/vbatts/imgsrv/types"
"labix.org/v2/mgo"
) )
// Handles are all the register backing Handlers // Handles are all the register backing Handlers
@ -13,8 +14,8 @@ type Handler interface {
Init(config interface{}) error Init(config interface{}) error
Close() error Close() error
Open(filename string) (*mgo.GridFile, error) Open(filename string) (File, error)
Create(filename string) (*mgo.GridFile, error) Create(filename string) (File, error)
Remove(filename string) error Remove(filename string) error
//HasFileByMd5(md5 string) (exists bool, err error) //HasFileByMd5(md5 string) (exists bool, err error)
@ -31,3 +32,15 @@ type Handler interface {
GetExtensions() (kp []types.IdCount, err error) GetExtensions() (kp []types.IdCount, err error)
GetKeywords() (kp []types.IdCount, err error) GetKeywords() (kp []types.IdCount, err error)
} }
type MetaDataer interface {
GetMeta(result interface{}) (err error)
SetMeta(metadata interface{})
}
type File interface {
io.Reader
io.Writer
io.Closer
MetaDataer
}

View File

@ -59,12 +59,12 @@ func (h mongoHandle) Close() error {
} }
// pass through for GridFs // pass through for GridFs
func (h mongoHandle) Open(filename string) (file *mgo.GridFile, err error) { func (h mongoHandle) Open(filename string) (file dbutil.File, err error) {
return h.Gfs.Open(strings.ToLower(filename)) return h.Gfs.Open(strings.ToLower(filename))
} }
// pass through for GridFs // pass through for GridFs
func (h mongoHandle) Create(filename string) (file *mgo.GridFile, err error) { func (h mongoHandle) Create(filename string) (file dbutil.File, err error) {
return h.Gfs.Create(strings.ToLower(filename)) return h.Gfs.Create(strings.ToLower(filename))
} }

View File

@ -15,37 +15,32 @@ type Info struct {
} }
type File struct { type File struct {
Metadata Info ",omitempty" Metadata Info ",omitempty"
Md5 string Md5 string
ChunkSize int ChunkSize int
UploadDate time.Time UploadDate time.Time
Length int64 Length int64
Filename string ",omitempty" Filename string ",omitempty"
ContentType string "contentType,omitempty"
} }
func (f *File) SetContentType() { // ContentType guesses the mime-type by the file's extension
f.ContentType = mime.TypeByExtension(filepath.Ext(f.Filename)) func (f *File) ContentType() string {
return mime.TypeByExtension(filepath.Ext(f.Filename))
} }
func (f *File) IsImage() bool { func (f *File) IsImage() bool {
f.SetContentType() return strings.HasPrefix(f.ContentType(), "image")
return strings.HasPrefix(f.ContentType, "image")
} }
func (f *File) IsVideo() bool { func (f *File) IsVideo() bool {
f.SetContentType() return strings.HasPrefix(f.ContentType(), "video")
return strings.HasPrefix(f.ContentType, "video")
} }
func (f *File) IsAudio() bool { func (f *File) IsAudio() bool {
f.SetContentType() return strings.HasPrefix(f.ContentType(), "audio")
return strings.HasPrefix(f.ContentType, "audio")
} }
/* // IdCount structure used for collecting values for a tag cloud
Structure used for collecting values from mongo for a tag cloud
*/
type IdCount struct { type IdCount struct {
Id string "_id" Id string "_id"
Value int Value int