diff options
| author | Horus3 | 2015-08-02 00:16:24 +0200 |
|---|---|---|
| committer | Horus3 | 2015-08-02 00:16:24 +0200 |
| commit | 4e1f9ae3e7864e791d6df2c215ca70bd1e42eaf6 (patch) | |
| tree | 604ce9614a40875b5d21a367c3ef9ab175520c88 /imagestore | |
| parent | 6afcbc7ba57d80bacace21b6e73ca474dd8217ca (diff) | |
| download | mandible-4e1f9ae3e7864e791d6df2c215ca70bd1e42eaf6.tar.gz | |
Added multidomain support.
Diffstat (limited to 'imagestore')
| -rw-r--r-- | imagestore/gcsstore.go | 62 | ||||
| -rw-r--r-- | imagestore/localstore.go | 4 | ||||
| -rw-r--r-- | imagestore/s3store.go | 53 | ||||
| -rw-r--r-- | imagestore/store.go | 4 | ||||
| -rw-r--r-- | imagestore/strippath.go | 10 |
5 files changed, 12 insertions, 121 deletions
diff --git a/imagestore/gcsstore.go b/imagestore/gcsstore.go deleted file mode 100644 index 67da3d7..0000000 --- a/imagestore/gcsstore.go +++ /dev/null @@ -1,62 +0,0 @@ -package imagestore - -import ( - "io/ioutil" - "log" - - "golang.org/x/net/context" - "google.golang.org/cloud/storage" -) - -type GCSImageStore struct { - ctx context.Context - bucketName string - storeRoot string - namePathMapper *NamePathMapper -} - -func NewGCSImageStore(ctx context.Context, bucket string, root string, mapper *NamePathMapper) *GCSImageStore { - return &GCSImageStore{ - ctx: ctx, - bucketName: bucket, - storeRoot: root, - namePathMapper: mapper, - } -} - -func (this *GCSImageStore) Exists(obj *StoreObject) (bool, error) { - _, err := storage.StatObject(this.ctx, this.bucketName, this.toPath(obj)) - if err != nil { - return false, err - } - return true, nil -} - -func (this *GCSImageStore) Save(src string, obj *StoreObject, url string) (*StoreObject, error) { - data, err := ioutil.ReadFile(src) - if err != nil { - log.Printf("error on read file: %s", err) - return nil, err - } - - wc := storage.NewWriter(this.ctx, this.bucketName, this.toPath(obj)) - wc.ContentType = obj.MimeType - if _, err := wc.Write(data); err != nil { - log.Printf("error on write data: %s", err) - return nil, err - } - if err := wc.Close(); err != nil { - log.Printf("error on close writer: %s", err) - return nil, err - } - - obj.Url = "https://storage.googleapis.com/" + this.bucketName + "/" + this.toPath(obj) - return obj, nil -} - -func (this *GCSImageStore) toPath(obj *StoreObject) string { - if this.storeRoot != "" { - return this.storeRoot + "/" + this.namePathMapper.mapToPath(obj) - } - return this.namePathMapper.mapToPath(obj) -} diff --git a/imagestore/localstore.go b/imagestore/localstore.go index 4df2931..707503d 100644 --- a/imagestore/localstore.go +++ b/imagestore/localstore.go @@ -27,7 +27,7 @@ func (this *LocalImageStore) Exists(obj *StoreObject) (bool, error) { return true, nil } -func (this *LocalImageStore) Save(src string, obj *StoreObject) (*StoreObject, error) { +func (this *LocalImageStore) Save(src string, obj *StoreObject, responseUrl string) (*StoreObject, error) { // open input file fi, err := os.Open(src) if err != nil { @@ -75,7 +75,7 @@ func (this *LocalImageStore) Save(src string, obj *StoreObject) (*StoreObject, e } obj.Url = this.toPath(obj) - obj.Url = stripPath(obj.Url) + obj.Url = stripPath(obj.Url, responseUrl) return obj, nil } diff --git a/imagestore/s3store.go b/imagestore/s3store.go deleted file mode 100644 index c31a45f..0000000 --- a/imagestore/s3store.go +++ /dev/null @@ -1,53 +0,0 @@ -package imagestore - -import ( - "github.com/mitchellh/goamz/s3" - "io/ioutil" -) - -type S3ImageStore struct { - bucketName string - storeRoot string - client *s3.S3 - namePathMapper *NamePathMapper -} - -func NewS3ImageStore(bucket string, root string, client *s3.S3, mapper *NamePathMapper) *S3ImageStore { - return &S3ImageStore{ - bucketName: bucket, - storeRoot: root, - client: client, - namePathMapper: mapper, - } -} - -func (this *S3ImageStore) Exists(obj *StoreObject) (bool, error) { - bucket := this.client.Bucket(this.bucketName) - response, err := bucket.Head(this.toPath(obj)) - if err != nil { - return false, err - } - - return (response.StatusCode == 200), nil -} - -func (this *S3ImageStore) Save(src string, obj *StoreObject, url string) (*StoreObject, error) { - bucket := this.client.Bucket(this.bucketName) - - data, err := ioutil.ReadFile(src) - if err != nil { - return nil, err - } - - err = bucket.Put(this.toPath(obj), data, obj.MimeType, s3.PublicReadWrite) - if err != nil { - return nil, err - } - - obj.Url = "https://s3.amazonaws.com/" + this.bucketName + this.toPath(obj) - return obj, nil -} - -func (this *S3ImageStore) toPath(obj *StoreObject) string { - return this.storeRoot + "/" + this.namePathMapper.mapToPath(obj) -} diff --git a/imagestore/store.go b/imagestore/store.go index a58d1ff..97aaa1d 100644 --- a/imagestore/store.go +++ b/imagestore/store.go @@ -1,13 +1,13 @@ package imagestore type ImageStore interface { - Save(src string, obj *StoreObject) (*StoreObject, error) + Save(src string, obj *StoreObject, url string) (*StoreObject, error) Exists(obj *StoreObject) (bool, error) } type ImageStores []ImageStore -func (this *ImageStores) Save(src string, obj *StoreObject) { +func (this *ImageStores) Save(src string, obj *StoreObject, url string) { // TODO } diff --git a/imagestore/strippath.go b/imagestore/strippath.go index 11ea12e..7f804ce 100644 --- a/imagestore/strippath.go +++ b/imagestore/strippath.go @@ -5,9 +5,15 @@ import ( "strings" ) -func stripPath(url string) string { +func stripPath(url, responseUrl string) string { + var URL string ABSPATH := os.Getenv("UPLOAD_DIR") - URL := os.Getenv("UPLOAD_URL") + if responseUrl == "" { + URL = os.Getenv("UPLOAD_URL") + } else { + URL = responseUrl + } + return URL + strings.Replace(strings.TrimPrefix(url, ABSPATH), "/original/", "/i/", 1) //return URL + strings.TrimPrefix(url, ABSPATH) } |
