Skip to content

Commit

Permalink
VSP-848 [iOS] File paths get corrupted during upload and get stuck in…
Browse files Browse the repository at this point in the history
… an error state
  • Loading branch information
v-rusu committed Aug 10, 2022
1 parent ae251c4 commit 05511c0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 24 deletions.
24 changes: 12 additions & 12 deletions Permanent.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3182,7 +3182,7 @@
CODE_SIGN_ENTITLEMENTS = Permanent/Assets/Entitlements/Permanent.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 80;
CURRENT_PROJECT_VERSION = 81;
DEVELOPMENT_TEAM = C8YKZNBVWT;
ENV_VARS_FILENAME = "env-vars.sh";
INFOPLIST_FILE = "$(SRCROOT)/Permanent/Assets/Info.plist";
Expand Down Expand Up @@ -3210,7 +3210,7 @@
CODE_SIGN_ENTITLEMENTS = Permanent/PermanentRelease.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 80;
CURRENT_PROJECT_VERSION = 81;
DEVELOPMENT_TEAM = C8YKZNBVWT;
ENV_VARS_FILENAME = "env-vars.sh";
INFOPLIST_FILE = "$(SRCROOT)/Permanent/Assets/Info.plist";
Expand Down Expand Up @@ -3329,7 +3329,7 @@
CODE_SIGN_ENTITLEMENTS = ShareExtension/ShareExtensionDebug.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 80;
CURRENT_PROJECT_VERSION = 81;
DEVELOPMENT_TEAM = C8YKZNBVWT;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = ShareExtension/Info.plist;
Expand Down Expand Up @@ -3361,7 +3361,7 @@
CODE_SIGN_ENTITLEMENTS = "ShareExtension/ShareExtensionDEV-Debug.entitlements";
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 80;
CURRENT_PROJECT_VERSION = 81;
DEVELOPMENT_TEAM = C8YKZNBVWT;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = ShareExtension/Info.plist;
Expand Down Expand Up @@ -3393,7 +3393,7 @@
CODE_SIGN_ENTITLEMENTS = ShareExtension/ShareExtensionRelease.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 80;
CURRENT_PROJECT_VERSION = 81;
DEVELOPMENT_TEAM = C8YKZNBVWT;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = ShareExtension/Info.plist;
Expand Down Expand Up @@ -3424,7 +3424,7 @@
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CODE_SIGN_IDENTITY = "iPhone Distribution";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 80;
CURRENT_PROJECT_VERSION = 81;
DEVELOPMENT_TEAM = C8YKZNBVWT;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = ShareExtension/Info.plist;
Expand Down Expand Up @@ -3516,7 +3516,7 @@
CODE_SIGN_ENTITLEMENTS = "Permanent/PermanentDEV-Debug.entitlements";
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 80;
CURRENT_PROJECT_VERSION = 81;
DEVELOPMENT_TEAM = C8YKZNBVWT;
ENV_VARS_FILENAME = "env-vars-staging.sh";
INFOPLIST_FILE = "$(SRCROOT)/Permanent/Assets/Info.plist";
Expand Down Expand Up @@ -3598,7 +3598,7 @@
CODE_SIGN_ENTITLEMENTS = "Permanent/Assets/Entitlements/Permanent-Staging.entitlements";
CODE_SIGN_IDENTITY = "iPhone Distribution";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 80;
CURRENT_PROJECT_VERSION = 81;
DEVELOPMENT_TEAM = C8YKZNBVWT;
ENV_VARS_FILENAME = "env-vars-staging.sh";
INFOPLIST_FILE = "$(SRCROOT)/Permanent/Assets/Info.plist";
Expand All @@ -3623,7 +3623,7 @@
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 80;
CURRENT_PROJECT_VERSION = 81;
DEVELOPMENT_TEAM = C8YKZNBVWT;
INFOPLIST_FILE = PushExtension/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 12.1;
Expand All @@ -3647,7 +3647,7 @@
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 80;
CURRENT_PROJECT_VERSION = 81;
DEVELOPMENT_TEAM = C8YKZNBVWT;
INFOPLIST_FILE = PushExtension/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 12.1;
Expand All @@ -3671,7 +3671,7 @@
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 80;
CURRENT_PROJECT_VERSION = 81;
DEVELOPMENT_TEAM = C8YKZNBVWT;
INFOPLIST_FILE = PushExtension/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 12.1;
Expand All @@ -3695,7 +3695,7 @@
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CODE_SIGN_IDENTITY = "iPhone Distribution";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 80;
CURRENT_PROJECT_VERSION = 81;
DEVELOPMENT_TEAM = C8YKZNBVWT;
INFOPLIST_FILE = PushExtension/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 12.1;
Expand Down
29 changes: 25 additions & 4 deletions Permanent/Extensions/PHAssetExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,46 @@

import Photos

struct AssetDescriptor {
let url: URL
let name: String
}

extension PHAsset {
func getURL(completionHandler: @escaping ((_ responseURL: URL?) -> Void)) {
func getURL(completionHandler: @escaping ((_ descriptor: AssetDescriptor?) -> Void)) {
if self.mediaType == .image {
let options = PHContentEditingInputRequestOptions()
options.isNetworkAccessAllowed = true
options.canHandleAdjustmentData = { (_: PHAdjustmentData) -> Bool in
false
}
self.requestContentEditingInput(with: options, completionHandler: { (contentEditingInput: PHContentEditingInput?, _: [AnyHashable: Any]) -> Void in
completionHandler(contentEditingInput?.fullSizeImageURL as URL?)
guard let url = contentEditingInput?.fullSizeImageURL as URL? else {
completionHandler(nil)
return
}

var fileName = url.lastPathComponent
if url.lastPathComponent.contains("FullSizeRender") {
let fileExtention = url.lastPathComponent.components(separatedBy: ".").last ?? ""
fileName = url.deletingLastPathComponent().deletingLastPathComponent().lastPathComponent
if fileExtention.isNotEmpty { fileName.append(".\(fileExtention)") }
}

let descriptor = AssetDescriptor(url: url, name: fileName)
completionHandler(descriptor)
})
} else if self.mediaType == .video {
let options = PHVideoRequestOptions()
options.version = .original
options.isNetworkAccessAllowed = true
PHImageManager.default().requestAVAsset(forVideo: self, options: options, resultHandler: { (asset: AVAsset?, _: AVAudioMix?, _: [AnyHashable: Any]?) -> Void in
if let urlAsset = asset as? AVURLAsset {
let localVideoUrl: URL = urlAsset.url as URL
completionHandler(localVideoUrl)
let url: URL = urlAsset.url as URL
let fileName = url.lastPathComponent

let descriptor = AssetDescriptor(url: url, name: fileName)
completionHandler(descriptor)
} else {
completionHandler(nil)
}
Expand Down
11 changes: 6 additions & 5 deletions Permanent/Helpers/FileHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ class FileHelper {
}
}

func copyFile(withURL from: URL, usingAppSuiteGroup suiteGroupName: String? = nil) throws -> URL {
var to = self.uploadDirectoryURL!.appendingPathComponent(from.lastPathComponent)
func copyFile(withURL from: URL, name: String? = nil, usingAppSuiteGroup suiteGroupName: String? = nil) throws -> URL {
let toLastPathComponent = name ?? from.lastPathComponent
var to = self.uploadDirectoryURL!.appendingPathComponent(toLastPathComponent)

if let suiteGroup = suiteGroupName {
to = FileManager().containerURL(forSecurityApplicationGroupIdentifier: suiteGroup)!.appendingPathComponent(from.lastPathComponent)
to = FileManager().containerURL(forSecurityApplicationGroupIdentifier: suiteGroup)!.appendingPathComponent(toLastPathComponent)
}
// If we already have an item stored at this path, we remove it first.
try? FileManager.default.removeItem(at: to)
Expand Down Expand Up @@ -86,7 +87,7 @@ class FileHelper {
func hasFile(named name: String, isDownload: Bool = true) -> Bool {
var fileURL: URL
if isDownload {
fileURL = self.defaultDirectoryURL!
fileURL = self.defaultDirectoryURL!
} else {
fileURL = self.uploadDirectoryURL!
}
Expand All @@ -100,7 +101,7 @@ class FileHelper {
if hasFile(named: name, isDownload: isDownload) {
var fileURL: URL
if isDownload {
fileURL = self.defaultDirectoryURL!
fileURL = self.defaultDirectoryURL!
} else {
fileURL = self.uploadDirectoryURL!
}
Expand Down
6 changes: 3 additions & 3 deletions Permanent/ViewModels/FilesViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -354,14 +354,14 @@ class FilesViewModel: NSObject, ViewModelInterface {
assets.forEach { photo in
dispatchGroup.enter()

photo.getURL { url in
guard let imageURL = url else {
photo.getURL { descriptor in
guard let fileDescriptor = descriptor else {
dispatchGroup.leave()
return
}

do {
let localURL = try FileHelper().copyFile(withURL: imageURL)
let localURL = try FileHelper().copyFile(withURL: fileDescriptor.url, name: fileDescriptor.name)
urls.append(localURL)
} catch {
print(error)
Expand Down

0 comments on commit 05511c0

Please sign in to comment.