|
@@ -60,7 +60,6 @@ class NCNetworkingProcessUpload: NSObject {
|
|
|
var counterUpload: Int = 0
|
|
|
let sessionSelectors = [NCGlobal.shared.selectorUploadFile, NCGlobal.shared.selectorUploadAutoUpload, NCGlobal.shared.selectorUploadAutoUploadAll]
|
|
|
let metadatasUpload = NCManageDatabase.shared.getMetadatas(predicate: NSPredicate(format: "status == %d OR status == %d", NCGlobal.shared.metadataStatusInUpload, NCGlobal.shared.metadataStatusUploading))
|
|
|
- let chunckSize = CCUtility.getChunkSize() * 1000000
|
|
|
counterUpload = metadatasUpload.count
|
|
|
|
|
|
stopTimer()
|
|
@@ -80,9 +79,6 @@ class NCNetworkingProcessUpload: NSObject {
|
|
|
|
|
|
for metadata in metadatas {
|
|
|
|
|
|
- var metadataForUpload: tableMetadata?
|
|
|
- var metadataLivePhotoForUpload: tableMetadata?
|
|
|
-
|
|
|
// Different account
|
|
|
if self.appDelegate.account != metadata.account {
|
|
|
NCCommunicationCommon.shared.writeLog("Process auto upload skipped file: \(metadata.serverUrl)/\(metadata.fileNameView) on account: \(metadata.account), because the actual account is \(self.appDelegate.account).")
|
|
@@ -103,119 +99,41 @@ class NCNetworkingProcessUpload: NSObject {
|
|
|
// Is already in upload E2EE / CHUNK ? exit [ ONLY ONE IN QUEUE ]
|
|
|
for metadata in metadatasUpload {
|
|
|
if metadata.chunk || metadata.e2eEncrypted {
|
|
|
- self.startTimer()
|
|
|
- return
|
|
|
+ counterUpload = self.maxConcurrentOperationUpload
|
|
|
+ continue
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // OK EXTRACT
|
|
|
- let semaphore = Semaphore()
|
|
|
- if !metadata.assetLocalIdentifier.isEmpty {
|
|
|
- CCUtility.extractImageVideoFromAssetLocalIdentifier(forUpload: metadata, queue: queue) { extractMetadata, fileNamePath in
|
|
|
- if let metadata = extractMetadata {
|
|
|
- let toPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
|
|
|
- NCUtilityFileSystem.shared.moveFile(atPath: fileNamePath!, toPath: toPath)
|
|
|
- metadata.size = NCUtilityFileSystem.shared.getFileSize(filePath: toPath)
|
|
|
- // DETECT IF CHUNCK
|
|
|
- if chunckSize > 0 && metadata.size > chunckSize {
|
|
|
- metadata.chunk = true
|
|
|
- metadata.session = NCCommunicationCommon.shared.sessionIdentifierUpload
|
|
|
- }
|
|
|
- // DETECT IF E2EE
|
|
|
- if CCUtility.isFolderEncrypted(metadata.serverUrl, e2eEncrypted: metadata.e2eEncrypted, account: metadata.account, urlBase: metadata.urlBase) {
|
|
|
- metadata.e2eEncrypted = true
|
|
|
- }
|
|
|
- // update
|
|
|
- metadataForUpload = NCManageDatabase.shared.addMetadata(metadata)
|
|
|
- if metadata.livePhoto {
|
|
|
- let fetchAssets = PHAsset.fetchAssets(withLocalIdentifiers: [metadata.assetLocalIdentifier], options: nil)
|
|
|
- if fetchAssets.count > 0 {
|
|
|
- let ocId = NSUUID().uuidString
|
|
|
- let fileName = (metadata.fileName as NSString).deletingPathExtension + ".mov"
|
|
|
- let filePath = CCUtility.getDirectoryProviderStorageOcId(ocId, fileNameView: fileName)!
|
|
|
- CCUtility.extractLivePhotoAsset(fetchAssets.firstObject, filePath: filePath, queue: queue) { url in
|
|
|
- if url != nil {
|
|
|
- let metadataLivePhoto = NCManageDatabase.shared.createMetadata(account: metadata.account, user: metadata.user, userId: metadata.userId, fileName: fileName, fileNameView: fileName, ocId: ocId, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, url: "", contentType: "", isLivePhoto: true)
|
|
|
- metadataLivePhoto.classFile = NCCommunicationCommon.typeClassFile.video.rawValue
|
|
|
- metadataLivePhoto.e2eEncrypted = metadata.e2eEncrypted
|
|
|
- metadataLivePhoto.isAutoupload = metadata.isAutoupload
|
|
|
- metadataLivePhoto.session = metadata.session
|
|
|
- metadataLivePhoto.sessionSelector = metadata.sessionSelector
|
|
|
- metadataLivePhoto.size = NCUtilityFileSystem.shared.getFileSize(filePath: filePath)
|
|
|
- metadataLivePhoto.status = metadata.status
|
|
|
- if chunckSize > 0 && metadataLivePhoto.size > chunckSize {
|
|
|
- metadataLivePhoto.chunk = true
|
|
|
- metadataLivePhoto.session = NCCommunicationCommon.shared.sessionIdentifierUpload
|
|
|
- }
|
|
|
- metadataLivePhotoForUpload = NCManageDatabase.shared.addMetadata(metadataLivePhoto)
|
|
|
- }
|
|
|
- semaphore.continue()
|
|
|
- }
|
|
|
- } else {
|
|
|
- semaphore.continue()
|
|
|
- }
|
|
|
- } else {
|
|
|
- semaphore.continue()
|
|
|
- }
|
|
|
- } else {
|
|
|
- NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
|
|
|
- semaphore.continue()
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- semaphore.continue()
|
|
|
- }
|
|
|
- semaphore.wait()
|
|
|
-
|
|
|
- // Chunk
|
|
|
- if metadata.chunk && UIApplication.shared.applicationState == .active {
|
|
|
-// if metadata.session == NCNetworking.shared.sessionIdentifierBackgroundWWan && metadata.isAutoupload && NCNetworking.shared.networkReachability != NCCommunicationCommon.typeReachability.reachableEthernetOrWiFi {
|
|
|
-// continue
|
|
|
-// }
|
|
|
+ let (metadataForUpload, metadataLivePhotoForUpload) = self.extractMetadataFileLive(metadata: metadata, queue: queue)
|
|
|
|
|
|
+ // Upload
|
|
|
+ if let metadata = metadataForUpload {
|
|
|
+ if (metadata.e2eEncrypted || metadata.chunk) && UIApplication.shared.applicationState != .active { continue }
|
|
|
if let metadata = NCManageDatabase.shared.setMetadataStatus(ocId: metadata.ocId, status: NCGlobal.shared.metadataStatusInUpload) {
|
|
|
NCNetworking.shared.upload(metadata: metadata) {
|
|
|
- // start
|
|
|
- } completion: { _, _ in
|
|
|
- DispatchQueue.main.async {
|
|
|
- self.startTimer()
|
|
|
- }
|
|
|
- }
|
|
|
+ } completion: { _, _ in }
|
|
|
+ }
|
|
|
+ if metadata.e2eEncrypted || metadata.chunk {
|
|
|
+ counterUpload = self.maxConcurrentOperationUpload
|
|
|
} else {
|
|
|
- self.startTimer()
|
|
|
+ counterUpload += 1
|
|
|
}
|
|
|
- return
|
|
|
}
|
|
|
|
|
|
- // E2EE
|
|
|
- if metadata.e2eEncrypted && UIApplication.shared.applicationState == .active {
|
|
|
+ // Upload Live photo
|
|
|
+ if let metadata = metadataLivePhotoForUpload {
|
|
|
+ if (metadata.e2eEncrypted || metadata.chunk) && UIApplication.shared.applicationState != .active { continue }
|
|
|
if let metadata = NCManageDatabase.shared.setMetadataStatus(ocId: metadata.ocId, status: NCGlobal.shared.metadataStatusInUpload) {
|
|
|
NCNetworking.shared.upload(metadata: metadata) {
|
|
|
- // start
|
|
|
- } completion: { _, _ in
|
|
|
- DispatchQueue.main.async {
|
|
|
- self.startTimer()
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- self.startTimer()
|
|
|
+ } completion: { _, _ in }
|
|
|
}
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- counterUpload += 1
|
|
|
- if let metadata = NCManageDatabase.shared.setMetadataStatus(ocId: metadata.ocId, status: NCGlobal.shared.metadataStatusInUpload) {
|
|
|
- NCNetworking.shared.upload(metadata: metadata) {
|
|
|
- // start
|
|
|
- } completion: { _, _ in
|
|
|
- // completion
|
|
|
+ if metadata.e2eEncrypted || metadata.chunk {
|
|
|
+ counterUpload = self.maxConcurrentOperationUpload
|
|
|
+ } else {
|
|
|
+ counterUpload += 1
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- } else {
|
|
|
- self.startTimer()
|
|
|
- return
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -270,6 +188,91 @@ class NCNetworkingProcessUpload: NSObject {
|
|
|
|
|
|
// MARK: -
|
|
|
|
|
|
+ func extractMetadataFileLive(metadata: tableMetadata, queue: DispatchQueue) -> (metadataForUpload: tableMetadata?, metadataLivePhotoForUpload: tableMetadata?) {
|
|
|
+
|
|
|
+ var metadataForUpload: tableMetadata?
|
|
|
+ var metadataLivePhotoForUpload: tableMetadata?
|
|
|
+ let chunckSize = CCUtility.getChunkSize() * 1000000
|
|
|
+ let semaphore = Semaphore()
|
|
|
+
|
|
|
+ guard queue != .main else { return(nil, nil) }
|
|
|
+ guard !metadata.assetLocalIdentifier.isEmpty else {
|
|
|
+ let filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileName)!
|
|
|
+ metadata.size = NCUtilityFileSystem.shared.getFileSize(filePath: filePath)
|
|
|
+ let results = NCCommunicationCommon.shared.getInternalType(fileName: metadata.fileNameView, mimeType: metadata.contentType, directory: false)
|
|
|
+ metadata.contentType = results.mimeType
|
|
|
+ metadata.iconName = results.iconName
|
|
|
+ metadata.classFile = results.classFile
|
|
|
+ if let date = NCUtilityFileSystem.shared.getFileCreationDate(filePath: filePath) { metadata.creationDate = date }
|
|
|
+ if let date = NCUtilityFileSystem.shared.getFileModificationDate(filePath: filePath) { metadata.date = date }
|
|
|
+ // DETECT IF CHUNCK
|
|
|
+ if chunckSize > 0 && metadata.size > chunckSize {
|
|
|
+ metadata.chunk = true
|
|
|
+ metadata.session = NCCommunicationCommon.shared.sessionIdentifierUpload
|
|
|
+ }
|
|
|
+ // DETECT IF E2EE
|
|
|
+ if CCUtility.isFolderEncrypted(metadata.serverUrl, e2eEncrypted: metadata.e2eEncrypted, account: metadata.account, urlBase: metadata.urlBase) {
|
|
|
+ metadata.e2eEncrypted = true
|
|
|
+ }
|
|
|
+ let metadata = NCManageDatabase.shared.addMetadata(metadata)
|
|
|
+ return (metadata, nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ CCUtility.extractImageVideoFromAssetLocalIdentifier(forUpload: metadata, queue: queue) { extractMetadata, fileNamePath in
|
|
|
+ if let metadata = extractMetadata {
|
|
|
+ let toPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
|
|
|
+ NCUtilityFileSystem.shared.moveFile(atPath: fileNamePath!, toPath: toPath)
|
|
|
+ metadata.size = NCUtilityFileSystem.shared.getFileSize(filePath: toPath)
|
|
|
+ // DETECT IF CHUNCK
|
|
|
+ if chunckSize > 0 && metadata.size > chunckSize {
|
|
|
+ metadata.chunk = true
|
|
|
+ metadata.session = NCCommunicationCommon.shared.sessionIdentifierUpload
|
|
|
+ }
|
|
|
+ // DETECT IF E2EE
|
|
|
+ if CCUtility.isFolderEncrypted(metadata.serverUrl, e2eEncrypted: metadata.e2eEncrypted, account: metadata.account, urlBase: metadata.urlBase) {
|
|
|
+ metadata.e2eEncrypted = true
|
|
|
+ }
|
|
|
+ // update
|
|
|
+ metadataForUpload = NCManageDatabase.shared.addMetadata(metadata)
|
|
|
+ }
|
|
|
+ semaphore.continue()
|
|
|
+ }
|
|
|
+ semaphore.wait()
|
|
|
+
|
|
|
+ let fetchAssets = PHAsset.fetchAssets(withLocalIdentifiers: [metadata.assetLocalIdentifier], options: nil)
|
|
|
+ if metadata.livePhoto, fetchAssets.count > 0 {
|
|
|
+ let ocId = NSUUID().uuidString
|
|
|
+ let fileName = (metadata.fileName as NSString).deletingPathExtension + ".mov"
|
|
|
+ let filePath = CCUtility.getDirectoryProviderStorageOcId(ocId, fileNameView: fileName)!
|
|
|
+ CCUtility.extractLivePhotoAsset(fetchAssets.firstObject, filePath: filePath, queue: queue) { url in
|
|
|
+ if url != nil {
|
|
|
+ let metadataLivePhoto = NCManageDatabase.shared.createMetadata(account: metadata.account, user: metadata.user, userId: metadata.userId, fileName: fileName, fileNameView: fileName, ocId: ocId, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, url: "", contentType: "", isLivePhoto: true)
|
|
|
+ metadataLivePhoto.classFile = NCCommunicationCommon.typeClassFile.video.rawValue
|
|
|
+ metadataLivePhoto.e2eEncrypted = metadata.e2eEncrypted
|
|
|
+ metadataLivePhoto.isAutoupload = metadata.isAutoupload
|
|
|
+ metadataLivePhoto.session = metadata.session
|
|
|
+ metadataLivePhoto.sessionSelector = metadata.sessionSelector
|
|
|
+ metadataLivePhoto.size = NCUtilityFileSystem.shared.getFileSize(filePath: filePath)
|
|
|
+ metadataLivePhoto.status = metadata.status
|
|
|
+ if chunckSize > 0 && metadataLivePhoto.size > chunckSize {
|
|
|
+ metadataLivePhoto.chunk = true
|
|
|
+ metadataLivePhoto.session = NCCommunicationCommon.shared.sessionIdentifierUpload
|
|
|
+ }
|
|
|
+ metadataLivePhotoForUpload = NCManageDatabase.shared.addMetadata(metadataLivePhoto)
|
|
|
+ }
|
|
|
+ semaphore.continue()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ semaphore.wait()
|
|
|
+
|
|
|
+ if metadataForUpload == nil {
|
|
|
+ NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
|
|
|
+ }
|
|
|
+ return(metadataForUpload, metadataLivePhotoForUpload)
|
|
|
+ }
|
|
|
+
|
|
|
+ // MARK: -
|
|
|
+
|
|
|
@objc func createProcessUploads(metadatas: [tableMetadata], verifyAlreadyExists: Bool = false) {
|
|
|
|
|
|
var metadatasForUpload: [tableMetadata] = []
|