From 335083f85d0f4e2add7c51ae70c8ed3151afee64 Mon Sep 17 00:00:00 2001 From: iswiftshek Date: Sun, 25 Aug 2019 08:09:27 +0530 Subject: [PATCH 1/2] upload feature added --- .../AmahiAnywhere.xcodeproj/project.pbxproj | 12 +- .../AmahiAnywhere/AmahiAnywhere.entitlements | 12 ++ .../Data/Remote/ApiCalls/ServerApi.swift | 18 +++ .../Files/FilesViewController.swift | 117 +++++++++++++++++- .../AmahiAnywhere/Utils/Network.swift | 24 ++++ 5 files changed, 174 insertions(+), 9 deletions(-) diff --git a/AmahiAnywhere/AmahiAnywhere.xcodeproj/project.pbxproj b/AmahiAnywhere/AmahiAnywhere.xcodeproj/project.pbxproj index d95e4da2dba..c6a059548f6 100644 --- a/AmahiAnywhere/AmahiAnywhere.xcodeproj/project.pbxproj +++ b/AmahiAnywhere/AmahiAnywhere.xcodeproj/project.pbxproj @@ -67,6 +67,7 @@ 80F60700206BE1530098BC60 /* SettingsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 80F606FF206BE1530098BC60 /* SettingsViewController.swift */; }; 8A0A88F8227CE39C00A1360C /* MimeType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4612851D2051DB770061EC21 /* MimeType.swift */; }; 8A67FC2B22666F81005A5038 /* OfflineFile+MimeType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A67FC2A22666F81005A5038 /* OfflineFile+MimeType.swift */; }; + 991F5521231217AC00EFA0DF /* CloudKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 991F5520231217AC00EFA0DF /* CloudKit.framework */; }; 9938343D22CBAAA2002A8213 /* Toast.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9938343C22CBAAA2002A8213 /* Toast.swift */; }; 993EC87522E6959800AF5F1F /* RootContainerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 993EC87422E6959800AF5F1F /* RootContainerViewController.swift */; }; 99CF9D7F22E288BB0060C0E5 /* QueueViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 99CF9D7E22E288BB0060C0E5 /* QueueViewController.swift */; }; @@ -188,6 +189,7 @@ 80F606FF206BE1530098BC60 /* SettingsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsViewController.swift; sourceTree = ""; }; 8A67FC2A22666F81005A5038 /* OfflineFile+MimeType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "OfflineFile+MimeType.swift"; sourceTree = ""; }; 9913634D22D5D2F100B48586 /* AmahiAnywhere.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = AmahiAnywhere.entitlements; sourceTree = ""; }; + 991F5520231217AC00EFA0DF /* CloudKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CloudKit.framework; path = System/Library/Frameworks/CloudKit.framework; sourceTree = SDKROOT; }; 9938343C22CBAAA2002A8213 /* Toast.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Toast.swift; sourceTree = ""; }; 993EC87422E6959800AF5F1F /* RootContainerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RootContainerViewController.swift; sourceTree = ""; }; 99CF9D7E22E288BB0060C0E5 /* QueueViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QueueViewController.swift; sourceTree = ""; }; @@ -256,6 +258,7 @@ buildActionMask = 2147483647; files = ( 07A3AB2E5DFE0D2AD44B0F33 /* Pods_AmahiAnywhere.framework in Frameworks */, + 991F5521231217AC00EFA0DF /* CloudKit.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -334,6 +337,7 @@ C446600BA8E0E01249C2486F /* Frameworks */ = { isa = PBXGroup; children = ( + 991F5520231217AC00EFA0DF /* CloudKit.framework */, F5293AC0952F87B50DCD644D /* Pods_AmahiAnywhere.framework */, ); name = Frameworks; @@ -1030,7 +1034,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_ENTITLEMENTS = AmahiAnywhere/AmahiAnywhere.entitlements; CODE_SIGN_STYLE = Automatic; - DEVELOPMENT_TEAM = ""; + DEVELOPMENT_TEAM = 7SY99V8Y8S; ENABLE_BITCODE = NO; INFOPLIST_FILE = AmahiAnywhere/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 11.0; @@ -1038,7 +1042,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - PRODUCT_BUNDLE_IDENTIFIER = com.amahi.AmahiAnywhere; + PRODUCT_BUNDLE_IDENTIFIER = "com.google.GoogleCast.CastVideos-ios21112"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_ENFORCE_EXCLUSIVE_ACCESS = none; SWIFT_OBJC_BRIDGING_HEADER = "AmahiAnywhere/AmahiAnywhere-Bridging-Header.h"; @@ -1054,7 +1058,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_ENTITLEMENTS = AmahiAnywhere/AmahiAnywhere.entitlements; CODE_SIGN_STYLE = Automatic; - DEVELOPMENT_TEAM = ""; + DEVELOPMENT_TEAM = 7SY99V8Y8S; ENABLE_BITCODE = NO; INFOPLIST_FILE = AmahiAnywhere/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 11.0; @@ -1062,7 +1066,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - PRODUCT_BUNDLE_IDENTIFIER = com.amahi.AmahiAnywhere; + PRODUCT_BUNDLE_IDENTIFIER = "com.google.GoogleCast.CastVideos-ios21112"; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_ENFORCE_EXCLUSIVE_ACCESS = none; SWIFT_OBJC_BRIDGING_HEADER = "AmahiAnywhere/AmahiAnywhere-Bridging-Header.h"; diff --git a/AmahiAnywhere/AmahiAnywhere/AmahiAnywhere.entitlements b/AmahiAnywhere/AmahiAnywhere/AmahiAnywhere.entitlements index ba21fbdaf29..1c19977c9fd 100644 --- a/AmahiAnywhere/AmahiAnywhere/AmahiAnywhere.entitlements +++ b/AmahiAnywhere/AmahiAnywhere/AmahiAnywhere.entitlements @@ -2,7 +2,19 @@ + com.apple.developer.icloud-container-identifiers + + com.apple.developer.icloud-services + + CloudDocuments + com.apple.developer.networking.wifi-info + com.apple.developer.ubiquity-container-identifiers + + com.apple.developer.ubiquity-kvstore-identifier + $(TeamIdentifierPrefix)$(CFBundleIdentifier) + keychain-access-groups + diff --git a/AmahiAnywhere/AmahiAnywhere/Data/Remote/ApiCalls/ServerApi.swift b/AmahiAnywhere/AmahiAnywhere/Data/Remote/ApiCalls/ServerApi.swift index 610073042ec..2c0144050bc 100644 --- a/AmahiAnywhere/AmahiAnywhere/Data/Remote/ApiCalls/ServerApi.swift +++ b/AmahiAnywhere/AmahiAnywhere/Data/Remote/ApiCalls/ServerApi.swift @@ -210,4 +210,22 @@ class ServerApi { return components.url } + + public func getShareUri(_ share: ServerShare) -> URL? { + var components = URLComponents(string: serverAddress!)! + components.path = "/files" + + components.queryItems = [ + URLQueryItem(name: "s", value: share.name), + URLQueryItem(name: "session", value: server.session_token) + ] + + if let authToken = auth_token{ + components.queryItems?.append(URLQueryItem(name: "auth", value: authToken)) + } + components.percentEncodedQuery = components.percentEncodedQuery? + .replacingOccurrences(of: "+", with: "%2B") + + return components.url + } } diff --git a/AmahiAnywhere/AmahiAnywhere/Presentation/Files/FilesViewController.swift b/AmahiAnywhere/AmahiAnywhere/Presentation/Files/FilesViewController.swift index 1d4f58b8e2d..26e31eec995 100644 --- a/AmahiAnywhere/AmahiAnywhere/Presentation/Files/FilesViewController.swift +++ b/AmahiAnywhere/AmahiAnywhere/Presentation/Files/FilesViewController.swift @@ -33,6 +33,9 @@ class FilesViewController: BaseUIViewController, GCKRemoteMediaClientListener { } } + //fileTypeNumber: Image = 0, Video = 1, Document = 2 + var fileTypeNumber = 0 + public var sessionManager: GCKSessionManager! public var mediaInformation: GCKMediaInformation? public var mediaClient: GCKRemoteMediaClient! @@ -139,6 +142,7 @@ class FilesViewController: BaseUIViewController, GCKRemoteMediaClientListener { @objc func uploadDocumentTapped(){ self.present(documentPicker, animated: true, completion: nil) + self.fileTypeNumber = 2 } @objc func expiredAuthTokenHDA(){ @@ -154,6 +158,7 @@ class FilesViewController: BaseUIViewController, GCKRemoteMediaClientListener { func uploadImageVideoTapped(isTypePhoto: Bool){ imagePicker.mediaTypes = isTypePhoto ? [kUTTypeImage as String] : [kUTTypeMovie as String] + self.fileTypeNumber = isTypePhoto ? 0 : 1 let libraryTitle = isTypePhoto ? "Photo Library" : "Video Library" let alertVC = UIAlertController(title: "Select your source", message: nil, preferredStyle: .actionSheet) @@ -571,8 +576,90 @@ extension FilesViewController: UINavigationControllerDelegate, UIImagePickerCont func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { picker.dismiss(animated: true, completion: nil) - guard let _ = info[.editedImage] as? UIImage else { - return + let mediaType = info[UIImagePickerController.InfoKey.mediaType] as! CFString + var fileName: String = "" + var mimeType: String = "" + + var url: String = "" + if directory != nil { + url = ServerApi.shared!.getFileUri(directory!)!.absoluteString + } + else { + url = ServerApi.shared!.getShareUri(share)!.absoluteString + } + + let assetPath = info[UIImagePickerController.InfoKey.referenceURL] as? NSURL + + if assetPath != nil { + let ext = assetPath!.absoluteString?.components(separatedBy: "ext=")[1].lowercased() + if ext == "png" || ext == "jpg" || ext == "heic" { + mimeType = "image/jpeg" + } + else { + if ext == "mp4" { + mimeType = "video/mp4" + } + else if ext == "mov" { + mimeType = "video/quicktime" + } + else if ext == "flv" { + mimeType = "video/x-flv" + } + else if ext == "avi" { + mimeType = "video/x-msvideo" + } + } + } + else { + if self.fileTypeNumber == 0 { + mimeType = "image/jpeg" + } + else { + mimeType = "video/quicktime" + } + } + + switch mediaType { + case kUTTypeImage: + guard let image = info[.editedImage] as? UIImage else { + return + } + fileName = "IMG.jpeg" + let data: Data = image.jpegData(compressionQuality: 100)! + Network.shared.uploadFile(url, data: data, fileName: fileName, mime: mimeType) { success in + if success { + self.showStatusAlert(title: "Image was successfully uploaded", true) + } + else { + self.showStatusAlert(title: "An error occured while uploading the image", true) + } + self.presenter.getFiles(self.share, directory: self.directory) + } + + break + case kUTTypeMovie: + guard let videoURL = info[UIImagePickerController.InfoKey.mediaURL] as? NSURL else { + return + } + do { + let data = try Data(contentsOf: videoURL as URL, options: .mappedIfSafe) + print(data) + fileName = "VIDEO.mp4" + Network.shared.uploadFile(url, data: data, fileName: fileName, mime: mimeType) { success in + if success { + self.showStatusAlert(title: "Video was successfully uploaded", true) + } + else { + self.showStatusAlert(title: "An error occured while uploading the video", true) + } + self.presenter.getFiles(self.share, directory: self.directory) + } + } catch { + print(error) + } + break + default: + break } } } @@ -592,8 +679,28 @@ extension FilesViewController: UIDocumentPickerDelegate{ guard let myURL = urls.first else { return } + var url: String = "" + if directory != nil { + url = ServerApi.shared!.getFileUri(directory!)!.absoluteString + } + else { + url = ServerApi.shared!.getShareUri(share)!.absoluteString + } - print("selected document url : \(myURL)") - } - + let fileName = myURL.lastPathComponent + let mimeType = "application/\(myURL.lastPathComponent.components(separatedBy: ".")[1])" + + do { + let data = try Data(contentsOf: myURL as URL, options: .mappedIfSafe) + Network.shared.uploadFile(url, data: data, fileName: fileName, mime: mimeType) { success in + if success { + self.showStatusAlert(title: "Document was successfully uploaded", true) + } + else { + self.showStatusAlert(title: "An error occured while uploading the document", true) + } + self.presenter.getFiles(self.share, directory: self.directory) + } + } catch { return } + } } diff --git a/AmahiAnywhere/AmahiAnywhere/Utils/Network.swift b/AmahiAnywhere/AmahiAnywhere/Utils/Network.swift index ca3cba58efa..6d9d3574418 100644 --- a/AmahiAnywhere/AmahiAnywhere/Utils/Network.swift +++ b/AmahiAnywhere/AmahiAnywhere/Utils/Network.swift @@ -120,6 +120,30 @@ public class Network { } } + public func uploadFile(_ url: String!, data: Data, fileName: String!, mime: String!, parameters: Parameters = [:], headers: HTTPHeaders = [:], completion: @escaping (_ isSuccessful: Bool ) -> Void) { + Alamofire.upload(multipartFormData: { multipartFormData in + for (key,value) in parameters { + multipartFormData.append((value as! String).data(using: .utf8)!, withName: key) + } + multipartFormData.append(data, withName: "file", fileName: fileName,mimeType: mime) + }, + usingThreshold: UInt64.init(), + to: url, + method: .post, + encodingCompletion: { encodingResult in + switch encodingResult { + case .success(let upload, _, _): + upload.response { response in + debugPrint(response) + completion(true) + } + case .failure(let encodingError): + print(encodingError) + completion(false) + } + }) + } + public func downloadFileToStorage(file: ServerFile, progressCompletion: @escaping (_ percent: Float) -> Void, completion: @escaping (_ isSuccessful: Bool ) -> Void) { From 1c04b7f07426c297d35a44e4f9904f7a645a8ded Mon Sep 17 00:00:00 2001 From: iswiftshek Date: Sun, 25 Aug 2019 08:11:58 +0530 Subject: [PATCH 2/2] development team removed --- AmahiAnywhere/AmahiAnywhere.xcodeproj/project.pbxproj | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/AmahiAnywhere/AmahiAnywhere.xcodeproj/project.pbxproj b/AmahiAnywhere/AmahiAnywhere.xcodeproj/project.pbxproj index c6a059548f6..63403391fcd 100644 --- a/AmahiAnywhere/AmahiAnywhere.xcodeproj/project.pbxproj +++ b/AmahiAnywhere/AmahiAnywhere.xcodeproj/project.pbxproj @@ -1034,7 +1034,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_ENTITLEMENTS = AmahiAnywhere/AmahiAnywhere.entitlements; CODE_SIGN_STYLE = Automatic; - DEVELOPMENT_TEAM = 7SY99V8Y8S; + DEVELOPMENT_TEAM = ""; ENABLE_BITCODE = NO; INFOPLIST_FILE = AmahiAnywhere/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 11.0; @@ -1042,7 +1042,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - PRODUCT_BUNDLE_IDENTIFIER = "com.google.GoogleCast.CastVideos-ios21112"; + PRODUCT_BUNDLE_IDENTIFIER = com.amahi.AmahiAnywhere; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_ENFORCE_EXCLUSIVE_ACCESS = none; SWIFT_OBJC_BRIDGING_HEADER = "AmahiAnywhere/AmahiAnywhere-Bridging-Header.h"; @@ -1058,7 +1058,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_ENTITLEMENTS = AmahiAnywhere/AmahiAnywhere.entitlements; CODE_SIGN_STYLE = Automatic; - DEVELOPMENT_TEAM = 7SY99V8Y8S; + DEVELOPMENT_TEAM = ""; ENABLE_BITCODE = NO; INFOPLIST_FILE = AmahiAnywhere/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 11.0; @@ -1066,7 +1066,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - PRODUCT_BUNDLE_IDENTIFIER = "com.google.GoogleCast.CastVideos-ios21112"; + PRODUCT_BUNDLE_IDENTIFIER = com.amahi.AmahiAnywhere; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_ENFORCE_EXCLUSIVE_ACCESS = none; SWIFT_OBJC_BRIDGING_HEADER = "AmahiAnywhere/AmahiAnywhere-Bridging-Header.h";