-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Share extension #92
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
Merged
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
8aa9169
feat: Add shareExtension to open the app
BaptGrv aa27fd8
feat: Add appGroup to share data between shareExtension and app
BaptGrv d994aa6
feat: Add func to copy the shared image into the appGroup
BaptGrv c9933c1
chore: Remove extraneous ')'
BaptGrv ae42802
feat: Add loader
PhilippeWeidmann e0a04f9
feat: Import items to shared container
PhilippeWeidmann 2331cdc
feat: Prepare files for upload from share extension
PhilippeWeidmann dedd5a2
fix: Only handle https for kSuite links
PhilippeWeidmann bab13f2
feat: Add upload endpoint
PhilippeWeidmann 03de8a9
ci: Ensure CI runs on protected branches
PhilippeWeidmann 73946e2
refactor: Cleanup
PhilippeWeidmann 13283ab
fix: Project.swift declaration
PhilippeWeidmann 57d18b2
feat: Upload files
PhilippeWeidmann aa3f907
feat: Upload error
PhilippeWeidmann aa251e8
fix: Throw correct error
PhilippeWeidmann 213c773
refactor: Rename jsmessage to jsfunction
PhilippeWeidmann 208e3aa
feat: Subscribe to webview message
PhilippeWeidmann f7fb658
feat: Make apifetcher uploadFile cancellable
PhilippeWeidmann faaa424
feat: Handle cancel
PhilippeWeidmann 80b20c7
feat: Cleanup after upload
PhilippeWeidmann a396cf1
feat: Add NSExtensionActivationRule for data
PhilippeWeidmann 7df7be5
refactor: Remove unused code
PhilippeWeidmann 330f7cf
feat: Share extension periphery cleanup (#93)
PhilippeWeidmann 66ef78b
fix: Correct scheme
PhilippeWeidmann 3ebb401
fix: Correct scheme (#94)
PhilippeWeidmann a94095f
fix: Return after opening URL
PhilippeWeidmann aaec047
refactor: Rename attachment folder
PhilippeWeidmann c62bf61
feat: Share extension feedback (#96)
PhilippeWeidmann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
EuriaCore/Attachment Upload/FileUploadSucceedJsResponse.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| Infomaniak Euria - iOS App | ||
| Copyright (C) 2025 Infomaniak Network SA | ||
|
|
||
| This program is free software: you can redistribute it and/or modify | ||
| it under the terms of the GNU General Public License as published by | ||
| the Free Software Foundation, either version 3 of the License, or | ||
| (at your option) any later version. | ||
|
|
||
| This program is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU General Public License | ||
| along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| import Foundation | ||
|
|
||
| struct FileUploadSucceedJsResponse: Encodable, Sendable { | ||
| let ref: String | ||
| let id: String | ||
| let name: String | ||
| let mimeType: String | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
PhilippeWeidmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Infomaniak Euria - iOS App | ||
| Copyright (C) 2025 Infomaniak Network SA | ||
|
|
||
| This program is free software: you can redistribute it and/or modify | ||
| it under the terms of the GNU General Public License as published by | ||
| the Free Software Foundation, either version 3 of the License, or | ||
| (at your option) any later version. | ||
|
|
||
| This program is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU General Public License | ||
| along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| import Foundation | ||
|
|
||
| extension URL { | ||
| var importsDirectoryURL: URL { | ||
| appending(path: "imports", directoryHint: .isDirectory) | ||
| } | ||
| } | ||
|
|
||
| public struct ImportHelper { | ||
| public typealias ImportSessionUUID = String | ||
| public let importUUID: ImportSessionUUID | ||
| public let importURL: URL | ||
|
|
||
| public var importedFileURLs: [URL] { | ||
| let fileManager = FileManager.default | ||
| guard let contents = try? fileManager.contentsOfDirectory( | ||
| at: importURL, | ||
| includingPropertiesForKeys: nil, | ||
| options: [.skipsHiddenFiles, .skipsSubdirectoryDescendants] | ||
| ) else { | ||
| return [] | ||
| } | ||
| return contents | ||
| } | ||
|
|
||
| public init(baseURL: URL) { | ||
| self.init(baseURL: baseURL, importUUID: UUID().uuidString) | ||
| } | ||
|
|
||
| public init(baseURL: URL, importUUID: ImportSessionUUID) { | ||
| self.importUUID = importUUID | ||
| importURL = baseURL | ||
| .importsDirectoryURL | ||
| .appending(path: importUUID, directoryHint: .isDirectory) | ||
| } | ||
|
|
||
| public func moveURLsToImportDirectory(_ urls: [URL]) async throws { | ||
| let fileManager = FileManager.default | ||
|
|
||
| try fileManager.createDirectory(at: importURL, withIntermediateDirectories: true) | ||
|
|
||
| for url in urls { | ||
| let destinationURL = importURL.appending(path: url.lastPathComponent) | ||
| try fileManager.moveItem(at: url, to: destinationURL) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* | ||
PhilippeWeidmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Infomaniak Euria - iOS App | ||
| Copyright (C) 2025 Infomaniak Network SA | ||
|
|
||
| This program is free software: you can redistribute it and/or modify | ||
| it under the terms of the GNU General Public License as published by | ||
| the Free Software Foundation, either version 3 of the License, or | ||
| (at your option) any later version. | ||
|
|
||
| This program is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU General Public License | ||
| along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| import Foundation | ||
|
|
||
| public struct ImportedFile: Encodable, Sendable { | ||
| let ref: String | ||
| let name: String | ||
| let mimeType: String | ||
| let size: Int64 | ||
|
|
||
| let fileURL: URL | ||
|
|
||
| init(fileURL: URL) { | ||
| self.fileURL = fileURL | ||
|
|
||
| ref = UUID().uuidString | ||
| name = fileURL.lastPathComponent | ||
| do { | ||
| let resourceValues = try fileURL.resourceValues(forKeys: [.contentTypeKey, .fileSizeKey]) | ||
| mimeType = resourceValues.contentType?.preferredMIMEType ?? "application/octet-stream" | ||
| size = Int64(resourceValues.fileSize ?? 0) | ||
| } catch { | ||
| mimeType = "application/octet-stream" | ||
| size = 0 | ||
| } | ||
| } | ||
|
|
||
| enum CodingKeys: String, CodingKey { | ||
| case ref | ||
| case name | ||
| case mimeType | ||
| case size | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| /* | ||
PhilippeWeidmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Infomaniak Euria - iOS App | ||
| Copyright (C) 2025 Infomaniak Network SA | ||
|
|
||
| This program is free software: you can redistribute it and/or modify | ||
| it under the terms of the GNU General Public License as published by | ||
| the Free Software Foundation, either version 3 of the License, or | ||
| (at your option) any later version. | ||
|
|
||
| This program is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU General Public License | ||
| along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| import Foundation | ||
|
|
||
| @MainActor | ||
| public class UploadManager: ObservableObject, WebViewMessageSubscriber { | ||
|
Check warning on line 22 in EuriaCore/Attachment Upload/UploadManager.swift
|
||
| enum DomainError: Error { | ||
| case containerUnavailable | ||
| case noValidFiles | ||
| case invalidOrganizationId | ||
| case bridgeCommunicationFailed | ||
| } | ||
|
|
||
| public weak var bridge: WebViewBridge? { | ||
| didSet { | ||
| bridge?.addSubscriber(self, topic: .cancelFileUpload) | ||
| } | ||
| } | ||
|
|
||
| private var currentUploadTasks: [String: Task<Void, Never>] = [:] | ||
|
|
||
| public init() {} | ||
|
|
||
| public func handleImportSession(uuid: String, userSession: any UserSessionable) { | ||
| guard !userSession.isGuest else { return } | ||
|
|
||
| Task { | ||
| try await handleImportSession(uuid: uuid, userSession: userSession) | ||
PhilippeWeidmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| func handleImportSession(uuid: ImportHelper.ImportSessionUUID, userSession: any UserSessionable) async throws { | ||
| guard let containerURL = FileManager.default | ||
| .containerURL(forSecurityApplicationGroupIdentifier: Constants.appGroupIdentifier) else { | ||
| throw DomainError.containerUnavailable | ||
| } | ||
|
|
||
| cleanupOrphanUploadContainers(baseContainerURL: containerURL, currentImportUUID: uuid) | ||
|
|
||
| let importHelper = ImportHelper(baseURL: containerURL, importUUID: uuid) | ||
| let validImportedFiles = try await prepareUploadSessionWith(importHelper: importHelper) | ||
|
|
||
| let organizationId = try await getOrganizationId() | ||
|
|
||
| let uploadApiFetcher = UploadApiFetcher(apiFetcher: userSession.apiFetcher, organizationId: organizationId) | ||
|
|
||
| await validImportedFiles.asyncForEach { importedFile in | ||
| let uploadTask = Task { | ||
| do { | ||
| let result = try await uploadApiFetcher.uploadFile(importedFile: importedFile) | ||
| await bridge?.callFunction(FileUploadDone( | ||
| ref: importedFile.ref, | ||
| remoteId: result.id, | ||
| name: result.name, | ||
| mimeType: result.mimeType | ||
| )) | ||
| } catch UploadApiFetcher.DomainError.apiError(let rawJson) { | ||
| await bridge?.callFunction(FileUploadError(ref: importedFile.ref, error: rawJson)) | ||
| } catch { | ||
| await bridge?.callFunction(FileUploadError(ref: importedFile.ref, error: "")) | ||
PhilippeWeidmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| currentUploadTasks[importedFile.ref] = uploadTask | ||
| await uploadTask.value | ||
| currentUploadTasks[importedFile.ref] = nil | ||
| } | ||
|
|
||
| cleanupOrphanUploadContainers(baseContainerURL: containerURL, currentImportUUID: nil) | ||
| } | ||
|
|
||
| func prepareUploadSessionWith(importHelper: ImportHelper) async throws -> [ImportedFile] { | ||
| let importedFileURLs = importHelper.importedFileURLs | ||
| let importedFiles: [ImportedFile] = importedFileURLs.map { ImportedFile(fileURL: $0) } | ||
|
|
||
| guard let validFileUUIDs = await bridge?.callFunction(PrepareFilesForUpload(files: importedFiles)) else { | ||
| throw DomainError.bridgeCommunicationFailed | ||
| } | ||
|
|
||
| let validFiles = importedFiles.filter { validFileUUIDs.contains($0.ref) } | ||
|
|
||
| guard !validFiles.isEmpty else { | ||
| throw DomainError.noValidFiles | ||
| } | ||
|
|
||
| return validFiles | ||
| } | ||
|
|
||
| func getOrganizationId() async throws -> Int { | ||
| guard let organizationId = await bridge?.callFunction(GetCurrentOrganizationId()), | ||
| organizationId > 0 else { | ||
| throw DomainError.invalidOrganizationId | ||
| } | ||
|
|
||
| return organizationId | ||
| } | ||
|
|
||
| func cleanupOrphanUploadContainers(baseContainerURL: URL, currentImportUUID: String?) { | ||
| let fileManager = FileManager.default | ||
| guard let containerContents = try? fileManager.contentsOfDirectory(at: baseContainerURL.importsDirectoryURL, | ||
| includingPropertiesForKeys: nil, | ||
| options: .skipsHiddenFiles) else { | ||
| return | ||
| } | ||
|
|
||
| for containerURL in containerContents where containerURL.lastPathComponent != currentImportUUID { | ||
| try? fileManager.removeItem(at: containerURL) | ||
| } | ||
| } | ||
|
|
||
| public func handleMessage(topic: JSMessageTopic, body: Any) { | ||
| guard topic == .cancelFileUpload, | ||
| let ref = body as? String else { return } | ||
| currentUploadTasks[ref]?.cancel() | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* | ||
| Infomaniak Euria - iOS App | ||
| Copyright (C) 2025 Infomaniak Network SA | ||
|
|
||
| This program is free software: you can redistribute it and/or modify | ||
| it under the terms of the GNU General Public License as published by | ||
| the Free Software Foundation, either version 3 of the License, or | ||
| (at your option) any later version. | ||
|
|
||
| This program is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU General Public License | ||
| along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| import Foundation | ||
| import InfomaniakCore | ||
|
|
||
| public extension ApiEnvironment { | ||
| var euriaHost: String { | ||
| return "euria.\(host)" | ||
| } | ||
| } | ||
|
|
||
| extension Endpoint { | ||
PhilippeWeidmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private static var euriaHost: Endpoint { | ||
| return Endpoint(hostKeypath: \.euriaHost, path: "") | ||
| } | ||
|
|
||
| private static var base: Endpoint { | ||
| return .euriaHost.appending(path: "/api/1") | ||
| } | ||
|
|
||
| static func uploadFile(organizationId: Int) -> Endpoint { | ||
| return base.appending(path: "/accounts/\(organizationId)/files") | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.