Skip to content

Commit

Permalink
fix: Make file URL parsing more solid
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Jul 29, 2024
1 parent fef4b48 commit f623944
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 32 deletions.
13 changes: 3 additions & 10 deletions package/ios/Core/Types/RecordVideoOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,10 @@ struct RecordVideoOptions {
bitRateMultiplier = parsed
}
// Custom Path
let filename = FileUtils.createRandomFileName(withExtension: fileType.descriptor ?? "mov")
if let customPath = dictionary["path"] as? NSString {
guard let url = URL(string: customPath as String) else {
throw CameraError.capture(.invalidPath(path: customPath as String))
}
guard url.hasDirectoryPath else {
throw CameraError.capture(.createTempFileError(message: "Path (\(customPath)) is not a directory!"))
}
path = url.appendingPathComponent(filename)
if let customPath = dictionary["path"] as? String {
path = try FileUtils.getFilePath(customDirectory: customPath, fileExtension: "jpg")
} else {
path = FileUtils.tempDirectory.appendingPathComponent(filename)
path = try FileUtils.getFilePath(fileExtension: "jpg")
}
}
}
13 changes: 3 additions & 10 deletions package/ios/Core/Types/TakePhotoOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,10 @@ struct TakePhotoOptions {
enableShutterSound = enable
}
// Custom Path
let filename = FileUtils.createRandomFileName(withExtension: "jpg")
if let customPath = dictionary["path"] as? NSString {
guard let url = URL(string: customPath as String) else {
throw CameraError.capture(.invalidPath(path: customPath as String))
}
guard url.hasDirectoryPath else {
throw CameraError.capture(.createTempFileError(message: "Path (\(customPath)) is not a directory!"))
}
path = url.appendingPathComponent(filename)
if let customPath = dictionary["path"] as? String {
path = try FileUtils.getFilePath(customDirectory: customPath, fileExtension: "jpg")
} else {
path = FileUtils.tempDirectory.appendingPathComponent(filename)
path = try FileUtils.getFilePath(fileExtension: "jpg")
}
}
}
13 changes: 3 additions & 10 deletions package/ios/Core/Types/TakeSnapshotOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,10 @@ struct TakeSnapshotOptions {
quality = customQuality / 100.0
}
// Custom Path
let filename = FileUtils.createRandomFileName(withExtension: "jpg")
if let customPath = dictionary["path"] as? NSString {
guard let url = URL(string: customPath as String) else {
throw CameraError.capture(.invalidPath(path: customPath as String))
}
guard url.hasDirectoryPath else {
throw CameraError.capture(.createTempFileError(message: "Path (\(customPath)) is not a directory!"))
}
path = url.appendingPathComponent(filename)
if let customPath = dictionary["path"] as? String {
path = try FileUtils.getFilePath(customDirectory: customPath, fileExtension: "jpg")
} else {
path = FileUtils.tempDirectory.appendingPathComponent(filename)
path = try FileUtils.getFilePath(fileExtension: "jpg")
}
}
}
35 changes: 33 additions & 2 deletions package/ios/Core/Utils/FileUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ import UIKit

enum FileUtils {
/**
Writes Data to a temporary file and returns the file path.
Writes Data to a temporary file.
*/
private static func writeDataToFile(data: Data, file: URL) throws {
do {
try data.write(to: file)
if file.isFileURL {
try data.write(to: file)
} else {
guard let url = URL(string: "file://\(file.absoluteString)") else {
throw CameraError.capture(.createTempFileError(message: "Cannot create URL with file:// prefix!"))
}
try data.write(to: url)
}
} catch {
throw CameraError.capture(.fileError(cause: error))
}
Expand All @@ -44,4 +51,28 @@ enum FileUtils {
static func createRandomFileName(withExtension fileExtension: String) -> String {
return UUID().uuidString + "." + fileExtension
}

static func getFilePath(directory: URL, fileExtension: String) throws -> URL {
// Random UUID filename
let filename = createRandomFileName(withExtension: fileExtension)
return directory.appendingPathComponent(filename)
}

static func getFilePath(customDirectory: String, fileExtension: String) throws -> URL {
// Prefix with file://
let prefixedDirectory = customDirectory.starts(with: "file:") ? customDirectory : "file://\(customDirectory)"
// Create URL
guard let url = URL(string: prefixedDirectory) else {
throw CameraError.capture(.invalidPath(path: customDirectory))
}
// Make sure it's a directory
guard url.hasDirectoryPath else {
throw CameraError.capture(.createTempFileError(message: "Path (\(customDirectory)) is not a directory!"))
}
return try getFilePath(directory: url, fileExtension: fileExtension)
}

static func getFilePath(fileExtension: String) throws -> URL {
return try getFilePath(directory: tempDirectory, fileExtension: fileExtension)
}
}

0 comments on commit f623944

Please sign in to comment.