This repository has been archived by the owner on Feb 13, 2024. It is now read-only.
/
wopifilecontents.go
72 lines (60 loc) · 2.11 KB
/
wopifilecontents.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package app
import (
"io"
"net/http"
"github.com/wkloucek/cs3-wopi-server/pkg/internal/helpers"
)
// GetFile downloads the file from the storage
// https://docs.microsoft.com/en-us/microsoft-365/cloud-storage-partner-program/rest/files/getfile
func GetFile(app *demoApp, w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
wopiContext, _ := WopiContextFromCtx(ctx)
// download the file
resp, err := helpers.DownloadFile(
ctx,
&wopiContext.FileReference,
app.gwc,
wopiContext.AccessToken,
app.Config.CS3DataGatewayInsecure,
app.Logger,
)
if err != nil || resp.StatusCode != http.StatusOK {
app.Logger.Error().Err(err).Str("status_code", http.StatusText(resp.StatusCode)).Str("FileReference", wopiContext.FileReference.String()).Msg("GetFile: downloading the file failed")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
// read the file from the body
defer resp.Body.Close()
_, err = io.Copy(w, resp.Body)
if err != nil {
app.Logger.Error().Str("FileReference", wopiContext.FileReference.String()).Msg("GetFile: copying the file content to the response body failed")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
http.Error(w, "", http.StatusOK)
}
// PutFile uploads the file to the storage
// https://docs.microsoft.com/en-us/microsoft-365/cloud-storage-partner-program/rest/files/putfile
func PutFile(app *demoApp, w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
wopiContext, _ := WopiContextFromCtx(ctx)
// read the file from the body
defer r.Body.Close()
// upload the file
err := helpers.UploadFile(
ctx,
r.Body,
&wopiContext.FileReference,
app.gwc,
wopiContext.AccessToken,
r.Header.Get(HeaderWopiLock),
app.Config.CS3DataGatewayInsecure,
app.Logger,
)
if err != nil {
app.Logger.Error().Err(err).Str("FileReference", wopiContext.FileReference.String()).Msg("PutFile: uploading the file failed")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
http.Error(w, "", http.StatusOK)
}