Skip to content

Commit b99879a

Browse files
author
ddvk
committed
- case insensitive extension comparison
- return error if file extension is not supported
1 parent 35e2066 commit b99879a

File tree

5 files changed

+46
-39
lines changed

5 files changed

+46
-39
lines changed

api/api.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,15 @@ func (ctx *ApiCtx) UploadDocument(parentId string, sourceDocPath string) (*model
210210
return nil, errors.New("file name is invalid")
211211
}
212212

213+
if !util.IsFileTypeSupported(ext) {
214+
return nil, errors.New("unsupported file extension: " + ext)
215+
}
216+
213217
id := ""
214218
var err error
215219

216220
//restore document
217-
if ext == ".zip" {
221+
if ext == "zip" {
218222
id, err = archive.GetIdFromZip(sourceDocPath)
219223
if err != nil {
220224
return nil, err

archive/zipdoc.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ import (
88
"image/jpeg"
99
"io/ioutil"
1010
"os"
11-
"path/filepath"
12-
"strings"
1311

1412
"github.com/juruen/rmapi/log"
13+
"github.com/juruen/rmapi/util"
1514
"github.com/nfnt/resize"
1615
pdfmodel "github.com/unidoc/unipdf/v3/model"
1716
"github.com/unidoc/unipdf/v3/render"
@@ -52,7 +51,7 @@ func makeThumbnail(pdf []byte) ([]byte, error) {
5251
return out.Bytes(), nil
5352
}
5453

55-
// gets the Document UUID from an archive
54+
// GetIdFromZip tries to get the Document UUID from an archive
5655
func GetIdFromZip(srcPath string) (id string, err error) {
5756
file, err := os.Open(srcPath)
5857
if err != nil {
@@ -73,7 +72,8 @@ func GetIdFromZip(srcPath string) (id string, err error) {
7372
}
7473

7574
func CreateZipDocument(id, srcPath string) (zipPath string, err error) {
76-
ext := strings.TrimPrefix(filepath.Ext(srcPath), ".")
75+
_, ext := util.DocPathToName(srcPath)
76+
7777
if ext == "zip" {
7878
zipPath = srcPath
7979
return

shell/mput.go

+22-31
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,6 @@ func mputCmd(ctx *ShellCtxt) *ishell.Cmd {
6666
}
6767
}
6868

69-
// Checks whether the file has a pdf, epub or zip extension.
70-
//
71-
// Input -> [string] Valid file name.
72-
// Returns -> true if the file is a pdf or epub, false otherwise
73-
func checkFileType(fName string) bool {
74-
return (strings.Contains(fName, ".pdf") ||
75-
strings.Contains(fName, ".epub") ||
76-
strings.Contains(fName, ".zip"))
77-
}
78-
7969
// Print the required spaces and characters for tree formatting.
8070
//
8171
// Input -> [*ishell.Context]
@@ -184,32 +174,33 @@ func putFilesAndDirs(pCtx *ShellCtxt, pC *ishell.Context, localDir string, depth
184174

185175
case mode.IsRegular():
186176

187-
// Is a file.
188-
if checkFileType(name) {
189-
// Is a pdf or epub file
177+
docName, ext := util.DocPathToName(name)
178+
179+
if !util.IsFileTypeSupported(ext) {
180+
continue
181+
}
182+
183+
_, err := pCtx.api.Filetree.NodeByPath(docName, pCtx.node)
190184

191-
docName, _ := util.DocPathToName(name)
192-
_, err := pCtx.api.Filetree.NodeByPath(docName, pCtx.node)
185+
if err == nil {
186+
// Document already exists.
187+
treeFormat(pC, depth, index, lSize, tFS)
188+
pC.Printf("document [%s] already exists\n", name)
189+
} else {
190+
// Document does not exist.
191+
treeFormat(pC, depth, index, lSize, tFS)
192+
pC.Printf("uploading: [%s]...", name)
193+
doc, err := pCtx.api.UploadDocument(pCtx.node.Id(), name)
193194

194-
if err == nil {
195-
// Document already exists.
196-
treeFormat(pC, depth, index, lSize, tFS)
197-
pC.Printf("document [%s] already exists\n", name)
195+
if err != nil {
196+
pC.Err(fmt.Errorf("failed to upload file %s", name))
198197
} else {
199-
// Document does not exist.
200-
treeFormat(pC, depth, index, lSize, tFS)
201-
pC.Printf("uploading: [%s]...", name)
202-
doc, err := pCtx.api.UploadDocument(pCtx.node.Id(), name)
203-
204-
if err != nil {
205-
pC.Err(fmt.Errorf("failed to upload file %s", name))
206-
} else {
207-
// Document uploaded successfully.
208-
pC.Println(" complete")
209-
pCtx.api.Filetree.AddDocument(*doc)
210-
}
198+
// Document uploaded successfully.
199+
pC.Println(" complete")
200+
pCtx.api.Filetree.AddDocument(*doc)
211201
}
212202
}
203+
213204
}
214205
}
215206

shell/put.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func putCmd(ctx *ShellCtxt) *ishell.Cmd {
4848
document, err := ctx.api.UploadDocument(dstDir, srcName)
4949

5050
if err != nil {
51-
c.Err(errors.New(fmt.Sprint("Failed to upload file", srcName, err.Error())))
51+
c.Err(fmt.Errorf("Failed to upload file [%s] %v", srcName, err))
5252
return
5353
}
5454

util/util.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,21 @@ import (
1111
"github.com/juruen/rmapi/model"
1212
)
1313

14+
var supportedExt = map[string]bool{
15+
"epub": true,
16+
"pdf": true,
17+
"zip": true,
18+
}
19+
20+
func IsFileTypeSupported(ext string) bool {
21+
return supportedExt[ext]
22+
}
23+
24+
// DocPathToName extracts the file name and file extension (without .) from a given path
1425
func DocPathToName(p string) (name string, ext string) {
15-
ext = path.Ext(p)
16-
name = strings.TrimSuffix(path.Base(p), path.Ext(p))
26+
tmpExt := path.Ext(p)
27+
name = strings.TrimSuffix(path.Base(p), tmpExt)
28+
ext = strings.ToLower(strings.TrimPrefix(tmpExt, "."))
1729
return
1830
}
1931

0 commit comments

Comments
 (0)