@@ -147,49 +147,64 @@ class DownloadUtility {
147
147
}
148
148
149
149
private static func downloadImages( for questions: [ [ String : Any ] ] , course: Course , progressHandler: @escaping ( Int , Int ) -> Void ) throws -> [ [ String : Any ] ] {
150
- let documentsURL = FileManager . default. urls ( for: . documentDirectory, in: . userDomainMask) . first!
151
- let imagesDirectoryURL = documentsURL. appendingPathComponent ( " images/ \( course. shortName) " )
152
-
153
- try FileManager . default. createDirectory ( at: imagesDirectoryURL, withIntermediateDirectories: true , attributes: nil )
154
-
155
- var updatedQuestions = questions
156
-
157
- let totalImages = questions. reduce ( 0 ) { count, question in
158
- count + ( ( question [ " images " ] as? [ [ String : Any ] ] ) ? . count ?? 0 )
159
- }
160
-
161
- var downloadedImages = 0
150
+ let documentsURL = FileManager . default. urls ( for: . documentDirectory, in: . userDomainMask) . first!
151
+ let imagesDirectoryURL = documentsURL. appendingPathComponent ( " images/ \( course. shortName) " )
162
152
163
- for (index, question) in questions. enumerated ( ) {
164
- if let imagePaths = question [ " images " ] as? [ [ String : Any ] ] {
165
- var updatedImagePaths : [ [ String : Any ] ] = [ ]
166
- for imagePath in imagePaths {
167
- if let path = imagePath [ " path " ] as? String {
168
- let imageUrlString = " \( course. repositoryURL) /blob/main/ \( path. replacingOccurrences ( of: " images/ \( course. shortName) / " , with: " " ) ) " . replacingOccurrences ( of: " github.com " , with: " raw.githubusercontent.com " ) . replacingOccurrences ( of: " /blob/ " , with: " / " )
169
- if let imageUrl = URL ( string: imageUrlString) {
170
- let imageData = try Data ( contentsOf: imageUrl)
171
- let imageFileName = path. replacingOccurrences ( of: " images/ \( course. shortName) / " , with: " " )
172
- let imageFileURL = imagesDirectoryURL. appendingPathComponent ( imageFileName)
153
+ try FileManager . default. createDirectory ( at: imagesDirectoryURL, withIntermediateDirectories: true , attributes: nil )
173
154
174
- let imageFileDirectory = imageFileURL. deletingLastPathComponent ( )
175
- try FileManager . default. createDirectory ( at: imageFileDirectory, withIntermediateDirectories: true , attributes: nil )
155
+ var updatedQuestions = questions
176
156
177
- try FileManager . default. removeItemIfExists ( at: imageFileURL)
178
- try imageData. write ( to: imageFileURL)
157
+ let totalImages = questions. reduce ( 0 ) { count, question in
158
+ count + ( ( question [ " images " ] as? [ [ String : Any ] ] ) ? . count ?? 0 )
159
+ }
179
160
180
- updatedImagePaths. append ( [ " path " : " images/ \( course. shortName) / \( imageFileName) " , " url " : imageUrl. absoluteString, " downloaded " : true ] )
181
- downloadedImages += 1
182
- progressHandler ( downloadedImages, totalImages)
161
+ var downloadedImages = 0
162
+
163
+ for (index, question) in questions. enumerated ( ) {
164
+ if let imagePaths = question [ " images " ] as? [ [ String : Any ] ] {
165
+ var updatedImagePaths : [ [ String : Any ] ] = [ ]
166
+ for imagePath in imagePaths {
167
+ if let path = imagePath [ " path " ] as? String {
168
+ let imageUrlStringMain = " \( course. repositoryURL) /blob/main/ \( path. replacingOccurrences ( of: " images/ \( course. shortName) / " , with: " " ) ) " . replacingOccurrences ( of: " github.com " , with: " raw.githubusercontent.com " ) . replacingOccurrences ( of: " /blob/ " , with: " / " )
169
+ let imageUrlStringMaster = " \( course. repositoryURL) /blob/master/ \( path. replacingOccurrences ( of: " images/ \( course. shortName) / " , with: " " ) ) " . replacingOccurrences ( of: " github.com " , with: " raw.githubusercontent.com " ) . replacingOccurrences ( of: " /blob/ " , with: " / " )
170
+
171
+ if let imageUrlMain = URL ( string: imageUrlStringMain) , let imageUrlMaster = URL ( string: imageUrlStringMaster) {
172
+ var imageUrl : URL ? = nil
173
+ do {
174
+ _ = try Data ( contentsOf: imageUrlMain)
175
+ imageUrl = imageUrlMain
176
+ } catch {
177
+ _ = try ? Data ( contentsOf: imageUrlMaster)
178
+ imageUrl = imageUrlMaster
179
+ }
180
+
181
+ if let imageUrl = imageUrl {
182
+ print ( " Downloading image: \( imageUrl) " )
183
+ let imageData = try Data ( contentsOf: imageUrl)
184
+ let imageFileName = path. replacingOccurrences ( of: " images/ \( course. shortName) / " , with: " " )
185
+ let imageFileURL = imagesDirectoryURL. appendingPathComponent ( imageFileName)
186
+
187
+ let imageFileDirectory = imageFileURL. deletingLastPathComponent ( )
188
+ try FileManager . default. createDirectory ( at: imageFileDirectory, withIntermediateDirectories: true , attributes: nil )
189
+
190
+ try FileManager . default. removeItemIfExists ( at: imageFileURL)
191
+ try imageData. write ( to: imageFileURL)
192
+
193
+ updatedImagePaths. append ( [ " path " : " images/ \( course. shortName) / \( imageFileName) " , " url " : imageUrl. absoluteString, " downloaded " : true ] )
194
+ downloadedImages += 1
195
+ progressHandler ( downloadedImages, totalImages)
196
+ }
197
+ }
183
198
}
184
199
}
200
+ updatedQuestions [ index] [ " images " ] = updatedImagePaths
185
201
}
186
- updatedQuestions [ index] [ " images " ] = updatedImagePaths
187
202
}
188
- }
189
203
190
- return updatedQuestions
204
+ return updatedQuestions
205
+ }
191
206
}
192
- }
207
+
193
208
194
209
extension FileManager {
195
210
func removeItemIfExists( at url: URL ) throws {
0 commit comments