implement thumbnail support for txt files

This commit is contained in:
David Christofas
2021-04-29 11:58:50 +02:00
parent 9ddfab8fbd
commit 12b4a26385
16 changed files with 160 additions and 59 deletions

View File

@@ -11,7 +11,7 @@ import (
"github.com/cs3org/reva/pkg/token"
"github.com/pkg/errors"
"google.golang.org/grpc/metadata"
"image"
"io"
"net/http"
)
@@ -25,7 +25,9 @@ func NewCS3Source(c gateway.GatewayAPIClient) CS3 {
}
}
func (s CS3) Get(ctx context.Context, path string) (image.Image, error) {
// Get downloads the file from a cs3 service
// The caller MUST make sure to close the returned ReadCloser
func (s CS3) Get(ctx context.Context, path string) (io.ReadCloser, error) {
auth, ok := ContextGetAuthorization(ctx)
if !ok {
return nil, errors.New("cs3source: authorization missing")
@@ -63,19 +65,14 @@ func (s CS3) Get(ctx context.Context, path string) (image.Image, error) {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} //nolint:gosec
client := &http.Client{}
resp, err := client.Do(httpReq)
resp, err := client.Do(httpReq) // nolint:bodyclose
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
return resp.Body, nil
}

View File

@@ -2,7 +2,7 @@ package imgsource
import (
"context"
"image"
"io"
"os"
"path/filepath"
@@ -23,17 +23,12 @@ type FileSystem struct {
}
// Get retrieves an image from the filesystem.
func (s FileSystem) Get(ctx context.Context, file string) (image.Image, error) {
func (s FileSystem) Get(ctx context.Context, file string) (io.ReadCloser, error) {
imgPath := filepath.Join(s.basePath, file)
f, err := os.Open(filepath.Clean(imgPath))
if err != nil {
return nil, errors.Wrapf(err, "failed to load the file %s from %s", file, imgPath)
}
img, _, err := image.Decode(f)
if err != nil {
return nil, errors.Wrap(err, "Get: Decode:")
}
return img, nil
return f, nil
}

View File

@@ -2,7 +2,7 @@ package imgsource
import (
"context"
"image"
"io"
)
type key int
@@ -13,7 +13,7 @@ const (
// Source defines the interface for image sources
type Source interface {
Get(ctx context.Context, path string) (image.Image, error)
Get(ctx context.Context, path string) (io.ReadCloser, error)
}
// ContextSetAuthorization puts the authorization in the context.

View File

@@ -6,10 +6,10 @@ import (
"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
"io"
"net/http"
)
@@ -26,7 +26,8 @@ type WebDav struct {
}
// Get downloads the file from a webdav service
func (s WebDav) Get(ctx context.Context, url string) (image.Image, error) {
// The caller MUST make sure to close the returned ReadCloser
func (s WebDav) Get(ctx context.Context, url string) (io.ReadCloser, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, errors.Wrapf(err, `could not get the image "%s"`, url)
@@ -39,19 +40,14 @@ func (s WebDav) Get(ctx context.Context, url string) (image.Image, error) {
}
client := &http.Client{}
resp, err := client.Do(req)
resp, err := client.Do(req) // nolint:bodyclose
if err != nil {
return nil, errors.Wrapf(err, `could not get the image "%s"`, url)
}
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 ", url, resp.StatusCode)
}
img, _, err := image.Decode(resp.Body)
if err != nil {
return nil, errors.Wrapf(err, `could not decode the image "%s"`, url)
}
return img, nil
return resp.Body, nil
}

View File

@@ -6,6 +6,7 @@ import (
"github.com/owncloud/ocis/ocis-pkg/log"
"github.com/owncloud/ocis/thumbnails/pkg/thumbnail/storage"
"image"
"mime"
"strings"
)
@@ -15,6 +16,7 @@ var (
"image/jpg",
"image/jpeg",
"image/gif",
"text/plain",
}
)
@@ -90,8 +92,12 @@ func mapToStorageRequest(r Request) storage.Request {
}
func IsMimeTypeSupported(m string) bool {
mimeType, _, err := mime.ParseMediaType(m)
if err != nil {
return false
}
for _, mt := range SupportedMimeTypes {
if strings.EqualFold(mt, m) {
if strings.EqualFold(mt, mimeType) {
return true
}
}