Skip to content

Commit

Permalink
Scrap Exif support
Browse files Browse the repository at this point in the history
Signed-off-by: Henrik Panhans <henrik@panhans.dev>
  • Loading branch information
henrik-dmg committed Jan 25, 2024
1 parent 44ac07a commit 01f0aad
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 186 deletions.
3 changes: 0 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ let package = Package(
name: "FileOrganiserKitTests",
dependencies: [
"FileOrganiserKit"
],
resources: [
.copy("Resources")
]
),
.executableTarget(
Expand Down
5 changes: 2 additions & 3 deletions Sources/FileOrganiser/CustomParsableCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension CustomParsableCommand {
}

let logger = Logger(options: loggerOptions)
let fileHandler = FileHandler()
let fileHandler = FileHandler(logger: logger)

do {
try Organiser(fileHandler: fileHandler, logger: logger)
Expand All @@ -30,8 +30,7 @@ extension CustomParsableCommand {
fileStrategy: strategy,
dateStrategy: options.dateStrategy,
dryRun: options.dryRun,
shouldSoftFail: options.softFail,
useExifMetadataIfPossible: options.useExifMetadata
shouldSoftFail: options.softFail
)
} catch let error as NSError {
logger.logError(message: error.localizedDescription)
Expand Down
3 changes: 0 additions & 3 deletions Sources/FileOrganiser/Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ struct Options: ParsableArguments {
)
var parseableOutput = false

@Flag(help: "Attempt to read EXIF metadata from image files to determine creation date. Will decrease processing speed")
var useExifMetadata = false

@Flag(inversion: .prefixedNo, help: "Enables or disables colored output")
var coloredOutput = true

Expand Down
7 changes: 0 additions & 7 deletions Sources/FileOrganiserKit/ExifMetadata.swift

This file was deleted.

69 changes: 0 additions & 69 deletions Sources/FileOrganiserKit/ExifParser.swift

This file was deleted.

30 changes: 10 additions & 20 deletions Sources/FileOrganiserKit/FileHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ImageIO
public protocol FileHandlerProtocol {

func doesFileExist(at url: URL) -> Bool
func resourceValues(of url: URL, useExifMetadataIfPossible: Bool) throws -> FileAttributes?
func resourceValues(of url: URL) throws -> FileAttributes?

func copyItem(at source: URL, to target: URL) throws
func moveItem(at source: URL, to target: URL) throws
Expand Down Expand Up @@ -41,34 +41,24 @@ public struct FileHandler: FileHandlerProtocol {
}
}

// MARK: - Properties

private let logger: Logger

// MARK: - Init

public init() {}
public init(logger: Logger) {
self.logger = logger
}

// MARK: - Methods

public func doesFileExist(at url: URL) -> Bool {
FileManager.default.fileExists(atPath: url.path)
}

public func resourceValues(of url: URL, useExifMetadataIfPossible: Bool) throws -> FileAttributes? {
var fileAttributes = try FileAttributes(url: url)

guard useExifMetadataIfPossible, fileAttributes.isRegularFileOrPackage else {
return fileAttributes
}

do {
let exifMetadata = try ExifParser.exifMetadata(ofFile: url)
fileAttributes.photoCreationDate = exifMetadata.captureDate
return fileAttributes
} catch {
guard error is ExifParser.ParserError else {
throw error
}
print(error.localizedDescription.addingTerminalColor(.yellow))
return fileAttributes
}
public func resourceValues(of url: URL) throws -> FileAttributes? {
try FileAttributes(url: url)
}

public func copyItem(at source: URL, to target: URL) throws {
Expand Down
23 changes: 6 additions & 17 deletions Sources/FileOrganiserKit/Organiser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,18 @@ public class Organiser {
fileStrategy: FileHandlingStrategy,
dateStrategy: DateGroupingStrategy,
dryRun: Bool,
shouldSoftFail: Bool,
useExifMetadataIfPossible: Bool
shouldSoftFail: Bool
) throws {
let startTime = DispatchTime.now()
let result = try processWithResult(
sourceURL: sourceURL,
destinationURL: destinationURL,
globPattern: globPattern,
fileStrategy: fileStrategy,
dateStrategy: dateStrategy,
dryRun: dryRun,
shouldSoftFail: shouldSoftFail,
useExifMetadataIfPossible: useExifMetadataIfPossible
shouldSoftFail: shouldSoftFail
)
let endTime = DispatchTime.now()

let elapsedTime = endTime.uptimeNanoseconds - startTime.uptimeNanoseconds
let elapsedTimeInMilliSeconds = Double(elapsedTime) / 1_000_000.0

Check warning on line 68 in Sources/FileOrganiserKit/Organiser.swift

View check run for this annotation

Codecov / codecov/patch

Sources/FileOrganiserKit/Organiser.swift#L68

Added line #L68 was not covered by tests
print("Elapsed", UInt64(elapsedTimeInMilliSeconds), "ms") // TODO: Remove before merging
logger.logSummary(
dryRun: dryRun,
filesProcessed: result.filesProcessed,
Expand All @@ -90,8 +82,7 @@ public class Organiser {
fileStrategy: FileHandlingStrategy,
dateStrategy: DateGroupingStrategy,
dryRun: Bool,
shouldSoftFail: Bool,
useExifMetadataIfPossible: Bool
shouldSoftFail: Bool
) throws -> DirectoryProcessingResult {
let glob: Glob.Pattern?
if let globPattern, !globPattern.isEmpty {
Expand Down Expand Up @@ -123,8 +114,7 @@ public class Organiser {
destination: destinationURL,
fileStrategy: fileStrategy,
dateStrategy: dateStrategy,
dryRun: dryRun,
useExifMetadataIfPossible: useExifMetadataIfPossible
dryRun: dryRun
)

switch processingResult {
Expand Down Expand Up @@ -159,10 +149,9 @@ public class Organiser {
destination: URL,
fileStrategy: FileHandlingStrategy,
dateStrategy: DateGroupingStrategy,
dryRun: Bool,
useExifMetadataIfPossible: Bool
dryRun: Bool
) throws -> FileProcessingResult {
guard let fileAttributes = try fileHandler.resourceValues(of: url, useExifMetadataIfPossible: useExifMetadataIfPossible),
guard let fileAttributes = try fileHandler.resourceValues(of: url),
fileAttributes.isRegularFileOrPackage
else {
return .notRegularFile
Expand Down
48 changes: 0 additions & 48 deletions Tests/FileOrganiserKitTests/ExifParserTests.swift

This file was deleted.

71 changes: 65 additions & 6 deletions Tests/FileOrganiserKitTests/FileHandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ final class FileHandlerTests: TemporaryFileTests {
func testFileHandler_CreatestTemporaryDirectory() throws {
let folderURL = temporaryDirectory.appendingPathComponent("some-subfolder", conformingTo: .folder)

let handler = FileHandler()
let handler = FileHandler(logger: Logger(options: .verbose))
try handler.createDirectory(at: folderURL)

XCTAssertTrue(FileManager.default.fileExists(atPath: folderURL.path))
Expand All @@ -21,7 +21,7 @@ final class FileHandlerTests: TemporaryFileTests {

try FileManager.default.createDirectory(at: folderURL, withIntermediateDirectories: true)

let handler = FileHandler()
let handler = FileHandler(logger: Logger(options: .verbose))
XCTAssertTrue(handler.doesFileExist(at: folderURL))
}

Expand All @@ -39,7 +39,7 @@ final class FileHandlerTests: TemporaryFileTests {
try FileManager.default.createDirectory(at: targetURL.deletingLastPathComponent(), withIntermediateDirectories: true)
try "some-content".write(to: sourceURL, atomically: true, encoding: .utf8)

let handler = FileHandler()
let handler = FileHandler(logger: Logger(options: .verbose))
try handler.copyItem(at: sourceURL, to: targetURL)

XCTAssertTrue(FileManager.default.fileExists(atPath: sourceURL.path))
Expand All @@ -60,7 +60,7 @@ final class FileHandlerTests: TemporaryFileTests {
try FileManager.default.createDirectory(at: targetURL.deletingLastPathComponent(), withIntermediateDirectories: true)
try "some-content".write(to: sourceURL, atomically: true, encoding: .utf8)

let handler = FileHandler()
let handler = FileHandler(logger: Logger(options: .verbose))
try handler.moveItem(at: sourceURL, to: targetURL)

XCTAssertFalse(FileManager.default.fileExists(atPath: sourceURL.path))
Expand All @@ -72,11 +72,70 @@ final class FileHandlerTests: TemporaryFileTests {

try "some-content".write(to: sourceURL, atomically: true, encoding: .utf8)

let handler = FileHandler()
let attributes = try XCTUnwrap(try handler.resourceValues(of: sourceURL, useExifMetadataIfPossible: false))
let handler = FileHandler(logger: Logger(options: .verbose))
let attributes = try XCTUnwrap(try handler.resourceValues(of: sourceURL))

XCTAssertTrue(attributes.isRegularFileOrPackage)
XCTAssertTrue((attributes.fileSizeInBytes ?? 0) > 0)
}

func testFileHandler_EnumeratesDirectory() throws {
let sourceURL =
temporaryDirectory
.appendingPathComponent("source", conformingTo: .folder)
.appendingPathComponent("text.txt", conformingTo: .text)
let targetURL =
temporaryDirectory
.appendingPathComponent("target", conformingTo: .folder)
.appendingPathComponent("text.txt", conformingTo: .plainText)

try FileManager.default.createDirectory(at: sourceURL.deletingLastPathComponent(), withIntermediateDirectories: true)
try FileManager.default.createDirectory(at: targetURL.deletingLastPathComponent(), withIntermediateDirectories: true)
try "some-content".write(to: sourceURL, atomically: true, encoding: .utf8)
try "some-content".write(to: targetURL, atomically: true, encoding: .utf8)

var enumeratedFiles = Set<String>()

let handler = FileHandler(logger: Logger(options: .verbose))
try handler.contentsOfDirectory(
at: temporaryDirectory,
includingPropertiesForKeys: nil,
options: [.skipsHiddenFiles, .producesRelativePathURLs],
shouldSoftFail: false
) { url in
enumeratedFiles.insert(url.relativePath)
} softFailCallback: { error in
XCTFail(error.localizedDescription)
}

XCTAssertEqual(enumeratedFiles.count, 4)
XCTAssertEqual(enumeratedFiles, Set(["target", "target/text.txt", "source", "source/text.txt"]))
}

func testFileHandler_ShouldSoftFail() throws {
let sourceURL =
temporaryDirectory
.appendingPathComponent("source", conformingTo: .folder)
.appendingPathComponent("text.txt", conformingTo: .text)

try FileManager.default.createDirectory(at: sourceURL.deletingLastPathComponent(), withIntermediateDirectories: true)
try "some-content".write(to: sourceURL, atomically: true, encoding: .utf8)

var hasThrownError = false

let handler = FileHandler(logger: Logger(options: .verbose))
try handler.contentsOfDirectory(
at: temporaryDirectory,
includingPropertiesForKeys: nil,
options: [.skipsHiddenFiles, .producesRelativePathURLs],
shouldSoftFail: true
) { url in
throw NSError(domain: "dev.panhans.FileOrganiserKitTests", code: 1000)
} softFailCallback: { error in
hasThrownError = true
}

XCTAssertTrue(hasThrownError)
}

}
Loading

0 comments on commit 01f0aad

Please sign in to comment.