Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 11416f6

Browse files
committedMar 17, 2025·
PASTE SAME ARCH reconstruction
1 parent 0ff3309 commit 11416f6

File tree

7 files changed

+103
-75
lines changed

7 files changed

+103
-75
lines changed
 

‎pkg/common/patch.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func AddVersionSuffix(source string, fs afero.Fs, isDir bool) string {
4949
return source
5050
}
5151

52-
func PatchAction(ctx context.Context, action, src, dst string, d *Data, fileCache fileutils.FileCache) error {
52+
func PatchAction(ctx context.Context, action, src, dst string, fileCache fileutils.FileCache) error {
5353
switch action {
5454
case "copy":
5555
return fileutils.Copy(files.DefaultFs, src, dst)

‎pkg/drives/base.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,17 @@ import (
2121
type handleFunc func(w http.ResponseWriter, r *http.Request, d *common.Data) (int, error)
2222

2323
type ResourceService interface {
24+
// resource handlers
2425
GetHandler(w http.ResponseWriter, r *http.Request, d *common.Data) (int, error)
2526
DeleteHandler(fileCache fileutils.FileCache) handleFunc
2627
PostHandler(w http.ResponseWriter, r *http.Request, d *common.Data) (int, error)
2728
PutHandler(w http.ResponseWriter, r *http.Request, d *common.Data) (int, error)
2829
PatchHandler(fileCache fileutils.FileCache) handleFunc
30+
31+
// paste funcs
32+
PasteSame(action, src, dst string, rename bool, fileCache fileutils.FileCache, w http.ResponseWriter, r *http.Request) error
33+
//PasteFrom(w http.ResponseWriter, r *http.Request, d *common.Data) (int, error)
34+
//PasteTo(w http.ResponseWriter, r *http.Request, d *common.Data) (int, error)
2935
}
3036

3137
var (
@@ -355,8 +361,12 @@ func (rs *BaseResourceService) PatchHandler(fileCache fileutils.FileCache) handl
355361
}
356362

357363
klog.Infoln("Before patch action:", src, dst, action, rename)
358-
err = common.PatchAction(r.Context(), action, src, dst, d, fileCache)
364+
err = common.PatchAction(r.Context(), action, src, dst, fileCache)
359365

360366
return common.ErrToStatus(err), err
361367
}
362368
}
369+
370+
func (rs *BaseResourceService) PasteSame(action, src, dst string, rename bool, fileCache fileutils.FileCache, w http.ResponseWriter, r *http.Request) error {
371+
return common.PatchAction(r.Context(), action, src, dst, fileCache)
372+
}

‎pkg/drives/cache.go

+30
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,51 @@
11
package drives
22

33
import (
4+
"bytes"
45
"context"
56
"files/pkg/common"
67
"files/pkg/files"
78
"files/pkg/fileutils"
89
"fmt"
10+
"io/ioutil"
911
"k8s.io/klog/v2"
1012
"net/http"
1113
"os"
14+
"strconv"
1215
"strings"
1316
)
1417

1518
type CacheResourceService struct {
1619
BaseResourceService
1720
}
1821

22+
func (*CacheResourceService) PasteSame(action, src, dst string, rename bool, fileCache fileutils.FileCache, w http.ResponseWriter, r *http.Request) error {
23+
patchUrl := "http://127.0.0.1:80/api/resources/" + common.EscapeURLWithSpace(strings.TrimLeft(src, "/")) + "?action=" + action + "&destination=" + common.EscapeURLWithSpace(dst) + "&rename=" + strconv.FormatBool(rename)
24+
method := "PATCH"
25+
payload := []byte(``)
26+
klog.Infoln(patchUrl)
27+
28+
client := &http.Client{}
29+
req, err := http.NewRequest(method, patchUrl, bytes.NewBuffer(payload))
30+
if err != nil {
31+
return err
32+
}
33+
34+
req.Header = r.Header
35+
36+
res, err := client.Do(req)
37+
if err != nil {
38+
return err
39+
}
40+
defer res.Body.Close()
41+
42+
_, err = ioutil.ReadAll(res.Body)
43+
if err != nil {
44+
return err
45+
}
46+
return nil
47+
}
48+
1949
func CacheMkdirAll(dst string, mode os.FileMode, r *http.Request) error {
2050
targetURL := "http://127.0.0.1:80/api/resources" + common.EscapeURLWithSpace(dst) + "/?mode=" + mode.String()
2151

‎pkg/drives/cloud_drive.go

+25
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,31 @@ func (rc *CloudDriveResourceService) PatchHandler(fileCache fileutils.FileCache)
10061006
}
10071007
}
10081008

1009+
func (rc *CloudDriveResourceService) PasteSame(action, src, dst string, rename bool, fileCache fileutils.FileCache, w http.ResponseWriter, r *http.Request) error {
1010+
switch action {
1011+
case "copy":
1012+
if strings.HasSuffix(src, "/") {
1013+
src = strings.TrimSuffix(src, "/")
1014+
}
1015+
metaInfo, err := GetCloudDriveFocusedMetaInfos(src, w, r)
1016+
if err != nil {
1017+
return err
1018+
}
1019+
1020+
if metaInfo.IsDir {
1021+
return CopyCloudDriveFolder(src, dst, w, r, metaInfo.Path, metaInfo.Name)
1022+
}
1023+
return CopyCloudDriveSingleFile(src, dst, w, r)
1024+
case "rename":
1025+
if !strings.HasSuffix(src, "/") {
1026+
src += "/"
1027+
}
1028+
return MoveCloudDriveFolderOrFiles(src, dst, w, r)
1029+
default:
1030+
return fmt.Errorf("unknown action: %s", action)
1031+
}
1032+
}
1033+
10091034
func ResourceDeleteCloudDrive(fileCache fileutils.FileCache, src string, w http.ResponseWriter, r *http.Request, returnResp bool) ([]byte, int, error) {
10101035
if src == "" {
10111036
src = r.URL.Path

‎pkg/drives/google_drive.go

+25
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,31 @@ func (rc *GoogleDriveResourceService) PatchHandler(fileCache fileutils.FileCache
11851185
}
11861186
}
11871187

1188+
func (rc *GoogleDriveResourceService) PasteSame(action, src, dst string, rename bool, fileCache fileutils.FileCache, w http.ResponseWriter, r *http.Request) error {
1189+
switch action {
1190+
case "copy":
1191+
if !strings.HasSuffix(src, "/") {
1192+
src += "/"
1193+
}
1194+
metaInfo, err := GetGoogleDriveIdFocusedMetaInfos(src, w, r)
1195+
if err != nil {
1196+
return err
1197+
}
1198+
1199+
if metaInfo.IsDir {
1200+
return CopyGoogleDriveFolder(src, dst, w, r, metaInfo.Path)
1201+
}
1202+
return CopyGoogleDriveSingleFile(src, dst, w, r)
1203+
case "rename":
1204+
if !strings.HasSuffix(src, "/") {
1205+
src += "/"
1206+
}
1207+
return MoveGoogleDriveFolderOrFiles(src, dst, w, r)
1208+
default:
1209+
return fmt.Errorf("unknown action: %s", action)
1210+
}
1211+
}
1212+
11881213
func ResourceDeleteGoogle(fileCache fileutils.FileCache, src string, w http.ResponseWriter, r *http.Request, returnResp bool) ([]byte, int, error) {
11891214
if src == "" {
11901215
src = r.URL.Path

‎pkg/drives/sync.go

+4
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ func (rc *SyncResourceService) PatchHandler(fileCache fileutils.FileCache) handl
178178
}
179179
}
180180

181+
func (rc *SyncResourceService) PasteSame(action, src, dst string, rename bool, fileCache fileutils.FileCache, w http.ResponseWriter, r *http.Request) error {
182+
return ResourceSyncPatch(action, src, dst, r)
183+
}
184+
181185
type Dirent struct {
182186
Type string `json:"type"`
183187
ID string `json:"id"`

‎pkg/http/paste.go

+7-73
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package http
22

33
import (
4-
"bytes"
54
"compress/gzip"
65
"context"
76
"encoding/json"
@@ -20,7 +19,6 @@ import (
2019
"os"
2120
"path"
2221
"path/filepath"
23-
"strconv"
2422
"strings"
2523
)
2624

@@ -157,7 +155,7 @@ func resourcePasteHandler(fileCache fileutils.FileCache) handleFunc {
157155
}
158156

159157
if same {
160-
err = pasteActionSameArch(r.Context(), action, srcType, src, dstType, dst, d, fileCache, rename, w, r)
158+
err = pasteActionSameArch(action, srcType, src, dstType, dst, rename, fileCache, w, r)
161159
} else {
162160
err = pasteActionDiffArch(r.Context(), action, srcType, src, dstType, dst, d, fileCache, w, r)
163161
}
@@ -1033,80 +1031,16 @@ func moveDelete(fileCache fileutils.FileCache, srcType, src string, ctx context.
10331031
return os.ErrInvalid
10341032
}
10351033

1036-
func pasteActionSameArch(ctx context.Context, action, srcType, src, dstType, dst string, d *common.Data, fileCache fileutils.FileCache, rename bool, w http.ResponseWriter, r *http.Request) error {
1034+
func pasteActionSameArch(action, srcType, src, dstType, dst string, rename bool, fileCache fileutils.FileCache, w http.ResponseWriter, r *http.Request) error {
10371035
klog.Infoln("Now deal with ", action, " for same arch ", dstType)
10381036
klog.Infoln("src: ", src, ", dst: ", dst)
1039-
if srcType == "drive" || srcType == "cache" {
1040-
patchUrl := "http://127.0.0.1:80/api/resources/" + common.EscapeURLWithSpace(strings.TrimLeft(src, "/")) + "?action=" + action + "&destination=" + common.EscapeURLWithSpace(dst) + "&rename=" + strconv.FormatBool(rename)
1041-
method := "PATCH"
1042-
payload := []byte(``)
1043-
klog.Infoln(patchUrl)
10441037

1045-
client := &http.Client{}
1046-
req, err := http.NewRequest(method, patchUrl, bytes.NewBuffer(payload))
1047-
if err != nil {
1048-
return err
1049-
}
1050-
1051-
req.Header = r.Header
1052-
1053-
res, err := client.Do(req)
1054-
if err != nil {
1055-
return err
1056-
}
1057-
defer res.Body.Close()
1058-
1059-
_, err = ioutil.ReadAll(res.Body)
1060-
if err != nil {
1061-
return err
1062-
}
1063-
return nil
1064-
} else if srcType == "google" {
1065-
switch action {
1066-
case "copy":
1067-
if !strings.HasSuffix(src, "/") {
1068-
src += "/"
1069-
}
1070-
metaInfo, err := drives.GetGoogleDriveIdFocusedMetaInfos(src, w, r)
1071-
if err != nil {
1072-
return err
1073-
}
1074-
1075-
if metaInfo.IsDir {
1076-
return drives.CopyGoogleDriveFolder(src, dst, w, r, metaInfo.Path)
1077-
}
1078-
return drives.CopyGoogleDriveSingleFile(src, dst, w, r)
1079-
case "rename":
1080-
if !strings.HasSuffix(src, "/") {
1081-
src += "/"
1082-
}
1083-
return drives.MoveGoogleDriveFolderOrFiles(src, dst, w, r)
1084-
}
1085-
} else if srcType == "cloud" || srcType == "awss3" || srcType == "tencent" || srcType == "dropbox" {
1086-
switch action {
1087-
case "copy":
1088-
if strings.HasSuffix(src, "/") {
1089-
src = strings.TrimSuffix(src, "/")
1090-
}
1091-
metaInfo, err := drives.GetCloudDriveFocusedMetaInfos(src, w, r)
1092-
if err != nil {
1093-
return err
1094-
}
1095-
1096-
if metaInfo.IsDir {
1097-
return drives.CopyCloudDriveFolder(src, dst, w, r, metaInfo.Path, metaInfo.Name)
1098-
}
1099-
return drives.CopyCloudDriveSingleFile(src, dst, w, r)
1100-
case "rename":
1101-
if !strings.HasSuffix(src, "/") {
1102-
src += "/"
1103-
}
1104-
return drives.MoveCloudDriveFolderOrFiles(src, dst, w, r)
1105-
}
1106-
} else if srcType == "sync" {
1107-
return drives.ResourceSyncPatch(action, src, dst, r)
1038+
handler, err := drives.GetResourceService(srcType)
1039+
if err != nil {
1040+
return err
11081041
}
1109-
return nil
1042+
1043+
return handler.PasteSame(action, src, dst, rename, fileCache, w, r)
11101044
}
11111045

11121046
func pasteActionDiffArch(ctx context.Context, action, srcType, src, dstType, dst string, d *common.Data, fileCache fileutils.FileCache, w http.ResponseWriter, r *http.Request) error {

0 commit comments

Comments
 (0)
Please sign in to comment.