Skip to content

Commit 3eefa56

Browse files
author
Benedikt Wagner
committed
download images from master if main does not exist
1 parent 67aed1c commit 3eefa56

File tree

4 files changed

+54
-39
lines changed

4 files changed

+54
-39
lines changed

CloudMaster.xcodeproj/project.pbxproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@
775775
CLANG_ENABLE_MODULES = YES;
776776
CODE_SIGN_IDENTITY = "Apple Development";
777777
CODE_SIGN_STYLE = Automatic;
778-
CURRENT_PROJECT_VERSION = 3;
778+
CURRENT_PROJECT_VERSION = 4;
779779
DEVELOPMENT_ASSET_PATHS = "\"CloudMaster/Preview Content\"";
780780
DEVELOPMENT_TEAM = 9D3QHQ7CMS;
781781
ENABLE_PREVIEWS = YES;
@@ -810,7 +810,7 @@
810810
CLANG_ENABLE_MODULES = YES;
811811
CODE_SIGN_IDENTITY = "Apple Development";
812812
CODE_SIGN_STYLE = Automatic;
813-
CURRENT_PROJECT_VERSION = 3;
813+
CURRENT_PROJECT_VERSION = 4;
814814
DEVELOPMENT_ASSET_PATHS = "\"CloudMaster/Preview Content\"";
815815
DEVELOPMENT_TEAM = 9D3QHQ7CMS;
816816
ENABLE_PREVIEWS = YES;

CloudMaster/Constants/Courses.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@ extension Course {
318318
lastUpdate: nil
319319
),
320320
Course(
321-
fullName: "Prince 2 - Agile Foundation",
322-
shortName: "Prince 2",
321+
fullName: "Agile Foundation",
322+
shortName: "Prince-2",
323323
description: "The PRINCE2 Agile Foundation Certificate validates an individual’s knowledge of how to apply the PRINCE2 project management methodology in an agile environment.",
324324
company: .other,
325325
repositoryURL:"https://github.com/Ditectrev/PRINCE2-Agile-Foundation-Tests-Exams-Questions-Answers",
@@ -334,10 +334,10 @@ extension Course {
334334
),
335335
Course(
336336
fullName: "Scrum Developer I",
337-
shortName: "SD-1",
337+
shortName: "PSD-1",
338338
description: "Verifies a developer’s understanding of Scrum principles and their ability to work effectively within a Scrum team, focusing on collaborative and incremental software development practices.",
339339
company: .other,
340-
repositoryURL:"https://github.com/Ditectrev/Professional-Scrum-Developer-I-PSD-I-Practice-Tests-Exams-Questions-Answers",
340+
repositoryURL: "https://github.com/Ditectrev/Professional-Scrum-Developer-I-PSD-I-Practice-Tests-Exams-Questions-Answers",
341341
questionURL: "https://raw.githubusercontent.com/Ditectrev/Professional-Scrum-Developer-I-PSD-I-Practice-Tests-Exams-Questions-Answers/master/README.md",
342342
url: "https://www.scrum.org/assessments/professional-scrum-developer-certification",
343343
exam: Exam(

CloudMaster/Utilities/DownloadUtility.swift

+48-33
Original file line numberDiff line numberDiff line change
@@ -147,49 +147,64 @@ class DownloadUtility {
147147
}
148148

149149
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)")
162152

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)
173154

174-
let imageFileDirectory = imageFileURL.deletingLastPathComponent()
175-
try FileManager.default.createDirectory(at: imageFileDirectory, withIntermediateDirectories: true, attributes: nil)
155+
var updatedQuestions = questions
176156

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+
}
179160

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+
}
183198
}
184199
}
200+
updatedQuestions[index]["images"] = updatedImagePaths
185201
}
186-
updatedQuestions[index]["images"] = updatedImagePaths
187202
}
188-
}
189203

190-
return updatedQuestions
204+
return updatedQuestions
205+
}
191206
}
192-
}
207+
193208

194209
extension FileManager {
195210
func removeItemIfExists(at url: URL) throws {

0 commit comments

Comments
 (0)