Skip to content

Commit 6fd3934

Browse files
committed
PUT /api/resource reconstruction
1 parent 99c69c8 commit 6fd3934

File tree

6 files changed

+84
-52
lines changed

6 files changed

+84
-52
lines changed

pkg/drives/base.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"files/pkg/fileutils"
99
"files/pkg/preview"
1010
"fmt"
11+
"github.com/spf13/afero"
1112
"io/ioutil"
1213
"k8s.io/klog/v2"
1314
"net/http"
@@ -23,7 +24,7 @@ type ResourceService interface {
2324
GetHandler(w http.ResponseWriter, r *http.Request, d *common.Data) (int, error)
2425
DeleteHandler(fileCache fileutils.FileCache) handleFunc
2526
PostHandler(w http.ResponseWriter, r *http.Request, d *common.Data) (int, error)
26-
//PutHandler(w http.ResponseWriter, r *http.Request, d *common.Data) (int, error)
27+
PutHandler(w http.ResponseWriter, r *http.Request, d *common.Data) (int, error)
2728
//PatchHandle(w http.ResponseWriter, r *http.Request, d *common.Data) (int, error)
2829
}
2930

@@ -302,3 +303,24 @@ func (rs *BaseResourceService) PostHandler(w http.ResponseWriter, r *http.Reques
302303
}
303304
return http.StatusBadRequest, fmt.Errorf("%s is not a valid directory path", r.URL.Path)
304305
}
306+
307+
func (rs *BaseResourceService) PutHandler(w http.ResponseWriter, r *http.Request, d *common.Data) (int, error) {
308+
// Only allow PUT for files.
309+
if strings.HasSuffix(r.URL.Path, "/") {
310+
return http.StatusMethodNotAllowed, nil
311+
}
312+
313+
exists, err := afero.Exists(files.DefaultFs, r.URL.Path)
314+
if err != nil {
315+
return http.StatusInternalServerError, err
316+
}
317+
if !exists {
318+
return http.StatusNotFound, nil
319+
}
320+
321+
info, err := fileutils.WriteFile(files.DefaultFs, r.URL.Path, r.Body)
322+
etag := fmt.Sprintf(`"%x%x"`, info.ModTime().UnixNano(), info.Size())
323+
w.Header().Set("ETag", etag)
324+
325+
return common.ErrToStatus(err), err
326+
}

pkg/drives/cloud_drive.go

+5
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,11 @@ func (rc *CloudDriveResourceService) PostHandler(w http.ResponseWriter, r *http.
995995
return status, err
996996
}
997997

998+
func (rc *CloudDriveResourceService) PutHandler(w http.ResponseWriter, r *http.Request, d *common.Data) (int, error) {
999+
// not public api for cloud drive, so it is not implemented
1000+
return http.StatusNotImplemented, fmt.Errorf("cloud drive does not supoort editing files")
1001+
}
1002+
9981003
func ResourceDeleteCloudDrive(fileCache fileutils.FileCache, src string, w http.ResponseWriter, r *http.Request, returnResp bool) ([]byte, int, error) {
9991004
if src == "" {
10001005
src = r.URL.Path

pkg/drives/google_drive.go

+5
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,11 @@ func (rc *GoogleDriveResourceService) PostHandler(w http.ResponseWriter, r *http
11741174
return status, err
11751175
}
11761176

1177+
func (rc *GoogleDriveResourceService) PutHandler(w http.ResponseWriter, r *http.Request, d *common.Data) (int, error) {
1178+
// not public api for google drive, so it is not implemented
1179+
return http.StatusNotImplemented, fmt.Errorf("google drive does not supoort editing files")
1180+
}
1181+
11771182
func ResourceDeleteGoogle(fileCache fileutils.FileCache, src string, w http.ResponseWriter, r *http.Request, returnResp bool) ([]byte, int, error) {
11781183
if src == "" {
11791184
src = r.URL.Path

pkg/drives/sync.go

+5
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ func (rc *SyncResourceService) PostHandler(w http.ResponseWriter, r *http.Reques
151151
return common.ErrToStatus(err), err
152152
}
153153

154+
func (rc *SyncResourceService) PutHandler(w http.ResponseWriter, r *http.Request, d *common.Data) (int, error) {
155+
// TODO: Sync support editing, but not in this structure for the time being. This func is reserving a slot for sync.
156+
return http.StatusNotImplemented, fmt.Errorf("sync drive does not supoort editing files for the time being")
157+
}
158+
154159
type Dirent struct {
155160
Type string `json:"type"`
156161
ID string `json:"id"`

pkg/fileutils/file.go

+35
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,38 @@ func GetUID(fs afero.Fs, path string) (int, error) {
247247

248248
return int(statT.Uid), nil
249249
}
250+
251+
func WriteFile(fs afero.Fs, dst string, in io.Reader) (os.FileInfo, error) {
252+
klog.Infoln("Before open ", dst)
253+
dir, _ := path.Split(dst)
254+
if err := fs.MkdirAll(dir, 0775); err != nil {
255+
return nil, err
256+
}
257+
if err := Chown(fs, dir, 1000, 1000); err != nil {
258+
klog.Errorf("can't chown directory %s to user %d: %s", dir, 1000, err)
259+
return nil, err
260+
}
261+
262+
klog.Infoln("Open ", dst)
263+
file, err := fs.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0775)
264+
if err != nil {
265+
return nil, err
266+
}
267+
defer file.Close()
268+
269+
klog.Infoln("Copy file!")
270+
_, err = io.Copy(file, in)
271+
if err != nil {
272+
return nil, err
273+
}
274+
275+
klog.Infoln("Get stat")
276+
// Gets the info about the file.
277+
info, err := file.Stat()
278+
if err != nil {
279+
return nil, err
280+
}
281+
282+
klog.Infoln(info)
283+
return info, nil
284+
}

pkg/http/resource.go

+11-51
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ import (
66
"files/pkg/drives"
77
"files/pkg/preview"
88
"fmt"
9-
"io"
109
"k8s.io/klog/v2"
1110
"net/http"
12-
"os"
1311
"path"
1412
"path/filepath"
1513
"strings"
@@ -79,24 +77,21 @@ func resourcePostHandler(w http.ResponseWriter, r *http.Request, d *common.Data)
7977
}
8078

8179
func resourcePutHandler(w http.ResponseWriter, r *http.Request, d *common.Data) (int, error) {
82-
// Only allow PUT for files.
83-
if strings.HasSuffix(r.URL.Path, "/") {
84-
return http.StatusMethodNotAllowed, nil
85-
}
80+
start := time.Now()
81+
klog.Infoln("Function resourcePostHandler starts at", start)
82+
defer func() {
83+
elapsed := time.Since(start)
84+
klog.Infof("Function resourcePostHandler execution time: %v\n", elapsed)
85+
}()
8686

87-
exists, err := afero.Exists(files.DefaultFs, r.URL.Path)
87+
srcType := r.URL.Query().Get("src")
88+
89+
handler, err := drives.GetResourceService(srcType)
8890
if err != nil {
89-
return http.StatusInternalServerError, err
90-
}
91-
if !exists {
92-
return http.StatusNotFound, nil
91+
return http.StatusBadRequest, err
9392
}
9493

95-
info, err := writeFile(files.DefaultFs, r.URL.Path, r.Body)
96-
etag := fmt.Sprintf(`"%x%x"`, info.ModTime().UnixNano(), info.Size())
97-
w.Header().Set("ETag", etag)
98-
99-
return common.ErrToStatus(err), err
94+
return handler.PutHandler(w, r, d)
10095
}
10196

10297
func resourcePatchHandler(fileCache fileutils.FileCache) handleFunc {
@@ -178,41 +173,6 @@ func addVersionSuffix(source string, fs afero.Fs, isDir bool) string {
178173
return source
179174
}
180175

181-
func writeFile(fs afero.Fs, dst string, in io.Reader) (os.FileInfo, error) {
182-
klog.Infoln("Before open ", dst)
183-
dir, _ := path.Split(dst)
184-
if err := fs.MkdirAll(dir, 0775); err != nil {
185-
return nil, err
186-
}
187-
if err := fileutils.Chown(fs, dir, 1000, 1000); err != nil {
188-
klog.Errorf("can't chown directory %s to user %d: %s", dir, 1000, err)
189-
return nil, err
190-
}
191-
192-
klog.Infoln("Open ", dst)
193-
file, err := fs.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0775)
194-
if err != nil {
195-
return nil, err
196-
}
197-
defer file.Close()
198-
199-
klog.Infoln("Copy file!")
200-
_, err = io.Copy(file, in)
201-
if err != nil {
202-
return nil, err
203-
}
204-
205-
klog.Infoln("Get stat")
206-
// Gets the info about the file.
207-
info, err := file.Stat()
208-
if err != nil {
209-
return nil, err
210-
}
211-
212-
klog.Infoln(info)
213-
return info, nil
214-
}
215-
216176
func patchAction(ctx context.Context, action, src, dst string, d *common.Data, fileCache fileutils.FileCache) error {
217177
switch action {
218178
case "copy":

0 commit comments

Comments
 (0)