/
output.go
76 lines (68 loc) · 2.1 KB
/
output.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
73
74
75
76
package main
import (
"image"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/cdillond/imgconv/pkg/utils"
"github.com/google/uuid"
)
func SaveFile(img image.Image, dstPath string, encCfg EncodeCfg) error {
f, err := os.Create(dstPath)
if err != nil {
return err
}
err = Encode(img, f, encCfg)
if err != nil {
f.Close() // can ignore this error
os.Remove(dstPath) // can ignore the error returned here
return err
}
return f.Close()
}
func GetDstFilePath(dstFileName, dstDir, srcUrl string, isRemote bool, fileType utils.FileType) (string, error) {
var dstName string
if dstFileName != "" {
// TO DO validate dstFileName to avoid possible issues with hidden files, files without extensions, and file names that include multiple periods
// if -out is an absolute file path, no further action is needed
if filepath.IsAbs(dstFileName) {
return dstFileName, nil
}
// if -out is not absolute, check if -dstDir is specified
if dstDir != "" {
// if so, join -dstDir and -out, and return as an absolute path
dstName = filepath.Join(dstDir, dstFileName)
return filepath.Abs(dstName)
} else {
// if no, join cwd and -out
return filepath.Abs(dstFileName)
}
}
// if -out is not specified...
// parse current file name and concat it with an appropriate extension
var srcFileNameExt, dstFileNameExt string
if isRemote {
u, _ := url.Parse(srcUrl) // this should already have succeeded, so no error this time
path := u.Path
srcFileNameExt = filepath.Base(path)
} else {
srcFileNameExt = filepath.Base(srcUrl)
}
// does not allow periods in file names
srcNameExtSlice := strings.Split(srcFileNameExt, ".")
if len(srcNameExtSlice) < 1 {
// assign random name
dstFileNameExt = uuid.NewString() + "." + utils.FileTypeToString(fileType)
} else {
dstFileNameExt = srcNameExtSlice[0] + "." + utils.FileTypeToString(fileType)
}
if dstDir != "" {
// if -dstDir is specified but -out is not
dstName = filepath.Join(dstDir, dstFileNameExt)
return filepath.Abs(dstName)
} else {
// if neither -dstDir nor -out is specified
return filepath.Abs(dstFileNameExt)
}
}