prepare for public link thumbnails
- Implement a CS3 image source handler - Use checksum instead of etag for unique check - Simplify storage layout
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
package imgsource
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
|
||||
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
|
||||
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
|
||||
"github.com/cs3org/reva/pkg/rhttp"
|
||||
"github.com/cs3org/reva/pkg/token"
|
||||
"github.com/pkg/errors"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"image"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type CS3 struct {
|
||||
client gateway.GatewayAPIClient
|
||||
}
|
||||
|
||||
func NewCS3Source(c gateway.GatewayAPIClient) CS3 {
|
||||
return CS3{
|
||||
client: c,
|
||||
}
|
||||
}
|
||||
|
||||
func (s CS3) Get(ctx context.Context, path string) (image.Image, error) {
|
||||
auth, _ := ContextGetAuthorization(ctx)
|
||||
ctx = metadata.AppendToOutgoingContext(context.Background(), token.TokenHeader, auth)
|
||||
rsp, err := s.client.InitiateFileDownload(ctx, &provider.InitiateFileDownloadRequest{
|
||||
Ref: &provider.Reference{
|
||||
Spec: &provider.Reference_Path{
|
||||
Path: path,
|
||||
},
|
||||
} ,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if rsp.Status.Code != rpc.Code_CODE_OK {
|
||||
return nil, fmt.Errorf("could not load image: %s", rsp.Status.Message)
|
||||
}
|
||||
var ep, tk string
|
||||
for _, p := range rsp.Protocols {
|
||||
if p.Protocol == "simple" {
|
||||
ep, tk = p.DownloadEndpoint, p.Token
|
||||
}
|
||||
}
|
||||
|
||||
httpReq, err := rhttp.NewRequest(ctx, "GET", ep, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
httpReq.Header.Set(token.TokenHeader, auth)
|
||||
httpReq.Header.Set("X-REVA-TRANSFER", tk)
|
||||
|
||||
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} //nolint:gosec
|
||||
client := &http.Client{}
|
||||
|
||||
resp, err := client.Do(httpReq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close() //nolint:errcheck
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("could not get the image \"%s\". Request returned with statuscode %d ", path, resp.StatusCode)
|
||||
}
|
||||
|
||||
img, _, err := image.Decode(resp.Body)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, `could not decode the image "%s"`, path)
|
||||
}
|
||||
return img, nil
|
||||
}
|
||||
@@ -4,37 +4,30 @@ import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"github.com/owncloud/ocis/thumbnails/pkg/config"
|
||||
"github.com/pkg/errors"
|
||||
"image"
|
||||
_ "image/gif" // Import the gif package so that image.Decode can understand gifs
|
||||
_ "image/jpeg" // Import the jpeg package so that image.Decode can understand jpegs
|
||||
_ "image/png" // Import the png package so that image.Decode can understand pngs
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
|
||||
"github.com/owncloud/ocis/thumbnails/pkg/config"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// NewWebDavSource creates a new webdav instance.
|
||||
func NewWebDavSource(cfg config.WebDavSource) WebDav {
|
||||
func NewWebDavSource(cfg config.Thumbnail) WebDav {
|
||||
return WebDav{
|
||||
baseURL: cfg.BaseURL,
|
||||
insecure: cfg.Insecure,
|
||||
insecure: cfg.WebdavAllowInsecure,
|
||||
}
|
||||
}
|
||||
|
||||
// WebDav implements the Source interface for webdav services
|
||||
type WebDav struct {
|
||||
baseURL string
|
||||
insecure bool
|
||||
}
|
||||
|
||||
// Get downloads the file from a webdav service
|
||||
func (s WebDav) Get(ctx context.Context, file string) (image.Image, error) {
|
||||
u, _ := url.Parse(s.baseURL)
|
||||
u.Path = path.Join(u.Path, file)
|
||||
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
|
||||
req, err := http.NewRequest(http.MethodGet, file, nil)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, `could not get the image "%s"`, file)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user