-
Notifications
You must be signed in to change notification settings - Fork 65
Upload Feature #244
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
Upload Feature #244
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as |
||
| 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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better to use the logger class. |
||
| fileName = "VIDEO.mp4" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a good way to name a file to be uploaded. The file name should be as unique as possible unless the backend handles that for you else there will be collision issues later. You can use the timestamp or some other dynamic properties to make it unique. |
||
| 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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use logger class. |
||
| } | ||
| 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 } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here. |
||
| completion(true) | ||
| } | ||
| case .failure(let encodingError): | ||
| print(encodingError) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here. |
||
| completion(false) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| public func downloadFileToStorage(file: ServerFile, | ||
| progressCompletion: @escaping (_ percent: Float) -> Void, | ||
| completion: @escaping (_ isSuccessful: Bool ) -> Void) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code should be in the presenter though. That way our VCs stay lean.