Add 'webdav/' from commit '0d5eded8d0068b8df984b3d4c44a66f10c97cf77'

git-subtree-dir: webdav
git-subtree-mainline: 7ffaa859b3
git-subtree-split: 0d5eded8d0
This commit is contained in:
A.Unger
2020-09-18 12:56:12 +02:00
64 changed files with 4328 additions and 0 deletions
+30
View File
@@ -0,0 +1,30 @@
package svc
import (
"net/http"
"github.com/owncloud/ocis-webdav/pkg/metrics"
)
// NewInstrument returns a service that instruments metrics.
func NewInstrument(next Service, metrics *metrics.Metrics) Service {
return instrument{
next: next,
metrics: metrics,
}
}
type instrument struct {
next Service
metrics *metrics.Metrics
}
// ServeHTTP implements the Service interface.
func (i instrument) ServeHTTP(w http.ResponseWriter, r *http.Request) {
i.next.ServeHTTP(w, r)
}
// Thumbnail implements the Service interface.
func (i instrument) Thumbnail(w http.ResponseWriter, r *http.Request) {
i.next.Thumbnail(w, r)
}
+30
View File
@@ -0,0 +1,30 @@
package svc
import (
"net/http"
"github.com/owncloud/ocis-pkg/v2/log"
)
// NewLogging returns a service that logs messages.
func NewLogging(next Service, logger log.Logger) Service {
return logging{
next: next,
logger: logger,
}
}
type logging struct {
next Service
logger log.Logger
}
// ServeHTTP implements the Service interface.
func (l logging) ServeHTTP(w http.ResponseWriter, r *http.Request) {
l.next.ServeHTTP(w, r)
}
// Dummy implements the Service interface.
func (l logging) Thumbnail(w http.ResponseWriter, r *http.Request) {
l.next.Thumbnail(w, r)
}
+50
View File
@@ -0,0 +1,50 @@
package svc
import (
"net/http"
"github.com/owncloud/ocis-pkg/v2/log"
"github.com/owncloud/ocis-webdav/pkg/config"
)
// Option defines a single option function.
type Option func(o *Options)
// Options defines the available options for this package.
type Options struct {
Logger log.Logger
Config *config.Config
Middleware []func(http.Handler) http.Handler
}
// newOptions initializes the available default options.
func newOptions(opts ...Option) Options {
opt := Options{}
for _, o := range opts {
o(&opt)
}
return opt
}
// Logger provides a function to set the logger option.
func Logger(val log.Logger) Option {
return func(o *Options) {
o.Logger = val
}
}
// Config provides a function to set the config option.
func Config(val *config.Config) Option {
return func(o *Options) {
o.Config = val
}
}
// Middleware provides a function to set the middleware option.
func Middleware(val ...func(http.Handler) http.Handler) Option {
return func(o *Options) {
o.Middleware = val
}
}
+90
View File
@@ -0,0 +1,90 @@
package svc
import (
"net/http"
"strings"
"github.com/go-chi/chi"
"github.com/micro/go-micro/v2/client"
thumbnails "github.com/owncloud/ocis-thumbnails/pkg/proto/v0"
"github.com/owncloud/ocis-webdav/pkg/config"
thumbnail "github.com/owncloud/ocis-webdav/pkg/dav/thumbnails"
)
// Service defines the extension handlers.
type Service interface {
ServeHTTP(http.ResponseWriter, *http.Request)
Thumbnail(http.ResponseWriter, *http.Request)
}
// NewService returns a service implementation for Service.
func NewService(opts ...Option) Service {
options := newOptions(opts...)
m := chi.NewMux()
m.Use(options.Middleware...)
svc := Webdav{
config: options.Config,
mux: m,
}
m.Route(options.Config.HTTP.Root, func(r chi.Router) {
r.Get("/remote.php/dav/files/{user}/*", svc.Thumbnail)
})
return svc
}
// Webdav defines implements the business logic for Service.
type Webdav struct {
config *config.Config
mux *chi.Mux
}
// ServeHTTP implements the Service interface.
func (g Webdav) ServeHTTP(w http.ResponseWriter, r *http.Request) {
g.mux.ServeHTTP(w, r)
}
// Thumbnail implements the Service interface.
func (g Webdav) Thumbnail(w http.ResponseWriter, r *http.Request) {
tr, err := thumbnail.NewRequest(r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
c := thumbnails.NewThumbnailService("com.owncloud.api.thumbnails", client.DefaultClient)
rsp, err := c.GetThumbnail(r.Context(), &thumbnails.GetRequest{
Filepath: strings.TrimLeft(tr.Filepath, "/"),
Filetype: extensionToFiletype(tr.Filetype),
Etag: tr.Etag,
Width: int32(tr.Width),
Height: int32(tr.Height),
Authorization: tr.Authorization,
})
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
if len(rsp.Thumbnail) == 0 {
w.WriteHeader(http.StatusNotFound)
return
}
w.Header().Set("Content-Type", rsp.GetMimetype())
w.WriteHeader(http.StatusOK)
w.Write(rsp.Thumbnail)
}
func extensionToFiletype(ext string) thumbnails.GetRequest_FileType {
val, ok := thumbnails.GetRequest_FileType_value[strings.ToUpper(ext)]
if !ok {
return thumbnails.GetRequest_FileType(-1)
}
return thumbnails.GetRequest_FileType(val)
}
+26
View File
@@ -0,0 +1,26 @@
package svc
import (
"net/http"
)
// NewTracing returns a service that instruments traces.
func NewTracing(next Service) Service {
return tracing{
next: next,
}
}
type tracing struct {
next Service
}
// ServeHTTP implements the Service interface.
func (t tracing) ServeHTTP(w http.ResponseWriter, r *http.Request) {
t.next.ServeHTTP(w, r)
}
// Thumbnail implements the Service interface.
func (t tracing) Thumbnail(w http.ResponseWriter, r *http.Request) {
t.next.Thumbnail(w, r)
}