From c8534430d0afb1c007e3d6c41a02c860b58aef43 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 27 Mar 2019 16:08:53 +0700 Subject: [PATCH] Prevent the user from accidentally saving the file without an extension Fixes #67 --- Gifski/MainWindowController.swift | 3 +- Gifski/util.swift | 74 +++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/Gifski/MainWindowController.swift b/Gifski/MainWindowController.swift index 0e2f09c0..a9e371cd 100644 --- a/Gifski/MainWindowController.swift +++ b/Gifski/MainWindowController.swift @@ -150,8 +150,9 @@ final class MainWindowController: NSWindowController { let panel = NSSavePanel() panel.canCreateDirectories = true + panel.allowedFileTypes = [FileType.gif.identifier] panel.directoryURL = inputUrl.directoryURL - panel.nameFieldStringValue = inputUrl.changingFileExtension(to: "gif").filename + panel.nameFieldStringValue = inputUrl.filenameWithoutExtension panel.prompt = "Convert" panel.message = "Choose where to save the GIF" diff --git a/Gifski/util.swift b/Gifski/util.swift index 9e6cd9e1..e425c929 100644 --- a/Gifski/util.swift +++ b/Gifski/util.swift @@ -1401,3 +1401,77 @@ extension NSError { ) } } + +enum FileType { + case png + case jpeg + case heic + case tiff + case gif + + static func from(fileExtension: String) -> FileType { + switch fileExtension { + case "png": + return .png + case "jpg", "jpeg": + return .jpeg + case "heic": + return .heic + case "tif", "tiff": + return .tiff + case "gif": + return .gif + default: + fatalError("Unsupported file type") + } + } + + static func from(url: URL) -> FileType { + return from(fileExtension: url.pathExtension) + } + + var name: String { + switch self { + case .png: + return "PNG" + case .jpeg: + return "JPEG" + case .heic: + return "HEIC" + case .tiff: + return "TIFF" + case .gif: + return "GIF" + } + } + + var identifier: String { + switch self { + case .png: + return "public.png" + case .jpeg: + return "public.jpeg" + case .heic: + return "public.heic" + case .tiff: + return "public.tiff" + case .gif: + return "com.compuserve.gif" + } + } + + var fileExtension: String { + switch self { + case .png: + return "png" + case .jpeg: + return "jpg" + case .heic: + return "heic" + case .tiff: + return "tiff" + case .gif: + return "gif" + } + } +}