|
| 1 | +package svc |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "net/http" |
| 6 | + "path" |
| 7 | + "time" |
| 8 | + |
| 9 | + cs3rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1" |
| 10 | + storageprovider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" |
| 11 | + libregraph "github.com/opencloud-eu/libre-graph-api-go" |
| 12 | + revactx "github.com/opencloud-eu/reva/v2/pkg/ctx" |
| 13 | + "github.com/opencloud-eu/reva/v2/pkg/storagespace" |
| 14 | + |
| 15 | + "github.com/opencloud-eu/opencloud/services/graph/pkg/errorcode" |
| 16 | +) |
| 17 | + |
| 18 | +const downloadURLTTL = 30 * time.Minute |
| 19 | + |
| 20 | +// ErrDownloadURLSigningNotConfigured is returned when no url signing secret is configured |
| 21 | +var ErrDownloadURLSigningNotConfigured = errors.New("download url signing is not configured") |
| 22 | + |
| 23 | +// GetDriveItemContent redirects to a signed download url for a file |
| 24 | +func (g Graph) GetDriveItemContent(w http.ResponseWriter, r *http.Request) { |
| 25 | + ctx := r.Context() |
| 26 | + |
| 27 | + driveID, err := parseIDParam(r, "driveID") |
| 28 | + if err != nil { |
| 29 | + errorcode.RenderError(w, r, err) |
| 30 | + return |
| 31 | + } |
| 32 | + itemID, err := parseIDParam(r, "itemID") |
| 33 | + if err != nil { |
| 34 | + errorcode.RenderError(w, r, err) |
| 35 | + return |
| 36 | + } |
| 37 | + if driveID.GetStorageId() != itemID.GetStorageId() || driveID.GetSpaceId() != itemID.GetSpaceId() { |
| 38 | + errorcode.ItemNotFound.Render(w, r, http.StatusNotFound, "Item does not exist") |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + user, ok := revactx.ContextGetUser(ctx) |
| 43 | + if !ok { |
| 44 | + errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, "user not in context") |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + gatewayClient, err := g.gatewaySelector.Next() |
| 49 | + if err != nil { |
| 50 | + errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error()) |
| 51 | + return |
| 52 | + } |
| 53 | + stat, err := gatewayClient.Stat(ctx, &storageprovider.StatRequest{Ref: &storageprovider.Reference{ResourceId: &itemID}}) |
| 54 | + switch { |
| 55 | + case err != nil: |
| 56 | + errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error()) |
| 57 | + return |
| 58 | + case stat.GetStatus().GetCode() == cs3rpc.Code_CODE_OK: |
| 59 | + case stat.GetStatus().GetCode() == cs3rpc.Code_CODE_NOT_FOUND: |
| 60 | + errorcode.ItemNotFound.Render(w, r, http.StatusNotFound, stat.GetStatus().GetMessage()) |
| 61 | + return |
| 62 | + case stat.GetStatus().GetCode() == cs3rpc.Code_CODE_PERMISSION_DENIED: |
| 63 | + errorcode.ItemNotFound.Render(w, r, http.StatusNotFound, stat.GetStatus().GetMessage()) |
| 64 | + return |
| 65 | + case stat.GetStatus().GetCode() == cs3rpc.Code_CODE_UNAUTHENTICATED: |
| 66 | + errorcode.Unauthenticated.Render(w, r, http.StatusUnauthorized, stat.GetStatus().GetMessage()) |
| 67 | + return |
| 68 | + default: |
| 69 | + errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, stat.GetStatus().GetMessage()) |
| 70 | + return |
| 71 | + } |
| 72 | + if stat.GetInfo().GetType() != storageprovider.ResourceType_RESOURCE_TYPE_FILE { |
| 73 | + errorcode.ItemNotFound.Render(w, r, http.StatusNotFound, "Item is not a file") |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + downloadURL, err := g.signedDownloadURL(&itemID, user.GetId().GetOpaqueId()) |
| 78 | + if err != nil { |
| 79 | + errorcode.GeneralException.Render(w, r, http.StatusInternalServerError, err.Error()) |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + http.Redirect(w, r, downloadURL, http.StatusFound) |
| 84 | +} |
| 85 | + |
| 86 | +// SetDriveItemsDownloadURL adds a signed download url to every file in items when requested via $select |
| 87 | +func (g BaseGraphService) SetDriveItemsDownloadURL(r *http.Request, items []libregraph.DriveItem) { |
| 88 | + if !g.downloadURLRequested(r) { |
| 89 | + return |
| 90 | + } |
| 91 | + user, ok := revactx.ContextGetUser(r.Context()) |
| 92 | + if !ok { |
| 93 | + return |
| 94 | + } |
| 95 | + for i := range items { |
| 96 | + g.signDriveItemDownloadURL(&items[i], user.GetId().GetOpaqueId()) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func (g BaseGraphService) setDriveItemDownloadURL(r *http.Request, item *libregraph.DriveItem) { |
| 101 | + if !g.downloadURLRequested(r) { |
| 102 | + return |
| 103 | + } |
| 104 | + user, ok := revactx.ContextGetUser(r.Context()) |
| 105 | + if !ok { |
| 106 | + return |
| 107 | + } |
| 108 | + g.signDriveItemDownloadURL(item, user.GetId().GetOpaqueId()) |
| 109 | +} |
| 110 | + |
| 111 | +func (g BaseGraphService) signDriveItemDownloadURL(item *libregraph.DriveItem, userID string) { |
| 112 | + if item.File == nil { |
| 113 | + return |
| 114 | + } |
| 115 | + id, err := storagespace.ParseID(item.GetId()) |
| 116 | + if err != nil { |
| 117 | + g.logger.Debug().Err(err).Str("id", item.GetId()).Msg("could not parse drive item id for the download url") |
| 118 | + return |
| 119 | + } |
| 120 | + u, err := g.signedDownloadURL(&id, userID) |
| 121 | + if err != nil { |
| 122 | + g.logger.Debug().Err(err).Str("id", item.GetId()).Msg("could not sign the download url") |
| 123 | + return |
| 124 | + } |
| 125 | + item.MicrosoftGraphDownloadUrl = &u |
| 126 | +} |
| 127 | + |
| 128 | +func (g BaseGraphService) signedDownloadURL(id *storageprovider.ResourceId, userID string) (string, error) { |
| 129 | + if g.downloadSigner == nil { |
| 130 | + return "", ErrDownloadURLSigningNotConfigured |
| 131 | + } |
| 132 | + base, err := g.getWebDavBaseURL() |
| 133 | + if err != nil { |
| 134 | + return "", err |
| 135 | + } |
| 136 | + base.Path = path.Join(base.Path, storagespace.FormatResourceID(id)) |
| 137 | + return g.downloadSigner.Sign(base.String(), userID, downloadURLTTL) |
| 138 | +} |
| 139 | + |
| 140 | +func (g BaseGraphService) downloadURLRequested(r *http.Request) bool { |
| 141 | + return g.downloadSigner != nil && driveItemPropertySelected(r, _selectDownloadURL) |
| 142 | +} |
0 commit comments