diff --git a/Linphone.xcodeproj/project.pbxproj b/Linphone.xcodeproj/project.pbxproj index 2c20f5320..fca904b83 100644 --- a/Linphone.xcodeproj/project.pbxproj +++ b/Linphone.xcodeproj/project.pbxproj @@ -10,6 +10,7 @@ 66C491F92B24D25B00CEA16D /* ConfigExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66C491F82B24D25A00CEA16D /* ConfigExtension.swift */; }; 66C491FB2B24D32600CEA16D /* CoreExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66C491FA2B24D32600CEA16D /* CoreExtension.swift */; }; 66C491FD2B24D36500CEA16D /* AudioRouteUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66C491FC2B24D36500CEA16D /* AudioRouteUtils.swift */; }; + 66C491FF2B24D4AC00CEA16D /* FileUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66C491FE2B24D4AC00CEA16D /* FileUtils.swift */; }; D706BA822ADD72D100278F45 /* DeviceRotationViewModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = D706BA812ADD72D100278F45 /* DeviceRotationViewModifier.swift */; }; D70C93DE2AC2D0F60063CA3B /* Localizable.xcstrings in Resources */ = {isa = PBXBuildFile; fileRef = D70C93DD2AC2D0F60063CA3B /* Localizable.xcstrings */; }; D717071E2AC5922E0037746F /* ColorExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = D717071D2AC5922E0037746F /* ColorExtension.swift */; }; @@ -92,6 +93,7 @@ 66C491F82B24D25A00CEA16D /* ConfigExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConfigExtension.swift; sourceTree = ""; }; 66C491FA2B24D32600CEA16D /* CoreExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CoreExtension.swift; sourceTree = ""; }; 66C491FC2B24D36500CEA16D /* AudioRouteUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioRouteUtils.swift; sourceTree = ""; }; + 66C491FE2B24D4AC00CEA16D /* FileUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileUtils.swift; sourceTree = ""; }; D706BA812ADD72D100278F45 /* DeviceRotationViewModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceRotationViewModifier.swift; sourceTree = ""; }; D70C93DD2AC2D0F60063CA3B /* Localizable.xcstrings */ = {isa = PBXFileReference; lastKnownFileType = text.json.xcstrings; path = Localizable.xcstrings; sourceTree = ""; }; D717071D2AC5922E0037746F /* ColorExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorExtension.swift; sourceTree = ""; }; @@ -209,6 +211,7 @@ 66C491FC2B24D36500CEA16D /* AudioRouteUtils.swift */, D7ADF5FF2AFE356400212231 /* Avatar.swift */, D7C48DF32AFA66F900D938CB /* EditContactController.swift */, + 66C491FE2B24D4AC00CEA16D /* FileUtils.swift */, D7D1698B2AE66FA500109A5C /* MagicSearchSingleton.swift */, D74C9D002ACB098C0021626A /* PermissionManager.swift */, D7C3650D2AF15BF200FE6142 /* PhotoPicker.swift */, @@ -610,6 +613,7 @@ D7B5066D2AEFA9B900CEB4E9 /* ContactInnerFragment.swift in Sources */, D7E6D04D2AEBD77600A57AAF /* CustomBottomSheet.swift in Sources */, D7C48DF42AFA66F900D938CB /* EditContactController.swift in Sources */, + 66C491FF2B24D4AC00CEA16D /* FileUtils.swift in Sources */, D74C9D012ACB098C0021626A /* PermissionManager.swift in Sources */, D7702EF22AC7205000557C00 /* WelcomeView.swift in Sources */, D732A9152B04C7FE00DB42BA /* HistoryListViewModel.swift in Sources */, diff --git a/Linphone/Utils/FileUtils.swift b/Linphone/Utils/FileUtils.swift new file mode 100644 index 000000000..d923462ed --- /dev/null +++ b/Linphone/Utils/FileUtils.swift @@ -0,0 +1,125 @@ +/* +* Copyright (c) 2010-2023 Belledonne Communications SARL. +* +* This file is part of linhome +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +*/ + +import UIKit +import linphonesw + +class FileUtil: NSObject { + public class func bundleFilePath(_ file: NSString) -> String? { + return Bundle.main.path(forResource: file.deletingPathExtension, ofType: file.pathExtension) + } + + public class func bundleFilePathAsUrl(_ file: NSString) -> URL? { + if let bPath = bundleFilePath(file) { + return URL.init(fileURLWithPath: bPath) + } + return nil + } + + public class func documentsDirectory() -> URL { + let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) + let documentsDirectory = paths[0] + return documentsDirectory + } + + public class func libraryDirectory() -> URL { + let paths = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask) + let documentsDirectory = paths[0] + return documentsDirectory + } + + public class func sharedContainerUrl() -> URL { + return FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: Config.appGroupName)! + } + + public class func ensureDirectoryExists(path: String) { + if !FileManager.default.fileExists(atPath: path) { + do { + try FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil) + } catch { + print(error) + } + } + } + + public class func ensureFileExists(path: String) { + if !FileManager.default.fileExists(atPath: path) { + FileManager.default.createFile(atPath: path, contents: nil, attributes: nil) + } + } + + public class func fileExists(path: String) -> Bool { + return FileManager.default.fileExists(atPath: path) + } + + public class func fileExistsAndIsNotEmpty(path: String) -> Bool { + guard FileManager.default.fileExists(atPath: path) else {return false} + do { + let attribute = try FileManager.default.attributesOfItem(atPath: path) + if let size = attribute[FileAttributeKey.size] as? NSNumber { + return size.doubleValue > 0 + } else { + return false + } + } catch { + print(error) + return false + } + } + + public class func write(string: String, toPath: String) { + do { + try string.write(to: URL(fileURLWithPath: toPath), atomically: true, encoding: String.Encoding.utf8) + } catch { + print(error) + } + } + + public class func delete(path: String) { + do { + try FileManager.default.removeItem(atPath: path) + } catch { + print(error) + } + } + + public class func copy(_ fromPath: String, _ toPath: String, overWrite: Bool) { + do { + if overWrite && fileExists(path: toPath) { + delete(path: toPath) + } + try FileManager.default.copyItem(at: URL(fileURLWithPath: fromPath), to: URL(fileURLWithPath: toPath)) + } catch { + print(error) + } + } + + // For debugging + + public class func showListOfFilesInSharedDir() { + let fileManager = FileManager.default + do { + let fileURLs = try fileManager.contentsOfDirectory(at: FileUtil.sharedContainerUrl(), includingPropertiesForKeys: nil) + fileURLs.forEach { print($0) } + } catch { + print("Error while enumerating files \(error.localizedDescription)") + } + } + +}