Skip to content

Commit

Permalink
Merge pull request #14 from iGerman00/main, close #12
Browse files Browse the repository at this point in the history
uploader testing, callback for customUpload
  • Loading branch information
Adrian Castro committed Jul 19, 2023
2 parents 9555632 + 0336aea commit 113e6a1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
10 changes: 9 additions & 1 deletion ishare/Http/Custom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import Defaults
import AppKit
import SwiftyJSON

func customUpload(fileURL: URL, specification: CustomUploader, completion: @escaping () -> Void) {
enum CustomUploadError: Error {
case responseParsing
case responseRetrieval
}

func customUpload(fileURL: URL, specification: CustomUploader, callback: ((Error?, URL?) -> Void)? = nil, completion: @escaping () -> Void) {
@Default(.imageFileFormName) var imageFileFormName
@Default(.captureFileType) var fileType

Expand Down Expand Up @@ -54,13 +59,16 @@ func customUpload(fileURL: URL, specification: CustomUploader, completion: @esca
pasteboard.clearContents()
pasteboard.setString(modifiedLink, forType: .string)

callback?(nil, URL(string: modifiedLink))
completion()
} else {
print("Error parsing response or retrieving file link")
callback?(CustomUploadError.responseParsing, nil)
completion()
}
} else {
print("Error retrieving response data")
callback?(CustomUploadError.responseRetrieval, nil)
completion()
}
}
Expand Down
54 changes: 54 additions & 0 deletions ishare/Views/Settings/UploaderSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ struct UploaderSettingsView: View {
Image(systemName: "trash")
}
.buttonStyle(BorderlessButtonStyle())
.help("Delete Uploader")

Button(action: {
testCustomUploader(uploader)
}) {
Image(systemName: "icloud.and.arrow.up")
}
.buttonStyle(BorderlessButtonStyle())
.help("Test Uploader")
}
.padding(.horizontal)
.padding(.vertical, 4)
Expand Down Expand Up @@ -88,6 +97,51 @@ struct UploaderSettingsView: View {
uploadType = .IMGUR
}
}

private func testCustomUploader(_ uploader: CustomUploader) {
let image = NSImage(named: "AppIcon")
guard let imageData = image?.tiffRepresentation else { return }
let fileManager = FileManager.default
let temporaryDirectory = fileManager.temporaryDirectory
let fileURL = temporaryDirectory.appendingPathComponent("appIconImage.jpg")
do {
try imageData.write(to: fileURL)
} catch {
print("Failed to write image file: \(error)")
return
}

let callback: ((Error?, URL?) -> Void) = { error, finalURL in
if let error = error {
print("Upload error: \(error)")
DispatchQueue.main.async {
let alert = NSAlert()
alert.alertStyle = .critical
alert.messageText = "Upload Error"
alert.informativeText = "An error occurred during the upload process."
alert.runModal()
}
} else if let url = finalURL {
print("Final URL: \(url)")
DispatchQueue.main.async {
let alert = NSAlert()
alert.alertStyle = .informational
alert.messageText = "Upload Successful"
alert.informativeText = "The file was uploaded successfully."
alert.runModal()
}
}
}

customUpload(fileURL: fileURL, specification: uploader, callback: callback) {}

// Clean up temp
do {
try FileManager.default.removeItem(at: fileURL)
} catch {
print("Failed to delete temporary file: \(error)")
}
}

private func clearAllUploaders() {
savedCustomUploaders = nil
Expand Down

0 comments on commit 113e6a1

Please sign in to comment.