From 4f3347aaab9260ce89720e1f5c8279e4b5fd9d17 Mon Sep 17 00:00:00 2001 From: Remco Date: Mon, 10 Nov 2014 17:18:41 +0100 Subject: [PATCH] added head function to storage --- transfersh-server/storage.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/transfersh-server/storage.go b/transfersh-server/storage.go index 2a1a154..872698c 100644 --- a/transfersh-server/storage.go +++ b/transfersh-server/storage.go @@ -12,6 +12,7 @@ import ( type Storage interface { Get(token string, filename string) (reader io.ReadCloser, contentType string, contentLength uint64, err error) + Head(token string, filename string) (contentType string, contentLength uint64, err error) Put(token string, filename string, reader io.Reader, contentType string, contentLength uint64) error } @@ -24,6 +25,20 @@ func NewLocalStorage(basedir string) (*LocalStorage, error) { return &LocalStorage{basedir: basedir}, nil } +func (s *LocalStorage) Head(token string, filename string) (contentType string, contentLength uint64, err error) { + path := filepath.Join(s.basedir, token, filename) + + var fi os.FileInfo + if fi, err = os.Lstat(path); err != nil { + } + + contentLength = uint64(fi.Size()) + + contentType = mime.TypeByExtension(filepath.Ext(filename)) + + return +} + func (s *LocalStorage) Get(token string, filename string) (reader io.ReadCloser, contentType string, contentLength uint64, err error) { path := filepath.Join(s.basedir, token, filename) @@ -81,6 +96,17 @@ func NewS3Storage() (*S3Storage, error) { return &S3Storage{bucket: bucket}, nil } +func (s *S3Storage) Head(token string, filename string) (contentType string, contentLength uint64, err error) { + key := fmt.Sprintf("%s/%s", token, filename) + + // content type , content length + response, err := s.bucket.Head(key, map[string][]string{}) + contentType = "" + contentLength, err = strconv.ParseUint(response.Header.Get("Content-Length"), 10, 0) + + return +} + func (s *S3Storage) Get(token string, filename string) (reader io.ReadCloser, contentType string, contentLength uint64, err error) { key := fmt.Sprintf("%s/%s", token, filename)