Backend: Use constants for home dir and path separator
Signed-off-by: Michael Mayer <michael@lastzero.net>
This commit is contained in:
parent
c8ded4c7c1
commit
45f1a34018
|
@ -4,6 +4,7 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
|
@ -169,8 +170,7 @@ func ShareWithAccount(router *gin.RouterGroup) {
|
|||
}
|
||||
|
||||
for _, file := range files {
|
||||
dstFileName := dst + "/" + file.ShareFileName()
|
||||
|
||||
dstFileName := path.Join(dst, file.ShareFileName())
|
||||
fileShare := entity.NewFileShare(file.ID, m.ID, dstFileName)
|
||||
entity.FirstOrCreateFileShare(fileShare)
|
||||
}
|
||||
|
|
|
@ -439,7 +439,7 @@ func (m *MediaFile) RelPath(directory string) string {
|
|||
}
|
||||
|
||||
// Use empty string for current / root directory.
|
||||
if pathname == "." || pathname == "/" {
|
||||
if pathname == "." || pathname == "/" || pathname == "\\" {
|
||||
pathname = ""
|
||||
}
|
||||
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package fs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
@ -16,21 +16,21 @@ type FileInfo struct {
|
|||
}
|
||||
|
||||
func NewFileInfo(info os.FileInfo, dir string) FileInfo {
|
||||
if dir == "/" {
|
||||
dir = ""
|
||||
} else if len(dir) > 0 {
|
||||
if dir[len(dir)-1:] == "/" {
|
||||
if dir != PathSeparator && len(dir) > 0 {
|
||||
if dir[len(dir)-1:] == PathSeparator {
|
||||
dir = dir[:len(dir)-1]
|
||||
}
|
||||
|
||||
if dir[0:1] != "/" {
|
||||
dir = "/" + dir
|
||||
if dir[0:1] != PathSeparator {
|
||||
dir = PathSeparator + dir
|
||||
}
|
||||
} else {
|
||||
dir = PathSeparator
|
||||
}
|
||||
|
||||
result := FileInfo{
|
||||
Name: info.Name(),
|
||||
Abs: fmt.Sprintf("%s/%s", dir, info.Name()),
|
||||
Abs: filepath.Join(dir, info.Name()),
|
||||
Size: info.Size(),
|
||||
Date: info.ModTime(),
|
||||
Dir: info.IsDir(),
|
||||
|
|
|
@ -32,17 +32,17 @@ func TestNewFileInfos(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
|
||||
result := NewFileInfos(infos, "/")
|
||||
result := NewFileInfos(infos, PathSeparator)
|
||||
|
||||
if len(result) < 1 {
|
||||
t.Fatal("empty result")
|
||||
}
|
||||
|
||||
expected := map[string]FileInfo{
|
||||
"test.jpg": {Abs: "/test.jpg", Size: 10990, Dir: false},
|
||||
"CATYELLOW.jpg": {Abs: "/CATYELLOW.jpg", Size: 70790, Dir: false},
|
||||
"directory": {Abs: "/directory", Size: 4096, Dir: true},
|
||||
"linked": {Abs: "/linked", Size: 4096, Dir: true},
|
||||
"test.jpg": {Abs: PathSeparator + "test.jpg", Size: 10990, Dir: false},
|
||||
"CATYELLOW.jpg": {Abs: PathSeparator + "CATYELLOW.jpg", Size: 70790, Dir: false},
|
||||
"directory": {Abs: PathSeparator + "directory", Size: 4096, Dir: true},
|
||||
"linked": {Abs: PathSeparator + "linked", Size: 4096, Dir: true},
|
||||
}
|
||||
|
||||
for _, file := range result {
|
||||
|
|
|
@ -44,6 +44,9 @@ import (
|
|||
|
||||
const IgnoreFile = ".ppignore"
|
||||
const HiddenPath = ".photoprism"
|
||||
const PathSeparator = string(filepath.Separator)
|
||||
const Home = "~"
|
||||
const HomePath = Home + PathSeparator
|
||||
|
||||
// FileExists returns true if file exists and is not a directory.
|
||||
func FileExists(fileName string) bool {
|
||||
|
@ -86,7 +89,7 @@ func Abs(name string) string {
|
|||
return ""
|
||||
}
|
||||
|
||||
if len(name) > 2 && name[:2] == "~/" {
|
||||
if len(name) > 2 && name[:2] == HomePath {
|
||||
if usr, err := user.Current(); err == nil {
|
||||
name = filepath.Join(usr.HomeDir, name[2:])
|
||||
}
|
||||
|
@ -121,6 +124,7 @@ func copyToFile(f *zip.File, dest string) (fileName string, err error) {
|
|||
|
||||
// Make File
|
||||
var fdir string
|
||||
|
||||
if lastIndex := strings.LastIndex(fileName, string(os.PathSeparator)); lastIndex > -1 {
|
||||
fdir = fileName[:lastIndex]
|
||||
}
|
||||
|
@ -136,6 +140,7 @@ func copyToFile(f *zip.File, dest string) (fileName string, err error) {
|
|||
}
|
||||
|
||||
defer fd.Close()
|
||||
|
||||
_, err = io.Copy(fd, rc)
|
||||
if err != nil {
|
||||
return fileName, err
|
||||
|
@ -153,6 +158,7 @@ func Download(filepath string, url string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer out.Close()
|
||||
|
||||
// Get the data
|
||||
|
@ -160,6 +166,7 @@ func Download(filepath string, url string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Check server response
|
||||
|
|
|
@ -18,15 +18,15 @@ type IgnoreItem struct {
|
|||
// NewIgnoreItem returns a pointer to a new IgnoreItem instance.
|
||||
func NewIgnoreItem(dir, pattern string, caseSensitive bool) IgnoreItem {
|
||||
if caseSensitive {
|
||||
return IgnoreItem{Dir: dir + "/", Pattern: pattern}
|
||||
return IgnoreItem{Dir: dir + PathSeparator, Pattern: pattern}
|
||||
} else {
|
||||
return IgnoreItem{Dir: strings.ToLower(dir) + "/", Pattern: strings.ToLower(pattern)}
|
||||
return IgnoreItem{Dir: strings.ToLower(dir) + PathSeparator, Pattern: strings.ToLower(pattern)}
|
||||
}
|
||||
}
|
||||
|
||||
// Ignore returns true if the file name "base" in the directory "dir" should be ignored.
|
||||
func (i IgnoreItem) Ignore(dir, base string) bool {
|
||||
if !strings.HasPrefix(dir+"/", i.Dir) {
|
||||
if !strings.HasPrefix(dir+PathSeparator, i.Dir) {
|
||||
// different directory prefix: don't look any further
|
||||
return false
|
||||
} else if i.Pattern == base {
|
||||
|
|
Loading…
Reference in New Issue
Block a user