diff --git a/iOSClient/Utility/CCUtility.h b/iOSClient/Utility/CCUtility.h index 767cbab1af..d08824bfe7 100644 --- a/iOSClient/Utility/CCUtility.h +++ b/iOSClient/Utility/CCUtility.h @@ -35,6 +35,5 @@ @interface CCUtility : NSObject + (NSString *)createFileName:(NSString *)fileName fileDate:(NSDate *)fileDate fileType:(PHAssetMediaType)fileType keyFileName:(NSString *)keyFileName keyFileNameType:(NSString *)keyFileNameType keyFileNameOriginal:(NSString *)keyFileNameOriginal forcedNewFileName:(BOOL)forcedNewFileName; -+ (NSString *)getMimeType:(NSString *)fileNameView; @end diff --git a/iOSClient/Utility/CCUtility.m b/iOSClient/Utility/CCUtility.m index 13bfc0a68c..b232fcc47f 100644 --- a/iOSClient/Utility/CCUtility.m +++ b/iOSClient/Utility/CCUtility.m @@ -135,28 +135,4 @@ + (NSString *)createFileName:(NSString *)fileName fileDate:(NSDate *)fileDate fi return fileName; } -+ (NSString *)getMimeType:(NSString *)fileNameView -{ - CFStringRef fileUTI = nil; - NSString *returnFileUTI = nil; - - if ([fileNameView isEqualToString:@"."]) { - - return returnFileUTI; - - } else { - CFStringRef fileExtension = (__bridge CFStringRef)[fileNameView pathExtension]; - NSString *ext = (__bridge NSString *)fileExtension; - ext = ext.uppercaseString; - fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL); - - if (fileUTI != nil) { - returnFileUTI = (__bridge NSString *)fileUTI; - CFRelease(fileUTI); - } - } - - return returnFileUTI; -} - @end diff --git a/iOSClient/Utility/NCUtility.swift b/iOSClient/Utility/NCUtility.swift index 9a170719b1..43260ca6bb 100644 --- a/iOSClient/Utility/NCUtility.swift +++ b/iOSClient/Utility/NCUtility.swift @@ -30,10 +30,9 @@ import Photos import Alamofire class NCUtility: NSObject { - let utilityFileSystem = NCUtilityFileSystem() - @objc func isSimulatorOrTestFlight() -> Bool { + func isSimulatorOrTestFlight() -> Bool { guard let path = Bundle.main.appStoreReceiptURL?.path else { return false } @@ -48,16 +47,15 @@ class NCUtility: NSObject { } func isRichDocument(_ metadata: tableMetadata) -> Bool { - guard let mimeType = CCUtility.getMimeType(metadata.fileNameView) else { - return true - } - - // contentype + guard metadata.fileNameView != "." else { return false } + let fileExtension = (metadata.fileNameView as NSString).pathExtension + guard !fileExtension.isEmpty else { return false } + guard let mimeType = UTType(tag: fileExtension.uppercased(), tagClass: .filenameExtension, conformingTo: nil)?.identifier else { return false } + /// contentype if !NCGlobal.shared.capabilityRichDocumentsMimetypes.filter({ $0.contains(metadata.contentType) || $0.contains("text/plain") }).isEmpty { return true } - - // mimetype + /// mimetype if !NCGlobal.shared.capabilityRichDocumentsMimetypes.isEmpty && mimeType.components(separatedBy: ".").count > 2 { let mimeTypeArray = mimeType.components(separatedBy: ".") let mimeType = mimeTypeArray[mimeTypeArray.count - 2] + "." + mimeTypeArray[mimeTypeArray.count - 1] @@ -65,27 +63,20 @@ class NCUtility: NSObject { return true } } - return false } func isDirectEditing(account: String, contentType: String) -> [String] { - var editor: [String] = [] - - guard let results = NCManageDatabase.shared.getDirectEditingEditors(account: account) else { - return editor - } + guard let results = NCManageDatabase.shared.getDirectEditingEditors(account: account) else { return editor } for result: tableDirectEditingEditors in results { for mimetype in result.mimetypes { if mimetype == contentType { editor.append(result.editor) } - // HARDCODE // https://github.com/nextcloud/text/issues/913 - if mimetype == "text/markdown" && contentType == "text/x-markdown" { editor.append(result.editor) } @@ -99,12 +90,10 @@ class NCUtility: NSObject { } } } - return Array(Set(editor)) } func permissionsContainsString(_ metadataPermissions: String, permissions: String) -> Bool { - for char in permissions { if metadataPermissions.contains(char) == false { return false @@ -126,7 +115,6 @@ class NCUtility: NSObject { } func getCustomUserAgentOnlyOffice() -> String { - let appVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString")! if UIDevice.current.userInterfaceIdiom == .pad { return "Mozilla/5.0 (iPad) Nextcloud-iOS/\(appVersion)" @@ -135,22 +123,20 @@ class NCUtility: NSObject { } } - @objc func isQuickLookDisplayable(metadata: tableMetadata) -> Bool { + func isQuickLookDisplayable(metadata: tableMetadata) -> Bool { return true } - @objc func ocIdToFileId(ocId: String?) -> String? { - + func ocIdToFileId(ocId: String?) -> String? { guard let ocId = ocId else { return nil } - let items = ocId.components(separatedBy: "oc") + if items.count < 2 { return nil } guard let intFileId = Int(items[0]) else { return nil } return String(intFileId) } func getUserStatus(userIcon: String?, userStatus: String?, userMessage: String?) -> (onlineStatus: UIImage?, statusMessage: String, descriptionMessage: String) { - var onlineStatus: UIImage? var statusMessage: String = "" var descriptionMessage: String = "" @@ -207,7 +193,6 @@ class NCUtility: NSObject { */ func compare(tolerance: Float, expected: Data, observed: Data) throws -> Bool { - enum customError: Error { case unableToGetUIImageFromData case unableToGetCGImageFromData @@ -311,12 +296,9 @@ class NCUtility: NSObject { // https://stackoverflow.com/questions/27556807/swift-pointer-problems-with-mach-task-basic-info/27559770#27559770 func getMemoryUsedAndDeviceTotalInMegabytes() -> (Float, Float) { - var usedmegabytes: Float = 0 - let totalbytes = Float(ProcessInfo.processInfo.physicalMemory) let totalmegabytes = totalbytes / 1024.0 / 1024.0 - var info = mach_task_basic_info() var count = mach_msg_type_number_t(MemoryLayout.size) / 4 @@ -340,7 +322,6 @@ class NCUtility: NSObject { } func removeForbiddenCharacters(_ fileName: String) -> String { - var fileName = fileName for character in NCGlobal.shared.forbiddenCharacters { fileName = fileName.replacingOccurrences(of: character, with: "") diff --git a/iOSClient/Utility/NCUtilityFileSystem.swift b/iOSClient/Utility/NCUtilityFileSystem.swift index 833028d61d..e6747af111 100644 --- a/iOSClient/Utility/NCUtilityFileSystem.swift +++ b/iOSClient/Utility/NCUtilityFileSystem.swift @@ -26,19 +26,13 @@ import NextcloudKit import PhotosUI class NCUtilityFileSystem: NSObject { - let fileManager = FileManager.default - - // MARK: - - var directoryGroup: String { return fileManager.containerURL(forSecurityApplicationGroupIdentifier: NCBrandOptions.shared.capabilitiesGroups)?.path ?? "" } - var directoryDocuments: String { return NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first ?? "" } - var directoryCertificates: String { guard let directoryGroup = fileManager.containerURL(forSecurityApplicationGroupIdentifier: NCBrandOptions.shared.capabilitiesGroups) else { return "" } let path = directoryGroup.appendingPathComponent(NCGlobal.shared.appCertificates).path @@ -49,7 +43,6 @@ class NCUtilityFileSystem: NSObject { } return path } - var directoryUserData: String { guard let directoryGroup = fileManager.containerURL(forSecurityApplicationGroupIdentifier: NCBrandOptions.shared.capabilitiesGroups) else { return "" } let path = directoryGroup.appendingPathComponent(NCGlobal.shared.appUserData).path @@ -60,7 +53,6 @@ class NCUtilityFileSystem: NSObject { } return path } - var directoryScan: String { guard let directoryGroup = fileManager.containerURL(forSecurityApplicationGroupIdentifier: NCBrandOptions.shared.capabilitiesGroups) else { return "" } let path = directoryGroup.appendingPathComponent(NCGlobal.shared.appScan).path @@ -71,8 +63,7 @@ class NCUtilityFileSystem: NSObject { } return path } - - @objc var directoryProviderStorage: String { + var directoryProviderStorage: String { guard let directoryGroup = fileManager.containerURL(forSecurityApplicationGroupIdentifier: NCBrandOptions.shared.capabilitiesGroups) else { return "" } let path = directoryGroup.appendingPathComponent(NCGlobal.shared.directoryProviderStorage).path if !fileManager.fileExists(atPath: path) { @@ -83,7 +74,9 @@ class NCUtilityFileSystem: NSObject { return path } - @objc func getDirectoryProviderStorageOcId(_ ocId: String) -> String { + // MARK: - + + func getDirectoryProviderStorageOcId(_ ocId: String) -> String { let path = directoryProviderStorage + "/" + ocId if !fileManager.fileExists(atPath: path) { do { @@ -157,7 +150,7 @@ class NCUtilityFileSystem: NSObject { return false } - @objc func createDirectoryStandard() { + func createDirectoryStandard() { guard let directoryGroup = fileManager.containerURL(forSecurityApplicationGroupIdentifier: NCBrandOptions.shared.capabilitiesGroups)?.path else { return } if !fileManager.fileExists(atPath: directoryDocuments) { try? fileManager.createDirectory(atPath: directoryDocuments, withIntermediateDirectories: true) } let appDatabaseNextcloud = directoryGroup + "/" + NCGlobal.shared.appDatabaseNextcloud @@ -176,29 +169,29 @@ class NCUtilityFileSystem: NSObject { } } - @objc func removeGroupApplicationSupport() { + func removeGroupApplicationSupport() { let path = directoryGroup + "/" + NCGlobal.shared.appApplicationSupport try? fileManager.removeItem(atPath: path) } - @objc func removeGroupLibraryDirectory() { + func removeGroupLibraryDirectory() { try? fileManager.removeItem(atPath: directoryScan) try? fileManager.removeItem(atPath: directoryUserData) } - @objc func removeGroupDirectoryProviderStorage() { + func removeGroupDirectoryProviderStorage() { try? fileManager.removeItem(atPath: directoryProviderStorage) } - @objc func removeDocumentsDirectory() { + func removeDocumentsDirectory() { try? fileManager.removeItem(atPath: directoryDocuments) } - @objc func removeTemporaryDirectory() { + func removeTemporaryDirectory() { try? fileManager.removeItem(atPath: NSTemporaryDirectory()) } - @objc func emptyTemporaryDirectory() { + func emptyTemporaryDirectory() { do { let files = try fileManager.contentsOfDirectory(atPath: NSTemporaryDirectory()) for file in files { @@ -226,7 +219,6 @@ class NCUtilityFileSystem: NSObject { } func isDirectoryE2EETop(account: String, serverUrl: String) -> Bool { - guard let serverUrl = serverUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { return false } if let url = URL(string: serverUrl)?.deletingLastPathComponent(), @@ -239,7 +231,6 @@ class NCUtilityFileSystem: NSObject { } func getDirectoryE2EETop(serverUrl: String, account: String) -> tableDirectory? { - guard var serverUrl = serverUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { return nil } var top: tableDirectory? @@ -261,8 +252,7 @@ class NCUtilityFileSystem: NSObject { // MARK: - - @objc func getFileSize(filePath: String) -> Int64 { - + func getFileSize(filePath: String) -> Int64 { do { let attributes = try fileManager.attributesOfItem(atPath: filePath) return attributes[FileAttributeKey.size] as? Int64 ?? 0 @@ -272,8 +262,7 @@ class NCUtilityFileSystem: NSObject { return 0 } - @objc func getFileModificationDate(filePath: String) -> NSDate? { - + func getFileModificationDate(filePath: String) -> NSDate? { do { let attributes = try fileManager.attributesOfItem(atPath: filePath) return attributes[FileAttributeKey.modificationDate] as? NSDate @@ -283,8 +272,7 @@ class NCUtilityFileSystem: NSObject { return nil } - @objc func getFileCreationDate(filePath: String) -> NSDate? { - + func getFileCreationDate(filePath: String) -> NSDate? { do { let attributes = try fileManager.attributesOfItem(atPath: filePath) return attributes[FileAttributeKey.creationDate] as? NSDate @@ -294,8 +282,7 @@ class NCUtilityFileSystem: NSObject { return nil } - @objc func writeFile(fileURL: URL, text: String) -> Bool { - + func writeFile(fileURL: URL, text: String) -> Bool { do { try FileManager.default.removeItem(at: fileURL) } catch { @@ -311,8 +298,7 @@ class NCUtilityFileSystem: NSObject { } } - @objc func removeFile(atPath: String) { - + func removeFile(atPath: String) { do { try FileManager.default.removeItem(atPath: atPath) } catch { @@ -321,8 +307,7 @@ class NCUtilityFileSystem: NSObject { } @discardableResult - @objc func moveFile(atPath: String, toPath: String) -> Bool { - + func moveFile(atPath: String, toPath: String) -> Bool { if atPath == toPath { return true } do { @@ -342,8 +327,7 @@ class NCUtilityFileSystem: NSObject { } @discardableResult - @objc func copyFile(atPath: String, toPath: String) -> Bool { - + func copyFile(atPath: String, toPath: String) -> Bool { if atPath == toPath { return true } do { @@ -363,7 +347,6 @@ class NCUtilityFileSystem: NSObject { @discardableResult func copyFile(at: URL, to: URL) -> Bool { - if at == to { return true } do { @@ -381,32 +364,27 @@ class NCUtilityFileSystem: NSObject { } } - @objc func moveFileInBackground(atPath: String, toPath: String) { - + func moveFileInBackground(atPath: String, toPath: String) { if atPath == toPath { return } - DispatchQueue.global().async { - try? FileManager.default.removeItem(atPath: toPath) try? FileManager.default.copyItem(atPath: atPath, toPath: toPath) try? FileManager.default.removeItem(atPath: atPath) } } - @objc func linkItem(atPath: String, toPath: String) { - + func linkItem(atPath: String, toPath: String) { try? FileManager.default.removeItem(atPath: toPath) try? FileManager.default.linkItem(atPath: atPath, toPath: toPath) } // MARK: - - @objc func getHomeServer(urlBase: String, userId: String) -> String { + func getHomeServer(urlBase: String, userId: String) -> String { return urlBase + "/remote.php/dav/files/" + userId } - @objc func getPath(path: String, user: String, fileName: String? = nil) -> String { - + func getPath(path: String, user: String, fileName: String? = nil) -> String { var path = path.replacingOccurrences(of: "/remote.php/dav/files/" + user, with: "") if let fileName = fileName { path += fileName @@ -414,13 +392,9 @@ class NCUtilityFileSystem: NSObject { return path } - @objc func deleteLastPath(serverUrlPath: String, home: String? = nil) -> String? { - + func deleteLastPath(serverUrlPath: String, home: String? = nil) -> String? { var returnString: String? - - if home == serverUrlPath { - return serverUrlPath - } + if home == serverUrlPath { return serverUrlPath } if let serverUrlPath = serverUrlPath.urlEncoded, let url = URL(string: serverUrlPath) { if let path = url.deletingLastPathComponent().absoluteString.removingPercentEncoding { @@ -435,7 +409,6 @@ class NCUtilityFileSystem: NSObject { } func stringAppendServerUrl(_ serverUrl: String, addFileName: String) -> String { - if addFileName.isEmpty { return serverUrl } else if serverUrl.last == "/" { @@ -445,8 +418,7 @@ class NCUtilityFileSystem: NSObject { } } - @objc func getFileNamePath(_ fileName: String, serverUrl: String, urlBase: String, userId: String) -> String { - + func getFileNamePath(_ fileName: String, serverUrl: String, urlBase: String, userId: String) -> String { let home = getHomeServer(urlBase: urlBase, userId: userId) var fileNamePath = serverUrl.replacingOccurrences(of: home, with: "") + "/" + fileName if fileNamePath.first == "/" { @@ -455,56 +427,51 @@ class NCUtilityFileSystem: NSObject { return fileNamePath } - @objc func createFileName(_ fileName: String, serverUrl: String, account: String) -> String { - + func createFileName(_ fileName: String, serverUrl: String, account: String) -> String { var resultFileName = fileName var exitLoop = false - while exitLoop == false { - - if NCManageDatabase.shared.getMetadata(predicate: NSPredicate(format: "fileNameView == %@ AND serverUrl == %@ AND account == %@", resultFileName, serverUrl, account)) != nil { + while exitLoop == false { + if NCManageDatabase.shared.getMetadata(predicate: NSPredicate(format: "fileNameView == %@ AND serverUrl == %@ AND account == %@", resultFileName, serverUrl, account)) != nil { + var name = NSString(string: resultFileName).deletingPathExtension + let ext = NSString(string: resultFileName).pathExtension + let characters = Array(name) - var name = NSString(string: resultFileName).deletingPathExtension - let ext = NSString(string: resultFileName).pathExtension - let characters = Array(name) - - if characters.count < 2 { + if characters.count < 2 { + if ext.isEmpty { + resultFileName = name + " " + "1" + } else { + resultFileName = name + " " + "1" + "." + ext + } + } else { + let space = characters[characters.count - 2] + let numChar = characters[characters.count - 1] + var num = Int(String(numChar)) + if space == " " && num != nil { + name = String(name.dropLast()) + num = num! + 1 if ext.isEmpty { - resultFileName = name + " " + "1" + resultFileName = name + "\(num!)" } else { - resultFileName = name + " " + "1" + "." + ext + resultFileName = name + "\(num!)" + "." + ext } } else { - let space = characters[characters.count - 2] - let numChar = characters[characters.count - 1] - var num = Int(String(numChar)) - if space == " " && num != nil { - name = String(name.dropLast()) - num = num! + 1 - if ext.isEmpty { - resultFileName = name + "\(num!)" - } else { - resultFileName = name + "\(num!)" + "." + ext - } + if ext.isEmpty { + resultFileName = name + " " + "1" } else { - if ext.isEmpty { - resultFileName = name + " " + "1" - } else { - resultFileName = name + " " + "1" + "." + ext - } + resultFileName = name + " " + "1" + "." + ext } } - - } else { - exitLoop = true } + } else { + exitLoop = true + } } return resultFileName } func createFileNameDate(_ fileName: String, ext: String) -> String { - let formatter = DateFormatter() formatter.dateFormat = "yy-MM-dd HH-mm-ss" let fileNameDate = formatter.string(from: Date()) @@ -520,8 +487,7 @@ class NCUtilityFileSystem: NSObject { } } - @objc func getDirectorySize(directory: String) -> Int64 { - + func getDirectorySize(directory: String) -> Int64 { let url = URL(fileURLWithPath: directory) let manager = FileManager.default var totalSize: Int64 = 0 @@ -539,16 +505,14 @@ class NCUtilityFileSystem: NSObject { return totalSize } - @objc func transformedSize(_ bytes: Int64) -> String { + func transformedSize(_ bytes: Int64) -> String { let formatter: ByteCountFormatter = ByteCountFormatter() formatter.countStyle = .binary return formatter.string(fromByteCount: bytes) } func cleanUp(directory: String, days: TimeInterval) { - if days == 0 { return} - let minimumDate = Date().addingTimeInterval(-days * 24 * 60 * 60) let url = URL(fileURLWithPath: directory) var offlineDir: [String] = [] @@ -604,7 +568,6 @@ class NCUtilityFileSystem: NSObject { } func createGranularityPath(asset: PHAsset? = nil, serverUrl: String? = nil) -> String { - let autoUploadSubfolderGranularity = NCManageDatabase.shared.getAccountAutoUploadSubfolderGranularity() let dateFormatter = DateFormatter() let date = asset?.creationDate ?? Date()