@@ -46,10 +46,15 @@ func readPseudoAlbum(collection *Collection, album *Album) ([]PseudoAlbumEntry,
4646 entries := make ([]PseudoAlbumEntry , 0 )
4747 for scanner .Scan () {
4848 line := scanner .Text ()
49+ // Skip empty lines and comments
50+ if len (line ) == 0 || strings .HasPrefix (line , "#" ) {
51+ continue
52+ }
53+ // Split line into fields
4954 split := strings .Split (line , ":" )
5055 if len (split ) != 3 {
51- log .Println ("The following line is not formatted correctly:" , line )
52- break
56+ log .Println ("Line is not formatted correctly:" , line )
57+ continue
5358 }
5459 // Decompose slice
5560 collection , album , photo := split [0 ], split [1 ], strings .ToLower (split [2 ])
@@ -79,6 +84,42 @@ func writePseudoAlbum(collection *Collection, album *Album, entries ...PseudoAlb
7984 return nil
8085}
8186
87+ // If the album is a pseudo album, it will resolve the reference to the source photos
88+ func resolveQueryPhotos (query PseudoAlbumSaveQuery ) (list []PseudoAlbumEntry , err error ) {
89+ collection , err := GetCollection (query .Collection )
90+ if err != nil {
91+ return nil , err
92+ }
93+
94+ album , err := collection .GetAlbum (query .Album )
95+ if err != nil {
96+ return nil , err
97+ }
98+
99+ // If it is a pseudo album, we need to resolve the links to the photos
100+ if album .IsPseudo {
101+ entries , err := readPseudoAlbum (collection , album )
102+ if err != nil {
103+ return nil , err
104+ }
105+ for _ , photo := range query .Photos {
106+ for _ , entry := range entries {
107+ if entry .Photo == photo {
108+ list = append (list , entry )
109+ break
110+ }
111+ }
112+ }
113+ } else {
114+ // If not, just convert the list of photos to PseudoAlbumEntry
115+ for _ , photo := range query .Photos {
116+ list = append (list , PseudoAlbumEntry {Collection : query .Collection , Album : query .Album , Photo : photo })
117+ }
118+ }
119+
120+ return
121+ }
122+
82123func GetPseudoAlbums (collections map [string ]* Collection ) []PseudoAlbum {
83124 pseudos := make ([]PseudoAlbum , 0 )
84125
@@ -95,36 +136,52 @@ func GetPseudoAlbums(collections map[string]*Collection) []PseudoAlbum {
95136}
96137
97138func (album * Album ) EditPseudoAlbum (collection * Collection , query PseudoAlbumSaveQuery , isAdd bool ) error {
139+ // Read entries from target album
98140 entries , err := readPseudoAlbum (collection , album )
99141 if err != nil {
100142 return err
101143 }
102144
103- var updated []string
104- for _ , photo := range query .Photos {
145+ // Resolve links for pseudo albums
146+ editPhotos , err := resolveQueryPhotos (query )
147+ if err != nil {
148+ return err
149+ }
150+
151+ updated := make (map [string ]PseudoAlbumSaveQuery )
152+ errs := make ([]string , 0 )
153+ for _ , edit := range editPhotos {
105154 // Find entry already in the album
106155 found := - 1
107156 for i , entry := range entries {
108- if entry .Collection == query .Collection && entry .Album == query .Album && entry .Photo == photo {
157+ if entry .Collection == edit .Collection && entry .Album == edit .Album && entry .Photo == edit . Photo {
109158 found = i
110159 break
111160 }
112161 }
113162
114163 if isAdd {
115- if found >= 0 { // Duplicated entry
116- return errors .New ("entry duplicated" )
164+ if found < 0 {
165+ // Add a new entry
166+ entries = append (entries , edit )
167+ } else {
168+ errs = append (errs , "entry duplicated: " + edit .Photo )
117169 }
118- // Add a new entry
119- entries = append (entries , PseudoAlbumEntry {Collection : query .Collection , Album : query .Album , Photo : photo })
120- updated = append (updated , photo )
121170 } else {
122- if found < 0 {
123- return errors .New ("entry could not be found" )
171+ if found >= 0 {
172+ // Remove the entry
173+ entries = append (entries [:found ], entries [found + 1 :]... )
174+ } else {
175+ errs = append (errs , "entry could not be found: " + edit .Photo )
124176 }
125- // Remove the entry
126- entries = append (entries [:found ], entries [found + 1 :]... )
127- updated = append (updated , photo )
177+ }
178+
179+ // Add photo to the list of updated photos
180+ key := edit .Collection + ":" + edit .Album
181+ if _ , ok := updated [key ]; ! ok {
182+ updated [key ] = PseudoAlbumSaveQuery {Collection : edit .Collection , Album : edit .Album , Photos : []string {edit .Photo }}
183+ } else {
184+ updated [key ] = PseudoAlbumSaveQuery {Collection : edit .Collection , Album : edit .Album , Photos : append (updated [key ].Photos , edit .Photo )}
128185 }
129186 }
130187
@@ -135,37 +192,46 @@ func (album *Album) EditPseudoAlbum(collection *Collection, query PseudoAlbumSav
135192 }
136193
137194 // Update in background cached entries that were changed
138- go func () error {
139- fromCollection , err := GetCollection (query .Collection )
140- if err != nil {
141- return err
142- }
143- fromAlbum , err := fromCollection .GetAlbumWithPhotos (query .Album , false )
144- if err != nil {
145- return err
146- }
147-
148- var photos []* Photo
149- for _ , photo := range updated {
150- fromPhoto , err := fromAlbum .GetPhoto (photo )
195+ go func () {
196+ for _ , entry := range updated {
197+ fromCollection , err := GetCollection (entry .Collection )
151198 if err != nil {
199+ log .Println (err )
200+ continue
201+ }
202+ fromAlbum , err := fromCollection .GetAlbumWithPhotos (entry .Album , false )
203+ if err != nil {
204+ log .Println (err )
152205 continue
153206 }
154207
155- result := false
156- if isAdd { // Add link to the album
157- result = fromPhoto .AddFavorite (collection , album )
158- } else { // Remove link to the album
159- result = fromPhoto .RemoveFavorite (collection , album )
208+ var photos []* Photo
209+ for _ , photo := range entry .Photos {
210+ fromPhoto , err := fromAlbum .GetPhoto (photo )
211+ if err != nil {
212+ continue
213+ }
214+
215+ result := false
216+ if isAdd { // Add link to the album
217+ result = fromPhoto .AddFavorite (collection , album )
218+ } else { // Remove link to the album
219+ result = fromPhoto .RemoveFavorite (collection , album )
220+ }
221+ if result {
222+ photos = append (photos , fromPhoto )
223+ }
160224 }
161- if result {
162- photos = append (photos , fromPhoto )
225+ // Update info about cached photos
226+ err = fromCollection .cache .AddPhotoInfo (fromAlbum , photos ... )
227+ if err != nil {
228+ log .Println (err )
163229 }
164230 }
165-
166- // Update info about cached photos
167- return fromCollection .cache .AddPhotoInfo (fromAlbum , photos ... )
168231 }()
169232
233+ if len (errs ) > 0 {
234+ return errors .New (strings .Join (errs , "\n " ))
235+ }
170236 return nil
171237}
0 commit comments