Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image uploading #652

Merged
merged 21 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Mlem.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@
030AC0522A64666C00037155 /* UserSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 030AC0512A64666C00037155 /* UserSettingsView.swift */; };
030D4AE62AA1273200A3393D /* ErrorDetails.swift in Sources */ = {isa = PBXBuildFile; fileRef = 030D4AE52AA1273200A3393D /* ErrorDetails.swift */; };
030D4AE82AA1278400A3393D /* ErrorDetails+Mock.swift in Sources */ = {isa = PBXBuildFile; fileRef = 030D4AE72AA1278400A3393D /* ErrorDetails+Mock.swift */; };
030E86392AC6B44B000283A6 /* DeletePictrsFile.swift in Sources */ = {isa = PBXBuildFile; fileRef = 030E86382AC6B44B000283A6 /* DeletePictrsFile.swift */; };
030E863B2AC6C3B1000283A6 /* PictrsRespository.swift in Sources */ = {isa = PBXBuildFile; fileRef = 030E863A2AC6C3B1000283A6 /* PictrsRespository.swift */; };
030E863D2AC6C49E000283A6 /* PictrsRepository+Dependency.swift in Sources */ = {isa = PBXBuildFile; fileRef = 030E863C2AC6C49E000283A6 /* PictrsRepository+Dependency.swift */; };
030E863F2AC6C5E9000283A6 /* PictrsImageModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 030E863E2AC6C5E9000283A6 /* PictrsImageModel.swift */; };
031A93D62AC847DA0077030C /* UploadConfirmationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 031A93D52AC847DA0077030C /* UploadConfirmationView.swift */; };
031BF9532AB24BAF00F4517F /* SiteVersion.swift in Sources */ = {isa = PBXBuildFile; fileRef = 031BF9522AB24BAF00F4517F /* SiteVersion.swift */; };
031BF9552AB25AFB00F4517F /* SiteVersionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 031BF9542AB25AFB00F4517F /* SiteVersionTests.swift */; };
032109472AA7C3FC00912DFC /* CommunityLabelView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 032109462AA7C3FC00912DFC /* CommunityLabelView.swift */; };
032109492AA7C41800912DFC /* AvatarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 032109482AA7C41800912DFC /* AvatarView.swift */; };
032DD2FD2AC3594B00F1B33D /* ImageUploadView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 032DD2FC2AC3594B00F1B33D /* ImageUploadView.swift */; };
034C724F2A82B61200B8A4B8 /* LayoutWidgetTracker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 034C724E2A82B61200B8A4B8 /* LayoutWidgetTracker.swift */; };
035EB0CA2A8687C200227859 /* JumpButtonView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 035EB0C92A8687C200227859 /* JumpButtonView.swift */; };
038A16DF2A75172C0087987E /* LayoutWidgetEditView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 038A16DE2A75172C0087987E /* LayoutWidgetEditView.swift */; };
Expand All @@ -35,6 +41,8 @@
03E0B9C82A61F0F400FED265 /* AdvancedSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03E0B9C72A61F0F400FED265 /* AdvancedSettingsView.swift */; };
03E0B9CA2A62B4A400FED265 /* ContributorsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03E0B9C92A62B4A400FED265 /* ContributorsView.swift */; };
03E0B9CC2A62CD5800FED265 /* ThemeSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03E0B9CB2A62CD5800FED265 /* ThemeSettingsView.swift */; };
03EA79C42AC0D92C00BCDC91 /* PostDetailEditorView+Logic.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03EA79C32AC0D92C00BCDC91 /* PostDetailEditorView+Logic.swift */; };
03EC92992AC0BF8A007BBE7E /* APIClient+Pictrs.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03EC92982AC0BF8A007BBE7E /* APIClient+Pictrs.swift */; };
500C168E2A66FAAB006F243B /* HapticManager+Dependency.swift in Sources */ = {isa = PBXBuildFile; fileRef = 500C168D2A66FAAB006F243B /* HapticManager+Dependency.swift */; };
5016A2B12A67EB8600B257E8 /* UIViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5016A2B02A67EB8600B257E8 /* UIViewController.swift */; };
5016A2B32A67EC0700B257E8 /* NotificationDisplayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5016A2B22A67EC0700B257E8 /* NotificationDisplayer.swift */; };
Expand Down Expand Up @@ -463,10 +471,16 @@
030AC0512A64666C00037155 /* UserSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserSettingsView.swift; sourceTree = "<group>"; };
030D4AE52AA1273200A3393D /* ErrorDetails.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ErrorDetails.swift; sourceTree = "<group>"; };
030D4AE72AA1278400A3393D /* ErrorDetails+Mock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ErrorDetails+Mock.swift"; sourceTree = "<group>"; };
030E86382AC6B44B000283A6 /* DeletePictrsFile.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeletePictrsFile.swift; sourceTree = "<group>"; };
030E863A2AC6C3B1000283A6 /* PictrsRespository.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PictrsRespository.swift; sourceTree = "<group>"; };
030E863C2AC6C49E000283A6 /* PictrsRepository+Dependency.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PictrsRepository+Dependency.swift"; sourceTree = "<group>"; };
030E863E2AC6C5E9000283A6 /* PictrsImageModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PictrsImageModel.swift; sourceTree = "<group>"; };
031A93D52AC847DA0077030C /* UploadConfirmationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UploadConfirmationView.swift; sourceTree = "<group>"; };
031BF9522AB24BAF00F4517F /* SiteVersion.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SiteVersion.swift; sourceTree = "<group>"; };
031BF9542AB25AFB00F4517F /* SiteVersionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SiteVersionTests.swift; sourceTree = "<group>"; };
032109462AA7C3FC00912DFC /* CommunityLabelView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommunityLabelView.swift; sourceTree = "<group>"; };
032109482AA7C41800912DFC /* AvatarView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AvatarView.swift; sourceTree = "<group>"; };
032DD2FC2AC3594B00F1B33D /* ImageUploadView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageUploadView.swift; sourceTree = "<group>"; };
034C724E2A82B61200B8A4B8 /* LayoutWidgetTracker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LayoutWidgetTracker.swift; sourceTree = "<group>"; };
035EB0C92A8687C200227859 /* JumpButtonView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JumpButtonView.swift; sourceTree = "<group>"; };
038A16DE2A75172C0087987E /* LayoutWidgetEditView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LayoutWidgetEditView.swift; sourceTree = "<group>"; };
Expand All @@ -487,6 +501,8 @@
03E0B9C72A61F0F400FED265 /* AdvancedSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdvancedSettingsView.swift; sourceTree = "<group>"; };
03E0B9C92A62B4A400FED265 /* ContributorsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContributorsView.swift; sourceTree = "<group>"; };
03E0B9CB2A62CD5800FED265 /* ThemeSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThemeSettingsView.swift; sourceTree = "<group>"; };
03EA79C32AC0D92C00BCDC91 /* PostDetailEditorView+Logic.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PostDetailEditorView+Logic.swift"; sourceTree = "<group>"; };
03EC92982AC0BF8A007BBE7E /* APIClient+Pictrs.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "APIClient+Pictrs.swift"; sourceTree = "<group>"; };
500C168D2A66FAAB006F243B /* HapticManager+Dependency.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "HapticManager+Dependency.swift"; sourceTree = "<group>"; };
5016A2B02A67EB8600B257E8 /* UIViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIViewController.swift; sourceTree = "<group>"; };
5016A2B22A67EC0700B257E8 /* NotificationDisplayer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationDisplayer.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1022,6 +1038,15 @@
path = User;
sourceTree = "<group>";
};
031A93D42AC847D10077030C /* Image Upload */ = {
isa = PBXGroup;
children = (
032DD2FC2AC3594B00F1B33D /* ImageUploadView.swift */,
031A93D52AC847DA0077030C /* UploadConfirmationView.swift */,
);
path = "Image Upload";
sourceTree = "<group>";
};
031BF9562AB25AFE00F4517F /* Model */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1193,6 +1218,7 @@
50C99B5B2A61F5EB005D57DD /* CommentRepository.swift */,
CD82A2522A716B8100111034 /* PersonRepository.swift */,
CD6F29A92A78003A00F20B6B /* PostRepository.swift */,
030E863A2AC6C3B1000283A6 /* PictrsRespository.swift */,
50A881292A72D6BD003E3661 /* CommunityRepository.swift */,
50A881232A71A4CD003E3661 /* PersistenceRepository.swift */,
);
Expand Down Expand Up @@ -1285,6 +1311,7 @@
6386E0352A042C59006B3C1D /* Contributor.swift */,
63F0C7A52A05225100A18C5D /* Saved Account.swift */,
63CE4E722A06F5A100405271 /* Access Token.swift */,
030E863E2AC6C5E9000283A6 /* PictrsImageModel.swift */,
63E5D3932A13CF3600EC1FBD /* Favorite Community.swift */,
CD2BD6772A79F55800ECFF89 /* ImageSize.swift */,
030D4AE52AA1273200A3393D /* ErrorDetails.swift */,
Expand Down Expand Up @@ -1698,6 +1725,7 @@
637218282A3A2AAD008C4816 /* CreatePostLike.swift */,
637218292A3A2AAD008C4816 /* DeletePost.swift */,
6372182A2A3A2AAD008C4816 /* CreatePost.swift */,
030E86382AC6B44B000283A6 /* DeletePictrsFile.swift */,
6372182B2A3A2AAD008C4816 /* GetPost.swift */,
6372182C2A3A2AAD008C4816 /* GetPosts.swift */,
6372182D2A3A2AAD008C4816 /* SavePost.swift */,
Expand Down Expand Up @@ -1883,6 +1911,7 @@
ADF266942A4E8A1E00EBA648 /* PostComposerView.swift */,
CD391F952A535F5400E213B5 /* ResponseEditorView.swift */,
03CB329D2A6D8E910021EF27 /* PostDetailEditorView.swift */,
03EA79C32AC0D92C00BCDC91 /* PostDetailEditorView+Logic.swift */,
);
path = Composer;
sourceTree = "<group>";
Expand Down Expand Up @@ -1914,6 +1943,7 @@
637218012A3A2AAD008C4816 /* APIClient.swift */,
50A881272A71D66B003E3661 /* APIClient+Community.swift */,
50A8812D2A72D76C003E3661 /* APIClient+Comment.swift */,
03EC92982AC0BF8A007BBE7E /* APIClient+Pictrs.swift */,
CDEBC3382A9ADE6C00518D9D /* APIClient+Post.swift */,
);
path = APIClient;
Expand Down Expand Up @@ -1980,6 +2010,7 @@
CD69F5702A422EDD0028D4F7 /* InteractionBarView.swift */,
632E8EE427EE63BD007E8D75 /* Components */,
CDF1EF152A6C3BC2003594B6 /* End Of Feed View.swift */,
031A93D42AC847D10077030C /* Image Upload */,
CD45BCED2A75CA7200A2899C /* Thumbnail Image View.swift */,
CDC1C9422A7AC24600072E3D /* ReadCheck.swift */,
CD309C452A93FBD300988F95 /* Logo View.swift */,
Expand Down Expand Up @@ -2052,6 +2083,7 @@
50A881252A71A511003E3661 /* PersistenceRepository+Dependency.swift */,
CD82A2562A716D7C00111034 /* PersonRepository+Dependency.swift */,
CD6F29AB2A78015200F20B6B /* PostRepository+Dependency.swift */,
030E863C2AC6C49E000283A6 /* PictrsRepository+Dependency.swift */,
);
path = Repositories;
sourceTree = "<group>";
Expand Down Expand Up @@ -2573,6 +2605,7 @@
CDB0117F2A6F70A000D043EB /* Editor Tracker.swift in Sources */,
6354F30A2A2E20040074C08D /* Alert - Multiple Alerts.swift in Sources */,
6318EDC727EE4E1500BFCAE8 /* Post.swift in Sources */,
03EC92992AC0BF8A007BBE7E /* APIClient+Pictrs.swift in Sources */,
6372186C2A3A2AAD008C4816 /* SaveComment.swift in Sources */,
E4D4DBA22A7F233200C4F3DE /* FancyTabNavigationSelectionHashValueEnvironmentKey.swift in Sources */,
6DD8677A2A5083A200BEB00F /* Community Sidebar Link.swift in Sources */,
Expand All @@ -2588,6 +2621,7 @@
5064D03D2A6DE0AA00B22EE3 /* Notifier.swift in Sources */,
CDC65D912A86B830007205E5 /* DeleteAccountView.swift in Sources */,
CD391F9C2A53980900E213B5 /* ReplyToCommentReply.swift in Sources */,
030E863D2AC6C49E000283A6 /* PictrsRepository+Dependency.swift in Sources */,
630737892A1CD1E900039852 /* String.swift in Sources */,
50811B402A9205EE006BA3F2 /* CommunityResponse+Mock.swift in Sources */,
03E0B9CC2A62CD5800FED265 /* ThemeSettingsView.swift in Sources */,
Expand Down Expand Up @@ -2624,6 +2658,7 @@
637218672A3A2AAD008C4816 /* GetPersonDetails.swift in Sources */,
B1A26FE12A44AAB200B91A32 /* Navigation getter.swift in Sources */,
6332FDC027EFB05F0009A98A /* Settings Item.swift in Sources */,
031A93D62AC847DA0077030C /* UploadConfirmationView.swift in Sources */,
CD8C55342A95515C0060B75B /* Onboarding Text.swift in Sources */,
50C99B602A6299D8005D57DD /* ErrorHandler.swift in Sources */,
50F830F82A4C92BF00D67099 /* FeedTrackerItemProviding.swift in Sources */,
Expand Down Expand Up @@ -2654,11 +2689,13 @@
E4F0B56F2ABD00A000BC3E4A /* PresentationBackgroundInteraction.swift in Sources */,
CDF842612A49EA3900723DA0 /* Mentions Tracker.swift in Sources */,
6D693A4C2A51B99E009E2D76 /* APICommentReport.swift in Sources */,
030E863B2AC6C3B1000283A6 /* PictrsRespository.swift in Sources */,
63344C672A08D4E3001BC616 /* AppearanceSettingsView.swift in Sources */,
E49F0E762A90395400BC4EE3 /* NavigationPath+Helpers.swift in Sources */,
B1955A1F2A606F010056CF99 /* EasterFlagsTracker.swift in Sources */,
63D24ED92A169A5F005CCA81 /* UIApplication.swift in Sources */,
039439912A98FA6100463032 /* UserFeedView.swift in Sources */,
03EA79C42AC0D92C00BCDC91 /* PostDetailEditorView+Logic.swift in Sources */,
637218482A3A2AAD008C4816 /* APICommentReply.swift in Sources */,
032109472AA7C3FC00912DFC /* CommunityLabelView.swift in Sources */,
637218502A3A2AAD008C4816 /* APIPersonAggregates.swift in Sources */,
Expand Down Expand Up @@ -2741,6 +2778,7 @@
03CB329E2A6D8E910021EF27 /* PostDetailEditorView.swift in Sources */,
CD69F5752A42479A0028D4F7 /* Comment Item Logic.swift in Sources */,
6D7782342A48EE8C008AC1BF /* APIPrivateMessageView.swift in Sources */,
030E86392AC6B44B000283A6 /* DeletePictrsFile.swift in Sources */,
50811B342A9204EB006BA3F2 /* APICommunityAggregates+Mock.swift in Sources */,
B11A1A782A4EFF2B00520DB4 /* Feed Root.swift in Sources */,
CDC1C93F2A7AB8C700072E3D /* AccessibilitySettingsView.swift in Sources */,
Expand Down Expand Up @@ -2806,6 +2844,7 @@
5016A2B12A67EB8600B257E8 /* UIViewController.swift in Sources */,
6372184C2A3A2AAD008C4816 /* APIPostView.swift in Sources */,
CDB0117D2A6F703800D043EB /* CommentEditor.swift in Sources */,
030E863F2AC6C5E9000283A6 /* PictrsImageModel.swift in Sources */,
632E8EE627EE63D3007E8D75 /* UpvoteButtonView.swift in Sources */,
B1A26FE32A45B11800B91A32 /* View - Handle Lemmy Links.swift in Sources */,
03B643572A6864CD00F65700 /* TabBarSettingsView.swift in Sources */,
Expand All @@ -2830,6 +2869,7 @@
03A1B3F72A84000400AB0DE0 /* APIContentAggregatesProtocol.swift in Sources */,
CD4DBC032A6F803C001A1E61 /* ReplyToPost.swift in Sources */,
CD6483302A38D31C00EE6CA3 /* UpvoteCounterView.swift in Sources */,
032DD2FD2AC3594B00F1B33D /* ImageUploadView.swift in Sources */,
88B165B82A8643F4007C9115 /* View - NavigationBar Color.swift in Sources */,
030AC0522A64666C00037155 /* UserSettingsView.swift in Sources */,
CDA2C5262A705D6000649D5A /* PostEditor.swift in Sources */,
Expand Down
107 changes: 107 additions & 0 deletions Mlem/API/APIClient/APIClient+Pictrs.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//
// APIClient+Pictrs.swift
// Mlem
//
// Created by Sjmarf on 24/09/2023.
//

import Foundation

extension APIClient {
@discardableResult
func deleteImage(file: PictrsFile) async throws -> ImageDeleteResponse {
let request = try ImageDeleteRequest(session: session, file: file.file, deleteToken: file.deleteToken)
return try await perform(request: request)
}

func uploadImage(
_ imageData: Data,
onProgress progressCallback: @escaping (_ progress: Double) -> Void,
onCompletion completionCallback: @escaping(_ response: ImageUploadResponse?) -> Void,
`catch`: @escaping (Error) -> Void
) async throws -> Task<(), any Error> {

let delegate = ImageUploadDelegate(callback: progressCallback)
// Modify the instance URL to remove "api/v3" and add "pictrs/image".
var components = URLComponents()
components.scheme = try self.session.instanceUrl.scheme
components.host = try self.session.instanceUrl.host
components.path = "/pictrs/image"

guard let url = components.url else {
throw APIClientError.response(.init(error: "Failed to modify instance URL to add pictrs."), nil)
}
var request = URLRequest(url: url)
Sjmarf marked this conversation as resolved.
Show resolved Hide resolved
request.httpMethod = "POST"

let boundary = UUID().uuidString

request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
request.setValue("jwt=\(try session.token)", forHTTPHeaderField: "Cookie")

let multiPartForm: MultiPartForm = try .init(
mimeType: "image/png",
fileName: "image.png",
imageData: imageData,
auth: session.token
)

return Task { [request] in
do {
let (data, _) = try await self.urlSession.upload(
for: request,
from: multiPartForm.createField(boundary: boundary),
delegate: delegate)
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let response = try decoder.decode(ImageUploadResponse.self, from: data)
completionCallback(response)
} catch DecodingError.dataCorrupted {
throw APIClientError.decoding(data)
}
} catch {
if !Task.isCancelled {
`catch`(error)
}
}
Sjmarf marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

private struct MultiPartForm: Codable {
var mimeType: String
var fileName: String
var imageData: Data
var auth: String

func createField(boundary: String) -> Data {
var data = Data()
data.append(Data("--\(boundary)\r\n".utf8))
data.append(Data("Content-Disposition: form-data; name=\"images[]\"; filename=\"\(fileName)\"\r\n".utf8))
data.append(Data("Content-Type: \(mimeType)\r\n".utf8))
data.append(Data("\r\n".utf8))
data.append(imageData)
data.append(Data("\r\n".utf8))
data.append(Data("--\(boundary)--\r\n".utf8))
return data
}
}

private class ImageUploadDelegate: NSObject, URLSessionTaskDelegate {
public let callback: (Double) -> Void

public init(callback: @escaping (Double) -> Void) {
self.callback = callback
}

public func urlSession(
_ session: URLSession,
task: URLSessionTask,
didSendBodyData bytesSent: Int64,
totalBytesSent: Int64,
totalBytesExpectedToSend: Int64
) {
callback(Double(totalBytesSent) / Double(totalBytesExpectedToSend))
}
}
Loading