Skip to content
Closed
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
4 changes: 4 additions & 0 deletions AmahiAnywhere/AmahiAnywhere.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -188,6 +189,7 @@
80F606FF206BE1530098BC60 /* SettingsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsViewController.swift; sourceTree = "<group>"; };
8A67FC2A22666F81005A5038 /* OfflineFile+MimeType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "OfflineFile+MimeType.swift"; sourceTree = "<group>"; };
9913634D22D5D2F100B48586 /* AmahiAnywhere.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = AmahiAnywhere.entitlements; sourceTree = "<group>"; };
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 = "<group>"; };
993EC87422E6959800AF5F1F /* RootContainerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RootContainerViewController.swift; sourceTree = "<group>"; };
99CF9D7E22E288BB0060C0E5 /* QueueViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QueueViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -256,6 +258,7 @@
buildActionMask = 2147483647;
files = (
07A3AB2E5DFE0D2AD44B0F33 /* Pods_AmahiAnywhere.framework in Frameworks */,
991F5521231217AC00EFA0DF /* CloudKit.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -334,6 +337,7 @@
C446600BA8E0E01249C2486F /* Frameworks */ = {
isa = PBXGroup;
children = (
991F5520231217AC00EFA0DF /* CloudKit.framework */,
F5293AC0952F87B50DCD644D /* Pods_AmahiAnywhere.framework */,
);
name = Frameworks;
Expand Down
12 changes: 12 additions & 0 deletions AmahiAnywhere/AmahiAnywhere/AmahiAnywhere.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.developer.icloud-container-identifiers</key>
<array/>
<key>com.apple.developer.icloud-services</key>
<array>
<string>CloudDocuments</string>
</array>
<key>com.apple.developer.networking.wifi-info</key>
<true/>
<key>com.apple.developer.ubiquity-container-identifiers</key>
<array/>
<key>com.apple.developer.ubiquity-kvstore-identifier</key>
<string>$(TeamIdentifierPrefix)$(CFBundleIdentifier)</string>
<key>keychain-access-groups</key>
<array/>
</dict>
</plist>
18 changes: 18 additions & 0 deletions AmahiAnywhere/AmahiAnywhere/Data/Remote/ApiCalls/ServerApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -139,6 +142,7 @@ class FilesViewController: BaseUIViewController, GCKRemoteMediaClientListener {

@objc func uploadDocumentTapped(){
self.present(documentPicker, animated: true, completion: nil)
self.fileTypeNumber = 2
}

@objc func expiredAuthTokenHDA(){
Expand All @@ -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)
Expand Down Expand Up @@ -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" {
Copy link
Member

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.

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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as "VIDEO.mp4" below

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to use the logger class.

fileName = "VIDEO.mp4"
Copy link
Member

@codedentwickler codedentwickler Dec 18, 2019

Choose a reason for hiding this comment

The 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use logger class.

}
break
default:
break
}
}
}
Expand All @@ -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 }
}
}
24 changes: 24 additions & 0 deletions AmahiAnywhere/AmahiAnywhere/Utils/Network.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here.

completion(true)
}
case .failure(let encodingError):
print(encodingError)
Copy link
Member

Choose a reason for hiding this comment

The 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) {
Expand Down