Skip to content

Commit 51fdec7

Browse files
committed
refactor(sessionData): adjust session data
- move mutation action info to `sessionContext` - move archive action info to `sessionContext` - extract archive format enum
1 parent 8e048f7 commit 51fdec7

File tree

3 files changed

+46
-37
lines changed

3 files changed

+46
-37
lines changed

src/serverHandler/aliasHandler.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,19 @@ func (h *aliasHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
118118

119119
header(w, session.headers)
120120

121-
if data.IsMutate && h.mutate(w, r, session, data) {
121+
if session.isMutate && h.mutate(w, r, session, data) {
122122
return
123-
} else if data.IsArchive {
124-
switch data.ArchiveFormat {
125-
case "tar":
123+
} else if session.isArchive {
124+
switch session.archiveFormat {
125+
case tarFmt:
126126
if h.tar(w, r, session, data) {
127127
return
128128
}
129-
case "tgz":
129+
case tgzFmt:
130130
if h.tgz(w, r, session, data) {
131131
return
132132
}
133-
case "zip":
133+
case zipFmt:
134134
if h.zip(w, r, session, data) {
135135
return
136136
}

src/serverHandler/mutate.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ func (h *aliasHandler) mutate(w http.ResponseWriter, r *http.Request, session *s
1111
}
1212

1313
switch {
14-
case data.IsUpload:
14+
case session.isUpload:
1515
if data.CanUpload {
1616
ok = h.saveUploadFiles(data.AuthUserName, h.fs+session.aliasReqPath, data.CanMkdir, data.CanDelete, data.AliasSubItems, r)
1717
} else {
1818
data.Status = http.StatusBadRequest
1919
return
2020
}
21-
case data.IsMkdir:
21+
case session.isMkdir:
2222
if data.CanMkdir && !h.logError(r.ParseForm()) {
2323
ok = h.mkdirs(data.AuthUserName, h.fs+session.aliasReqPath, r.Form["name"], data.AliasSubItems, r)
2424
} else {
2525
data.Status = http.StatusBadRequest
2626
return
2727
}
28-
case data.IsDelete:
28+
case session.isDelete:
2929
if data.CanDelete && !h.logError(r.ParseForm()) {
3030
ok = h.deleteItems(data.AuthUserName, h.fs+session.aliasReqPath, r.Form["name"], data.AliasSubItems, r)
3131
} else {

src/serverHandler/sessionData.go

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,22 @@ import (
1212
"strings"
1313
)
1414

15+
type redirectAction int
16+
1517
const (
1618
noRedirect redirectAction = iota
1719
addSlashSuffix
1820
removeSlashSuffix
1921
)
2022

23+
type archiveFormat int
24+
25+
const (
26+
tarFmt archiveFormat = iota
27+
tgzFmt
28+
zipFmt
29+
)
30+
2131
const contentTypeJson = "application/json"
2232

2333
var acceptContentTypes = []string{
@@ -41,8 +51,6 @@ type itemHtml struct {
4151
DeleteUrl string
4252
}
4353

44-
type redirectAction int
45-
4654
type sessionContext struct {
4755
prefixReqPath string
4856
vhostReqPath string
@@ -62,6 +70,14 @@ type sessionContext struct {
6270

6371
wantJson bool
6472

73+
isUpload bool
74+
isMkdir bool
75+
isDelete bool
76+
isMutate bool
77+
78+
isArchive bool
79+
archiveFormat archiveFormat
80+
6581
file *os.File
6682

6783
errors []error
@@ -72,12 +88,6 @@ type responseData struct {
7288

7389
IsDownload bool
7490
IsDownloadFile bool
75-
IsUpload bool
76-
IsMkdir bool
77-
IsDelete bool
78-
IsMutate bool
79-
IsArchive bool
80-
ArchiveFormat string
8191

8292
CanIndex bool
8393
CanUpload bool
@@ -382,19 +392,18 @@ func (h *aliasHandler) getSessionData(r *http.Request) (session *sessionContext,
382392
}
383393

384394
isArchive := false
385-
archiveFormat := ""
395+
var arFmt archiveFormat
386396
if len(rawQuery) == 3 || (len(rawQuery) > 3 && rawQuery[3] == '&') {
387-
rawQuery3 := rawQuery[:3]
388-
switch rawQuery3 {
397+
switch rawQuery[:3] {
389398
case "tar":
390399
isArchive = true
391-
archiveFormat = rawQuery3
400+
arFmt = tarFmt
392401
case "tgz":
393402
isArchive = true
394-
archiveFormat = rawQuery3
403+
arFmt = tgzFmt
395404
case "zip":
396405
isArchive = true
397-
archiveFormat = rawQuery3
406+
arFmt = zipFmt
398407
}
399408
}
400409

@@ -494,13 +503,6 @@ func (h *aliasHandler) getSessionData(r *http.Request) (session *sessionContext,
494503
canCors := allowAccess && authSuccess && h.cors.match(vhostReqPath, fsPath, authUserId)
495504
loginAvail := len(authUserName) == 0 && h.users.Len() > 0
496505

497-
context := pathContext{
498-
download: isDownload,
499-
downloadfile: isDownloadFile,
500-
sort: rawSortBy,
501-
defaultSort: h.defaultSort,
502-
}
503-
504506
session = &sessionContext{
505507
prefixReqPath: prefixReqPath,
506508
vhostReqPath: vhostReqPath,
@@ -520,6 +522,14 @@ func (h *aliasHandler) getSessionData(r *http.Request) (session *sessionContext,
520522

521523
wantJson: wantJson,
522524

525+
isUpload: isUpload,
526+
isMkdir: isMkdir,
527+
isDelete: isDelete,
528+
isMutate: isMutate,
529+
530+
isArchive: isArchive,
531+
archiveFormat: arFmt,
532+
523533
file: file,
524534

525535
errors: errs,
@@ -529,12 +539,6 @@ func (h *aliasHandler) getSessionData(r *http.Request) (session *sessionContext,
529539

530540
IsDownload: isDownload,
531541
IsDownloadFile: isDownloadFile,
532-
IsUpload: isUpload,
533-
IsMkdir: isMkdir,
534-
IsDelete: isDelete,
535-
IsMutate: isMutate,
536-
IsArchive: isArchive,
537-
ArchiveFormat: archiveFormat,
538542

539543
CanIndex: canIndex,
540544
CanUpload: canUpload,
@@ -559,7 +563,12 @@ func (h *aliasHandler) getSessionData(r *http.Request) (session *sessionContext,
559563
SubItemsHtml: nil,
560564
SubItemPrefix: subItemPrefix,
561565
SortState: sortState,
562-
Context: context,
566+
Context: pathContext{
567+
download: isDownload,
568+
downloadfile: isDownloadFile,
569+
sort: rawSortBy,
570+
defaultSort: h.defaultSort,
571+
},
563572
}
564573
return
565574
}

0 commit comments

Comments
 (0)