Skip to content

Commit

Permalink
Fix crash when image contains cyrillic characters in name
Browse files Browse the repository at this point in the history
Closes #44
  • Loading branch information
subdan committed Nov 27, 2020
1 parent f7c5740 commit 1985e7d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
17 changes: 10 additions & 7 deletions Sources/FigmaExport/Subcommands/ExportImages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions Sources/FigmaExport/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import Foundation

enum FigmaExportError: LocalizedError {

case invalidFileName(String)
case stylesNotFound
case componentsNotFound
case accessTokenNotFound
case colorsAssetsFolderNotSpecified

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:
Expand Down

0 comments on commit 1985e7d

Please sign in to comment.