Skip to content

Commit cc68d3c

Browse files
dazhijionghugo-saciHex4C
authored andcommitted
feat:implement-include-type (#10)
* feat:implement-include-type * bugfix:deal with duplicate extensions * bugfix:extract code from prerun * bugfix:import DefaultSupportedMedia from supported.go * feat:use set function instead of prerun * refactor: Implement IncludeType A type IncludeType has been implemented as the type for the possible values for the include-type flag. The conversion to extensions to include is done in a PreRun function. A function has been added to get the DefaultSupportedMedia as a map from a media type to a list of extensions. * docs: Add documentation for functions and reorder code Add simple explanation for funciton calls and reorder to keep the types at the top to introduce the types before the user reads them. Also document undocumented public functions or functions with importance to reduce code complexity overhead. close issue#9 #5 --------- Co-authored-by: Hugo Sacilotto <[email protected]> Co-authored-by: Hex4C <[email protected]>
1 parent c80672b commit cc68d3c

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

internal/cliFlags/extensionList.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,54 @@
11
package cliflags
22

33
import (
4+
"fmt"
45
"slices"
56
"strings"
67

8+
"github.com/simulot/immich-go/internal/filetypes"
79
"github.com/spf13/cobra"
810
)
911

1012
type InclusionFlags struct {
1113
ExcludedExtensions ExtensionList
1214
IncludedExtensions ExtensionList
15+
IncludedType IncludeType
1316
DateRange DateRange
1417
}
1518

19+
// An IncludeType is either of the constants below which
20+
// represents a collection of extensions.
21+
type IncludeType string
22+
23+
const (
24+
IncludeAll IncludeType = ""
25+
IncludeVideo IncludeType = "VIDEO"
26+
IncludeImage IncludeType = "IMAGE"
27+
)
28+
1629
func AddInclusionFlags(cmd *cobra.Command, flags *InclusionFlags) {
1730
cmd.Flags().Var(&flags.DateRange, "date-range", "Only import photos taken within the specified date range")
1831
cmd.Flags().Var(&flags.ExcludedExtensions, "exclude-extensions", "Comma-separated list of extension to exclude. (e.g. .gif,.PM) (default: none)")
1932
cmd.Flags().Var(&flags.IncludedExtensions, "include-extensions", "Comma-separated list of extension to include. (e.g. .jpg,.heic) (default: all)")
33+
cmd.Flags().Var(&flags.IncludedType, "include-type", "Single file type to include. (VIDEO or IMAGE) (default: all)")
34+
cmd.PreRun = func(cmd *cobra.Command, args []string) {
35+
if cmd.Flags().Changed("include-type") {
36+
setIncludeTypeExtensions(flags)
37+
}
38+
}
39+
}
40+
41+
// Add the approprite extensions flags given the user inclusion flag
42+
func setIncludeTypeExtensions(flags *InclusionFlags) {
43+
mediaToExtensionsMap := filetypes.MediaToExtensions()
44+
45+
switch flags.IncludedType {
46+
case IncludeVideo:
47+
flags.IncludedExtensions = append(flags.IncludedExtensions, mediaToExtensionsMap[filetypes.TypeVideo]...)
48+
case IncludeImage:
49+
flags.IncludedExtensions = append(flags.IncludedExtensions, mediaToExtensionsMap[filetypes.TypeImage]...)
50+
}
51+
flags.IncludedExtensions = append(flags.IncludedExtensions, mediaToExtensionsMap[filetypes.TypeSidecar]...)
2052
}
2153

2254
// Validate validates the common flags.
@@ -25,6 +57,26 @@ func (flags *InclusionFlags) Validate() {
2557
flags.IncludedExtensions = flags.IncludedExtensions.Validate()
2658
}
2759

60+
// Implements the flag interface
61+
func (t *IncludeType) Set(v string) error {
62+
v = strings.TrimSpace(strings.ToUpper(v))
63+
switch v {
64+
case string(IncludeVideo), string(IncludeImage):
65+
*t = IncludeType(v)
66+
default:
67+
return fmt.Errorf("invalid value for include type, expected %s or %s", IncludeVideo, IncludeImage)
68+
}
69+
return nil
70+
}
71+
72+
func (t IncludeType) String() string {
73+
return string(t)
74+
}
75+
76+
func (t IncludeType) Type() string {
77+
return "IncludeType"
78+
}
79+
2880
// An ExtensionList is a list of file extensions, where each extension is a string that starts with a dot (.) and is in lowercase.
2981
type ExtensionList []string
3082

internal/filetypes/supported.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ func (sm SupportedMedia) IsIgnoredExt(ext string) bool {
7373
return t == ""
7474
}
7575

76+
// MediaToExtensions defines a map from mediaType to mediaExtensions
77+
// returns the map with the format map[mediatype] = extensions
78+
func MediaToExtensions() map[string][]string {
79+
reversedMap := make(map[string][]string)
80+
81+
for ext, mediaType := range DefaultSupportedMedia {
82+
reversedMap[mediaType] = append(reversedMap[mediaType], ext)
83+
}
84+
85+
return reversedMap
86+
}
87+
7688
// rawExtensions defines the supported RAW file extensions
7789
// https://github.com/immich-app/immich/blob/39b571a95c99cbc4183e5d389e6d682cd8e903d9/server/src/utils/mime-types.ts#L1-L55
7890
// source: https://en.wikipedia.org/wiki/Raw_image_format

0 commit comments

Comments
 (0)