settings section
This commit is contained in:
35
internal/admin/auth/files.go
Normal file
35
internal/admin/auth/files.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func imageStorageDir(publicRoot string) string {
|
||||
return filepath.Join(publicRoot, "images")
|
||||
}
|
||||
|
||||
func imagePublicPath(filename string) string {
|
||||
return "/images/" + filename
|
||||
}
|
||||
|
||||
func resolveImagePath(publicRoot, filename string) (string, error) {
|
||||
if filename == "" || strings.Contains(filename, "..") || strings.ContainsAny(filename, `/\`) {
|
||||
return "", fmt.Errorf("invalid file name")
|
||||
}
|
||||
|
||||
base := filepath.Clean(imageStorageDir(publicRoot))
|
||||
path := filepath.Clean(filepath.Join(base, filepath.Base(filename)))
|
||||
if !strings.HasPrefix(path, base+string(os.PathSeparator)) && path != base {
|
||||
return "", fmt.Errorf("invalid file path")
|
||||
}
|
||||
|
||||
return path, nil
|
||||
}
|
||||
|
||||
func newUploadFilename(original string) string {
|
||||
return fmt.Sprintf("%d%s", time.Now().UnixNano(), filepath.Ext(original))
|
||||
}
|
||||
Reference in New Issue
Block a user