2020-04-07 10:42:42 +02:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
_ "image/gif" // Import for image.
|
|
|
|
_ "image/jpeg"
|
|
|
|
_ "image/png"
|
2020-04-22 16:39:45 +02:00
|
|
|
"os"
|
2020-04-07 10:42:42 +02:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-12-12 17:20:31 +01:00
|
|
|
type FileFormat string
|
2020-04-07 10:42:42 +02:00
|
|
|
|
|
|
|
const (
|
2020-12-12 17:20:31 +01:00
|
|
|
FormatJpeg FileFormat = "jpg" // JPEG image file.
|
|
|
|
FormatPng FileFormat = "png" // PNG image file.
|
|
|
|
FormatGif FileFormat = "gif" // GIF image file.
|
|
|
|
FormatTiff FileFormat = "tiff" // TIFF image file.
|
|
|
|
FormatBitmap FileFormat = "bmp" // BMP image file.
|
|
|
|
FormatRaw FileFormat = "raw" // RAW image file.
|
2022-04-02 18:04:02 +02:00
|
|
|
FormatMpo FileFormat = "mpo" // Stereoscopic Image that consists of two JPG images that are combined into one 3D image
|
2020-12-12 17:20:31 +01:00
|
|
|
FormatHEIF FileFormat = "heif" // High Efficiency Image File Format
|
2022-04-02 18:04:02 +02:00
|
|
|
FormatWebP FileFormat = "webp" // Google WebP Image
|
|
|
|
FormatWebM FileFormat = "webm" // Google WebM Video
|
|
|
|
FormatHEVC FileFormat = "hevc" // H.265, High Efficiency Video Coding (HEVC)
|
|
|
|
FormatAvc FileFormat = "avc" // H.264, Advanced Video Coding (AVC), MPEG-4 Part 10, used internally
|
|
|
|
FormatMov FileFormat = "mov" // QuickTime File Format, can contain AVC, HEVC,...
|
|
|
|
FormatMp4 FileFormat = "mp4" // Standard MPEG-4 Container based on QuickTime, can contain AVC, HEVC,...
|
|
|
|
FormatAvi FileFormat = "avi" // Microsoft Audio Video Interleave (AVI)
|
|
|
|
Format3gp FileFormat = "3gp" // Mobile Multimedia Container Format, MPEG-4 Part 12
|
|
|
|
Format3g2 FileFormat = "3g2" // Similar to 3GP, consumes less space & bandwidth
|
|
|
|
FormatFlv FileFormat = "flv" // Flash Video
|
|
|
|
FormatMkv FileFormat = "mkv" // Matroska Multimedia Container, free and open
|
|
|
|
FormatMpg FileFormat = "mpg" // Moving Picture Experts Group (MPEG)
|
|
|
|
FormatMts FileFormat = "mts" // AVCHD (Advanced Video Coding High Definition)
|
|
|
|
FormatOgv FileFormat = "ogv" // Ogg container format maintained by the Xiph.Org, free and open
|
|
|
|
FormatWMV FileFormat = "wmv" // Windows Media Video
|
2020-12-12 17:20:31 +01:00
|
|
|
FormatXMP FileFormat = "xmp" // Adobe XMP sidecar file (XML).
|
|
|
|
FormatAAE FileFormat = "aae" // Apple sidecar file (XML).
|
|
|
|
FormatXML FileFormat = "xml" // XML metadata / config / sidecar file.
|
|
|
|
FormatYaml FileFormat = "yml" // YAML metadata / config / sidecar file.
|
|
|
|
FormatToml FileFormat = "toml" // Tom's Obvious, Minimal Language sidecar file.
|
|
|
|
FormatJson FileFormat = "json" // JSON metadata / config / sidecar file.
|
|
|
|
FormatText FileFormat = "txt" // Text config / sidecar file.
|
|
|
|
FormatMarkdown FileFormat = "md" // Markdown text sidecar file.
|
|
|
|
FormatOther FileFormat = "" // Unknown file format.
|
2020-04-07 10:42:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// FileExt contains the filename extensions of file formats known to PhotoPrism.
|
2020-04-22 16:39:45 +02:00
|
|
|
var FileExt = FileExtensions{
|
2020-12-12 17:20:31 +01:00
|
|
|
".bmp": FormatBitmap,
|
|
|
|
".gif": FormatGif,
|
|
|
|
".tif": FormatTiff,
|
|
|
|
".tiff": FormatTiff,
|
|
|
|
".png": FormatPng,
|
|
|
|
".pn": FormatPng,
|
|
|
|
".crw": FormatRaw,
|
|
|
|
".cr2": FormatRaw,
|
|
|
|
".cr3": FormatRaw,
|
|
|
|
".nef": FormatRaw,
|
|
|
|
".arw": FormatRaw,
|
|
|
|
".dng": FormatRaw,
|
|
|
|
".mov": FormatMov,
|
|
|
|
".avi": FormatAvi,
|
2021-08-29 16:26:20 +02:00
|
|
|
".mp": FormatMp4,
|
2020-12-12 17:20:31 +01:00
|
|
|
".mp4": FormatMp4,
|
2021-02-15 13:02:55 +01:00
|
|
|
".m4v": FormatMp4,
|
2020-12-12 17:20:31 +01:00
|
|
|
".avc": FormatAvc,
|
|
|
|
".hevc": FormatHEVC,
|
|
|
|
".3gp": Format3gp,
|
|
|
|
".3g2": Format3g2,
|
|
|
|
".flv": FormatFlv,
|
|
|
|
".mkv": FormatMkv,
|
|
|
|
".mpg": FormatMpg,
|
|
|
|
".mpeg": FormatMpg,
|
2021-02-14 19:25:52 +01:00
|
|
|
".mpo": FormatMpo,
|
|
|
|
".mts": FormatMts,
|
2020-12-12 17:20:31 +01:00
|
|
|
".ogv": FormatOgv,
|
2022-04-02 18:04:02 +02:00
|
|
|
".webp": FormatWebP,
|
|
|
|
".webm": FormatWebM,
|
2020-12-12 17:20:31 +01:00
|
|
|
".wmv": FormatWMV,
|
|
|
|
".yml": FormatYaml,
|
|
|
|
".yaml": FormatYaml,
|
|
|
|
".jpg": FormatJpeg,
|
|
|
|
".jpeg": FormatJpeg,
|
|
|
|
".jpe": FormatJpeg,
|
|
|
|
".jif": FormatJpeg,
|
|
|
|
".jfif": FormatJpeg,
|
|
|
|
".jfi": FormatJpeg,
|
|
|
|
".thm": FormatJpeg,
|
|
|
|
".xmp": FormatXMP,
|
|
|
|
".aae": FormatAAE,
|
|
|
|
".heif": FormatHEIF,
|
|
|
|
".heic": FormatHEIF,
|
|
|
|
".3fr": FormatRaw,
|
|
|
|
".ari": FormatRaw,
|
|
|
|
".bay": FormatRaw,
|
|
|
|
".cap": FormatRaw,
|
|
|
|
".data": FormatRaw,
|
|
|
|
".dcs": FormatRaw,
|
|
|
|
".dcr": FormatRaw,
|
|
|
|
".drf": FormatRaw,
|
|
|
|
".eip": FormatRaw,
|
|
|
|
".erf": FormatRaw,
|
|
|
|
".fff": FormatRaw,
|
|
|
|
".gpr": FormatRaw,
|
|
|
|
".iiq": FormatRaw,
|
|
|
|
".k25": FormatRaw,
|
|
|
|
".kdc": FormatRaw,
|
|
|
|
".mdc": FormatRaw,
|
|
|
|
".mef": FormatRaw,
|
|
|
|
".mos": FormatRaw,
|
|
|
|
".mrw": FormatRaw,
|
|
|
|
".nrw": FormatRaw,
|
|
|
|
".obm": FormatRaw,
|
|
|
|
".orf": FormatRaw,
|
|
|
|
".pef": FormatRaw,
|
|
|
|
".ptx": FormatRaw,
|
|
|
|
".pxn": FormatRaw,
|
|
|
|
".r3d": FormatRaw,
|
|
|
|
".raf": FormatRaw,
|
|
|
|
".raw": FormatRaw,
|
|
|
|
".rwl": FormatRaw,
|
|
|
|
".rw2": FormatRaw,
|
|
|
|
".rwz": FormatRaw,
|
|
|
|
".sr2": FormatRaw,
|
|
|
|
".srf": FormatRaw,
|
|
|
|
".srw": FormatRaw,
|
|
|
|
".x3f": FormatRaw,
|
|
|
|
".xml": FormatXML,
|
|
|
|
".txt": FormatText,
|
|
|
|
".md": FormatMarkdown,
|
|
|
|
".json": FormatJson,
|
2020-04-07 10:42:42 +02:00
|
|
|
}
|
|
|
|
|
2020-07-11 16:46:29 +02:00
|
|
|
func (m FileExtensions) Known(name string) bool {
|
|
|
|
if name == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
ext := strings.ToLower(filepath.Ext(name))
|
|
|
|
|
|
|
|
if ext == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := m[ext]; ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-04-22 16:39:45 +02:00
|
|
|
func (m FileExtensions) TypeExt() TypeExtensions {
|
|
|
|
result := make(TypeExtensions)
|
|
|
|
|
2020-12-26 18:06:54 +01:00
|
|
|
if ignoreCase {
|
|
|
|
for ext, t := range m {
|
|
|
|
if _, ok := result[t]; ok {
|
|
|
|
result[t] = append(result[t], ext)
|
|
|
|
} else {
|
|
|
|
result[t] = []string{ext}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for ext, t := range m {
|
|
|
|
extUpper := strings.ToUpper(ext)
|
|
|
|
if _, ok := result[t]; ok {
|
|
|
|
result[t] = append(result[t], ext, extUpper)
|
|
|
|
} else {
|
|
|
|
result[t] = []string{ext, extUpper}
|
|
|
|
}
|
2020-04-22 16:39:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
var TypeExt = FileExt.TypeExt()
|
|
|
|
|
|
|
|
// Find returns the first filename with the same base name and a given type.
|
2020-12-12 17:20:31 +01:00
|
|
|
func (t FileFormat) Find(fileName string, stripSequence bool) string {
|
2020-07-14 11:00:49 +02:00
|
|
|
base := BasePrefix(fileName, stripSequence)
|
2020-04-22 16:39:45 +02:00
|
|
|
dir := filepath.Dir(fileName)
|
|
|
|
|
|
|
|
prefix := filepath.Join(dir, base)
|
|
|
|
prefixLower := filepath.Join(dir, strings.ToLower(base))
|
|
|
|
prefixUpper := filepath.Join(dir, strings.ToUpper(base))
|
|
|
|
|
|
|
|
for _, ext := range TypeExt[t] {
|
|
|
|
if info, err := os.Stat(prefix + ext); err == nil && info.Mode().IsRegular() {
|
|
|
|
return filepath.Join(dir, info.Name())
|
|
|
|
}
|
|
|
|
|
2020-12-26 18:06:54 +01:00
|
|
|
if ignoreCase {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-04-22 16:39:45 +02:00
|
|
|
if info, err := os.Stat(prefixLower + ext); err == nil && info.Mode().IsRegular() {
|
|
|
|
return filepath.Join(dir, info.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
if info, err := os.Stat(prefixUpper + ext); err == nil && info.Mode().IsRegular() {
|
|
|
|
return filepath.Join(dir, info.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-12-12 17:20:31 +01:00
|
|
|
// GetFileFormat returns the (expected) type for a given file name.
|
|
|
|
func GetFileFormat(fileName string) FileFormat {
|
2020-04-07 10:42:42 +02:00
|
|
|
fileExt := strings.ToLower(filepath.Ext(fileName))
|
|
|
|
result, ok := FileExt[fileExt]
|
|
|
|
|
|
|
|
if !ok {
|
2020-12-12 17:20:31 +01:00
|
|
|
result = FormatOther
|
2020-04-07 10:42:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
2020-06-07 10:09:35 +02:00
|
|
|
|
|
|
|
// FindFirst searches a list of directories for the first file with the same base name and a given type.
|
2020-12-12 17:20:31 +01:00
|
|
|
func (t FileFormat) FindFirst(fileName string, dirs []string, baseDir string, stripSequence bool) string {
|
2020-12-12 13:05:58 +01:00
|
|
|
fileBase := filepath.Base(fileName)
|
|
|
|
fileBasePrefix := BasePrefix(fileName, stripSequence)
|
|
|
|
fileBaseLower := strings.ToLower(fileBasePrefix)
|
|
|
|
fileBaseUpper := strings.ToUpper(fileBasePrefix)
|
2020-06-07 10:09:35 +02:00
|
|
|
|
|
|
|
fileDir := filepath.Dir(fileName)
|
|
|
|
search := append([]string{fileDir}, dirs...)
|
|
|
|
|
|
|
|
for _, ext := range TypeExt[t] {
|
|
|
|
lastDir := ""
|
|
|
|
|
|
|
|
for _, dir := range search {
|
|
|
|
if dir == "" || dir == lastDir {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
lastDir = dir
|
|
|
|
|
|
|
|
if dir != fileDir {
|
|
|
|
if filepath.IsAbs(dir) {
|
2020-07-14 11:00:49 +02:00
|
|
|
dir = filepath.Join(dir, RelName(fileDir, baseDir))
|
2020-06-07 10:09:35 +02:00
|
|
|
} else {
|
|
|
|
dir = filepath.Join(fileDir, dir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if info, err := os.Stat(filepath.Join(dir, fileBase) + ext); err == nil && info.Mode().IsRegular() {
|
2020-12-26 18:06:54 +01:00
|
|
|
return filepath.Join(dir, info.Name())
|
2020-12-25 20:29:06 +01:00
|
|
|
} else if info, err := os.Stat(filepath.Join(dir, fileBasePrefix) + ext); err == nil && info.Mode().IsRegular() {
|
2020-12-26 18:06:54 +01:00
|
|
|
return filepath.Join(dir, info.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
if ignoreCase {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if info, err := os.Stat(filepath.Join(dir, fileBaseLower) + ext); err == nil && info.Mode().IsRegular() {
|
|
|
|
return filepath.Join(dir, info.Name())
|
2020-12-25 20:29:06 +01:00
|
|
|
} else if info, err := os.Stat(filepath.Join(dir, fileBaseUpper) + ext); err == nil && info.Mode().IsRegular() {
|
2020-12-26 18:06:54 +01:00
|
|
|
return filepath.Join(dir, info.Name())
|
2020-06-07 10:09:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
2020-09-20 15:12:45 +02:00
|
|
|
|
2020-12-12 13:05:58 +01:00
|
|
|
// FindAll searches a list of directories for files with the same base name and a given type.
|
2020-12-12 17:20:31 +01:00
|
|
|
func (t FileFormat) FindAll(fileName string, dirs []string, baseDir string, stripSequence bool) (results []string) {
|
2020-12-12 13:05:58 +01:00
|
|
|
fileBase := filepath.Base(fileName)
|
|
|
|
fileBasePrefix := BasePrefix(fileName, stripSequence)
|
|
|
|
fileBaseLower := strings.ToLower(fileBasePrefix)
|
|
|
|
fileBaseUpper := strings.ToUpper(fileBasePrefix)
|
|
|
|
|
|
|
|
fileDir := filepath.Dir(fileName)
|
|
|
|
search := append([]string{fileDir}, dirs...)
|
|
|
|
|
|
|
|
for _, ext := range TypeExt[t] {
|
|
|
|
lastDir := ""
|
|
|
|
|
|
|
|
for _, dir := range search {
|
|
|
|
if dir == "" || dir == lastDir {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
lastDir = dir
|
|
|
|
|
|
|
|
if dir != fileDir {
|
|
|
|
if filepath.IsAbs(dir) {
|
|
|
|
dir = filepath.Join(dir, RelName(fileDir, baseDir))
|
|
|
|
} else {
|
|
|
|
dir = filepath.Join(fileDir, dir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if info, err := os.Stat(filepath.Join(dir, fileBase) + ext); err == nil && info.Mode().IsRegular() {
|
2020-12-26 18:06:54 +01:00
|
|
|
results = append(results, filepath.Join(dir, info.Name()))
|
2020-12-12 13:05:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if info, err := os.Stat(filepath.Join(dir, fileBasePrefix) + ext); err == nil && info.Mode().IsRegular() {
|
2020-12-26 18:06:54 +01:00
|
|
|
results = append(results, filepath.Join(dir, info.Name()))
|
|
|
|
}
|
|
|
|
|
|
|
|
if ignoreCase {
|
|
|
|
continue
|
2020-12-12 13:05:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if info, err := os.Stat(filepath.Join(dir, fileBaseLower) + ext); err == nil && info.Mode().IsRegular() {
|
2020-12-26 18:06:54 +01:00
|
|
|
results = append(results, filepath.Join(dir, info.Name()))
|
2020-12-12 13:05:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if info, err := os.Stat(filepath.Join(dir, fileBaseUpper) + ext); err == nil && info.Mode().IsRegular() {
|
2020-12-26 18:06:54 +01:00
|
|
|
results = append(results, filepath.Join(dir, info.Name()))
|
2020-12-12 13:05:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results
|
|
|
|
}
|