From 1985e7d5c5f453d800cdfbf288366d8cb70e4d5c Mon Sep 17 00:00:00 2001 From: Daniil Subbotin Date: Fri, 27 Nov 2020 09:54:57 +0300 Subject: [PATCH] Fix crash when image contains cyrillic characters in name Closes #44 --- .../FigmaExport/Subcommands/ExportImages.swift | 17 ++++++++++------- Sources/FigmaExport/main.swift | 3 +++ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/Sources/FigmaExport/Subcommands/ExportImages.swift b/Sources/FigmaExport/Subcommands/ExportImages.swift index a02d1376..16f8f418 100644 --- a/Sources/FigmaExport/Subcommands/ExportImages.swift +++ b/Sources/FigmaExport/Subcommands/ExportImages.swift @@ -221,14 +221,14 @@ extension FigmaExportCommand { // Download files to user's temp directory logger.info("Downloading remote files...") - let remoteFiles = images.flatMap { asset -> [FileContents] in - let lightFiles = makeRemoteFiles( + let remoteFiles = try images.flatMap { asset -> [FileContents] in + let lightFiles = try makeRemoteFiles( images: asset.light.images, dark: false, outputDirectory: tempDirectoryURL ) - let darkFiles = asset.dark.flatMap { darkImagePack -> [FileContents] in - makeRemoteFiles(images: darkImagePack.images, dark: true, outputDirectory: tempDirectoryURL) + let darkFiles = try asset.dark.flatMap { darkImagePack -> [FileContents] in + try makeRemoteFiles(images: darkImagePack.images, dark: true, outputDirectory: tempDirectoryURL) } ?? [] return lightFiles + darkFiles } @@ -284,9 +284,12 @@ extension FigmaExportCommand { /// - images: Dictionary of images. Key = scale, value = image info /// - dark: Dark mode? /// - outputDirectory: URL of the output directory - private func makeRemoteFiles(images: [Image], dark: Bool, outputDirectory: URL) -> [FileContents] { - images.map { image -> FileContents in - let fileURL = URL(string: "\(image.name).\(image.format)")! + private func makeRemoteFiles(images: [Image], dark: Bool, outputDirectory: URL) throws -> [FileContents] { + try images.map { image -> FileContents in + guard let name = image.name.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed), + let fileURL = URL(string: "\(name).\(image.format)") else { + throw FigmaExportError.invalidFileName(image.name) + } let scale = image.scale.value let dest = Destination( directory: outputDirectory diff --git a/Sources/FigmaExport/main.swift b/Sources/FigmaExport/main.swift index 8aca84d0..b39fdfd9 100644 --- a/Sources/FigmaExport/main.swift +++ b/Sources/FigmaExport/main.swift @@ -3,6 +3,7 @@ import Foundation enum FigmaExportError: LocalizedError { + case invalidFileName(String) case stylesNotFound case componentsNotFound case accessTokenNotFound @@ -10,6 +11,8 @@ enum FigmaExportError: LocalizedError { var errorDescription: String? { switch self { + case .invalidFileName(let name): + return "File name is invalid: \(name)" case .stylesNotFound: return "Color/Text styles not found in the Figma file. Have you published Styles to the Library?" case .componentsNotFound: