|
| 1 | +package ocdav |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "path" |
| 8 | + "strings" |
| 9 | + |
| 10 | + gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1" |
| 11 | + rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" |
| 12 | + link "github.com/cs3org/go-cs3apis/cs3/sharing/link/v1beta1" |
| 13 | + provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" |
| 14 | + "github.com/cs3org/reva/internal/http/services/archiver/manager" |
| 15 | + "github.com/cs3org/reva/pkg/appctx" |
| 16 | + ctxpkg "github.com/cs3org/reva/pkg/ctx" |
| 17 | + "github.com/cs3org/reva/pkg/errtypes" |
| 18 | + "github.com/cs3org/reva/pkg/rhttp" |
| 19 | + "github.com/cs3org/reva/pkg/storage/utils/downloader" |
| 20 | + "github.com/cs3org/reva/pkg/storage/utils/walker" |
| 21 | + "github.com/pkg/errors" |
| 22 | + "github.com/rs/zerolog" |
| 23 | + "google.golang.org/grpc/metadata" |
| 24 | +) |
| 25 | + |
| 26 | +// index.php/s/jIKrtrkXCIXwg1y/download?path=%2FHugo&files=Intrinsico |
| 27 | +func (s *svc) handleLegacyPublicLinkDownload(w http.ResponseWriter, r *http.Request) { |
| 28 | + token := strings.TrimPrefix(r.URL.Path, "/") |
| 29 | + files := getFilesFromRequest(r) |
| 30 | + s.downloadFiles(r.Context(), w, token, files) |
| 31 | +} |
| 32 | + |
| 33 | +func getFilesFromRequest(r *http.Request) []string { |
| 34 | + q := r.URL.Query() |
| 35 | + dir := q.Get("path") |
| 36 | + files := []string{} |
| 37 | + |
| 38 | + if q.Get("files") != "" { |
| 39 | + files = append(files, path.Join(dir, q.Get("files"))) |
| 40 | + } else { |
| 41 | + for _, f := range q["files[]"] { |
| 42 | + files = append(files, path.Join(dir, f)) |
| 43 | + } |
| 44 | + } |
| 45 | + return files |
| 46 | +} |
| 47 | + |
| 48 | +func (s *svc) authenticate(ctx context.Context, token string) (context.Context, error) { |
| 49 | + // TODO (gdelmont): support password protected public links |
| 50 | + c, err := s.getClient() |
| 51 | + if err != nil { |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + res, err := c.Authenticate(ctx, &gateway.AuthenticateRequest{ |
| 55 | + Type: "publicshares", |
| 56 | + ClientId: token, |
| 57 | + ClientSecret: "password|", |
| 58 | + }) |
| 59 | + if err != nil { |
| 60 | + return nil, err |
| 61 | + } |
| 62 | + |
| 63 | + if res.Status.Code != rpc.Code_CODE_OK { |
| 64 | + if res.Status.Code == rpc.Code_CODE_NOT_FOUND { |
| 65 | + return nil, errtypes.NotFound(token) |
| 66 | + } |
| 67 | + return nil, errors.New(res.Status.Message) |
| 68 | + } |
| 69 | + |
| 70 | + ctx = metadata.AppendToOutgoingContext(ctx, ctxpkg.TokenHeader, res.Token) |
| 71 | + ctx = ctxpkg.ContextSetToken(ctx, res.Token) |
| 72 | + |
| 73 | + return ctx, nil |
| 74 | +} |
| 75 | + |
| 76 | +func (s *svc) handleHttpError(w http.ResponseWriter, err error, log *zerolog.Logger) { |
| 77 | + log.Error().Err(err).Msg("ocdav: got error") |
| 78 | + switch err.(type) { |
| 79 | + case errtypes.NotFound: |
| 80 | + http.Error(w, "Resource not found", http.StatusNotFound) |
| 81 | + case manager.ErrMaxSize, manager.ErrMaxFileCount: |
| 82 | + http.Error(w, err.Error(), http.StatusRequestEntityTooLarge) |
| 83 | + default: |
| 84 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func (s *svc) downloadFiles(ctx context.Context, w http.ResponseWriter, token string, files []string) { |
| 89 | + log := appctx.GetLogger(ctx) |
| 90 | + ctx, err := s.authenticate(ctx, token) |
| 91 | + if err != nil { |
| 92 | + s.handleHttpError(w, err, log) |
| 93 | + return |
| 94 | + } |
| 95 | + isSingleFileShare, res, err := s.isSingleFileShare(ctx, token, files) |
| 96 | + if err != nil { |
| 97 | + s.handleHttpError(w, err, log) |
| 98 | + return |
| 99 | + } |
| 100 | + if isSingleFileShare { |
| 101 | + s.downloadFile(ctx, w, res) |
| 102 | + } else { |
| 103 | + s.downloadArchive(ctx, w, token, files) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func (s *svc) isSingleFileShare(ctx context.Context, token string, files []string) (bool, *provider.ResourceInfo, error) { |
| 108 | + switch len(files) { |
| 109 | + case 0: |
| 110 | + return s.resourceIsFileInPublicLink(ctx, token, "") |
| 111 | + case 1: |
| 112 | + return s.resourceIsFileInPublicLink(ctx, token, files[0]) |
| 113 | + default: |
| 114 | + // FIXME (gdelmont): even if the list contains more than one file |
| 115 | + // these (or part of them), could not exist |
| 116 | + // in this case, filtering the existing ones, we could |
| 117 | + // end up having 0 or 1 files |
| 118 | + return false, nil, nil |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +func (s *svc) resourceIsFileInPublicLink(ctx context.Context, token, file string) (bool, *provider.ResourceInfo, error) { |
| 123 | + res, err := s.getResourceFromPublicLinkToken(ctx, token, file) |
| 124 | + if err != nil { |
| 125 | + return false, nil, err |
| 126 | + } |
| 127 | + return res.Type == provider.ResourceType_RESOURCE_TYPE_FILE, res, nil |
| 128 | +} |
| 129 | + |
| 130 | +func (s *svc) getResourceFromPublicLinkToken(ctx context.Context, token, file string) (*provider.ResourceInfo, error) { |
| 131 | + c, err := s.getClient() |
| 132 | + if err != nil { |
| 133 | + return nil, err |
| 134 | + } |
| 135 | + res, err := c.GetPublicShareByToken(ctx, &link.GetPublicShareByTokenRequest{ |
| 136 | + Token: token, |
| 137 | + }) |
| 138 | + if err != nil { |
| 139 | + return nil, err |
| 140 | + } |
| 141 | + |
| 142 | + if res.Status.Code != rpc.Code_CODE_OK { |
| 143 | + if res.Status.Code == rpc.Code_CODE_NOT_FOUND { |
| 144 | + return nil, errtypes.NotFound(token) |
| 145 | + } |
| 146 | + return nil, errtypes.InternalError(res.Status.Message) |
| 147 | + } |
| 148 | + |
| 149 | + statRes, err := c.Stat(ctx, &provider.StatRequest{Ref: &provider.Reference{ResourceId: res.Share.ResourceId, Path: file}}) |
| 150 | + if err != nil { |
| 151 | + return nil, err |
| 152 | + } |
| 153 | + |
| 154 | + if statRes.Status.Code != rpc.Code_CODE_OK { |
| 155 | + if statRes.Status.Code == rpc.Code_CODE_NOT_FOUND { |
| 156 | + return nil, errtypes.NotFound(token) |
| 157 | + } |
| 158 | + return nil, errtypes.InternalError(statRes.Status.Message) |
| 159 | + } |
| 160 | + return statRes.Info, nil |
| 161 | +} |
| 162 | + |
| 163 | +func (s *svc) downloadFile(ctx context.Context, w http.ResponseWriter, res *provider.ResourceInfo) { |
| 164 | + log := appctx.GetLogger(ctx) |
| 165 | + c, err := s.getClient() |
| 166 | + if err != nil { |
| 167 | + s.handleHttpError(w, err, log) |
| 168 | + return |
| 169 | + } |
| 170 | + d := downloader.NewDownloader(c) |
| 171 | + r, err := d.Download(ctx, res.Path) |
| 172 | + if err != nil { |
| 173 | + s.handleHttpError(w, err, log) |
| 174 | + return |
| 175 | + } |
| 176 | + defer r.Close() |
| 177 | + |
| 178 | + w.WriteHeader(http.StatusOK) |
| 179 | + |
| 180 | + _, err = io.Copy(w, r) |
| 181 | + if err != nil { |
| 182 | + s.handleHttpError(w, err, log) |
| 183 | + return |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +func getPublicLinkResources(rootFolder, token string, files []string) []string { |
| 188 | + r := make([]string, 0, len(files)) |
| 189 | + for _, f := range files { |
| 190 | + r = append(r, path.Join(rootFolder, token, f)) |
| 191 | + } |
| 192 | + if len(r) == 0 { |
| 193 | + r = []string{path.Join(rootFolder, token)} |
| 194 | + } |
| 195 | + return r |
| 196 | +} |
| 197 | + |
| 198 | +func (s *svc) downloadArchive(ctx context.Context, w http.ResponseWriter, token string, files []string) { |
| 199 | + log := appctx.GetLogger(ctx) |
| 200 | + resources := getPublicLinkResources(s.c.PublicLinkDownload.PublicFolder, token, files) |
| 201 | + |
| 202 | + gtw, err := s.getClient() |
| 203 | + if err != nil { |
| 204 | + s.handleHttpError(w, err, log) |
| 205 | + return |
| 206 | + } |
| 207 | + |
| 208 | + downloader := downloader.NewDownloader(gtw, rhttp.Context(ctx)) |
| 209 | + walker := walker.NewWalker(gtw) |
| 210 | + |
| 211 | + archiver, err := manager.NewArchiver(resources, walker, downloader, manager.Config{ |
| 212 | + MaxNumFiles: s.c.PublicLinkDownload.MaxNumFiles, |
| 213 | + MaxSize: s.c.PublicLinkDownload.MaxSize, |
| 214 | + }) |
| 215 | + if err != nil { |
| 216 | + s.handleHttpError(w, err, log) |
| 217 | + return |
| 218 | + } |
| 219 | + |
| 220 | + if err := archiver.CreateTar(ctx, w); err != nil { |
| 221 | + s.handleHttpError(w, err, log) |
| 222 | + return |
| 223 | + } |
| 224 | +} |
0 commit comments