2022-04-19 12:29:03 +04:00
|
|
|
//
|
|
|
|
// FileUtils.swift
|
|
|
|
// SimpleX (iOS)
|
|
|
|
//
|
|
|
|
// Created by JRoberts on 15.04.2022.
|
|
|
|
// Copyright © 2022 SimpleX Chat. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2022-05-31 07:55:13 +01:00
|
|
|
import OSLog
|
|
|
|
|
|
|
|
let logger = Logger()
|
2022-04-19 12:29:03 +04:00
|
|
|
|
2022-04-25 12:44:24 +04:00
|
|
|
// maximum image file size to be auto-accepted
|
2022-11-26 12:43:26 +04:00
|
|
|
public let MAX_IMAGE_SIZE: Int64 = 236700
|
2022-04-25 12:44:24 +04:00
|
|
|
|
2022-12-24 00:22:12 +03:00
|
|
|
public let MAX_IMAGE_SIZE_AUTO_RCV: Int64 = MAX_IMAGE_SIZE * 2
|
|
|
|
|
2023-03-24 15:20:15 +04:00
|
|
|
//public let MAX_FILE_SIZE_SMP: Int64 = 8000000 // TODO distinguish between XFTP and SMP files
|
|
|
|
public let MAX_FILE_SIZE: Int64 = 1_073_741_824
|
2022-05-04 09:10:36 +04:00
|
|
|
|
2022-11-26 12:43:26 +04:00
|
|
|
public let MAX_VOICE_MESSAGE_LENGTH = TimeInterval(30)
|
|
|
|
|
|
|
|
public let MAX_VOICE_MESSAGE_SIZE_INLINE_SEND: Int64 = 94680
|
2022-11-24 21:18:28 +04:00
|
|
|
|
2022-09-17 16:41:20 +04:00
|
|
|
private let CHAT_DB: String = "_chat.db"
|
|
|
|
|
|
|
|
private let AGENT_DB: String = "_agent.db"
|
|
|
|
|
|
|
|
private let CHAT_DB_BAK: String = "_chat.db.bak"
|
|
|
|
|
|
|
|
private let AGENT_DB_BAK: String = "_agent.db.bak"
|
|
|
|
|
2022-06-24 13:52:20 +01:00
|
|
|
public func getDocumentsDirectory() -> URL {
|
|
|
|
FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
|
|
|
|
}
|
|
|
|
|
|
|
|
func getGroupContainerDirectory() -> URL {
|
2022-06-02 13:16:22 +01:00
|
|
|
FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: APP_GROUP_NAME)!
|
2022-04-19 12:29:03 +04:00
|
|
|
}
|
|
|
|
|
2022-06-24 13:52:20 +01:00
|
|
|
func getAppDirectory() -> URL {
|
|
|
|
dbContainerGroupDefault.get() == .group
|
|
|
|
? getGroupContainerDirectory()
|
|
|
|
: getDocumentsDirectory()
|
|
|
|
// getDocumentsDirectory()
|
|
|
|
}
|
|
|
|
|
|
|
|
let DB_FILE_PREFIX = "simplex_v1"
|
|
|
|
|
|
|
|
func getLegacyDatabasePath() -> URL {
|
|
|
|
getDocumentsDirectory().appendingPathComponent("mobile_v1", isDirectory: false)
|
|
|
|
}
|
|
|
|
|
|
|
|
public func getAppDatabasePath() -> URL {
|
|
|
|
dbContainerGroupDefault.get() == .group
|
|
|
|
? getGroupContainerDirectory().appendingPathComponent(DB_FILE_PREFIX, isDirectory: false)
|
|
|
|
: getLegacyDatabasePath()
|
|
|
|
}
|
|
|
|
|
2022-09-17 16:41:20 +04:00
|
|
|
func fileModificationDate(_ path: String) -> Date? {
|
|
|
|
do {
|
|
|
|
let attr = try FileManager.default.attributesOfItem(atPath: path)
|
|
|
|
return attr[FileAttributeKey.modificationDate] as? Date
|
|
|
|
} catch {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-19 19:05:29 +04:00
|
|
|
public func deleteAppFiles() {
|
|
|
|
let fm = FileManager.default
|
|
|
|
do {
|
|
|
|
let fileNames = try fm.contentsOfDirectory(atPath: getAppFilesDirectory().path)
|
|
|
|
for fileName in fileNames {
|
|
|
|
removeFile(fileName)
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
logger.error("FileUtils deleteAppFiles error: \(error.localizedDescription)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-24 21:18:28 +04:00
|
|
|
public func fileSize(_ url: URL) -> Int? { // in bytes
|
2022-09-19 19:05:29 +04:00
|
|
|
do {
|
|
|
|
let val = try url.resourceValues(forKeys: [.totalFileAllocatedSizeKey, .fileAllocatedSizeKey])
|
|
|
|
return val.totalFileAllocatedSize ?? val.fileAllocatedSize
|
|
|
|
} catch {
|
|
|
|
logger.error("FileUtils fileSize error: \(error.localizedDescription)")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public func directoryFileCountAndSize(_ dir: URL) -> (Int, Int)? { // size in bytes
|
|
|
|
let fm = FileManager.default
|
|
|
|
if let enumerator = fm.enumerator(at: dir, includingPropertiesForKeys: [.totalFileAllocatedSizeKey, .fileAllocatedSizeKey], options: [], errorHandler: { (_, error) -> Bool in
|
|
|
|
logger.error("FileUtils directoryFileCountAndSize error: \(error.localizedDescription)")
|
|
|
|
return false
|
|
|
|
}) {
|
|
|
|
var fileCount = 0
|
|
|
|
var bytes = 0
|
|
|
|
for case let url as URL in enumerator {
|
|
|
|
fileCount += 1
|
|
|
|
bytes += fileSize(url) ?? 0
|
|
|
|
}
|
|
|
|
return (fileCount, bytes)
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-17 16:41:20 +04:00
|
|
|
public func hasBackup(newerThan date: Date) -> Bool {
|
|
|
|
let dbPath = getAppDatabasePath().path
|
|
|
|
return hasBackupFile(dbPath + AGENT_DB_BAK, newerThan: date)
|
|
|
|
&& hasBackupFile(dbPath + CHAT_DB_BAK, newerThan: date)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func hasBackupFile(_ path: String, newerThan date: Date) -> Bool {
|
|
|
|
let fm = FileManager.default
|
|
|
|
return fm.fileExists(atPath: path)
|
|
|
|
&& date <= (fileModificationDate(path) ?? Date.distantPast)
|
|
|
|
}
|
|
|
|
|
|
|
|
public func restoreBackup() throws {
|
|
|
|
let dbPath = getAppDatabasePath().path
|
|
|
|
try restoreBackupFile(fromPath: dbPath + AGENT_DB_BAK, toPath: dbPath + AGENT_DB)
|
|
|
|
try restoreBackupFile(fromPath: dbPath + CHAT_DB_BAK, toPath: dbPath + CHAT_DB)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func restoreBackupFile(fromPath: String, toPath: String) throws {
|
|
|
|
let fm = FileManager.default
|
|
|
|
if fm.fileExists(atPath: toPath) {
|
|
|
|
try fm.removeItem(atPath: toPath)
|
|
|
|
}
|
|
|
|
try fm.copyItem(atPath: fromPath, toPath: toPath)
|
|
|
|
}
|
|
|
|
|
2022-06-24 13:52:20 +01:00
|
|
|
public func hasLegacyDatabase() -> Bool {
|
2022-09-07 12:49:41 +01:00
|
|
|
hasDatabaseAtPath(getLegacyDatabasePath())
|
|
|
|
}
|
|
|
|
|
|
|
|
public func hasDatabase() -> Bool {
|
|
|
|
hasDatabaseAtPath(getAppDatabasePath())
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasDatabaseAtPath(_ dbPath: URL) -> Bool {
|
2022-06-24 13:52:20 +01:00
|
|
|
let fm = FileManager.default
|
2022-09-17 16:41:20 +04:00
|
|
|
return fm.isReadableFile(atPath: dbPath.path + AGENT_DB) &&
|
|
|
|
fm.isReadableFile(atPath: dbPath.path + CHAT_DB)
|
2022-06-24 13:52:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public func removeLegacyDatabaseAndFiles() -> Bool {
|
|
|
|
let dbPath = getLegacyDatabasePath()
|
|
|
|
let appFiles = getDocumentsDirectory().appendingPathComponent("app_files", isDirectory: true)
|
|
|
|
let fm = FileManager.default
|
2022-09-17 16:41:20 +04:00
|
|
|
let r1 = nil != (try? fm.removeItem(atPath: dbPath.path + AGENT_DB))
|
|
|
|
let r2 = nil != (try? fm.removeItem(atPath: dbPath.path + CHAT_DB))
|
|
|
|
try? fm.removeItem(atPath: dbPath.path + AGENT_DB_BAK)
|
|
|
|
try? fm.removeItem(atPath: dbPath.path + CHAT_DB_BAK)
|
2022-06-24 13:52:20 +01:00
|
|
|
try? fm.removeItem(at: appFiles)
|
|
|
|
return r1 && r2
|
|
|
|
}
|
|
|
|
|
2023-03-24 15:20:15 +04:00
|
|
|
public func getTempFilesDirectory() -> URL {
|
|
|
|
getAppDirectory().appendingPathComponent("temp_files", isDirectory: true)
|
|
|
|
}
|
|
|
|
|
2022-05-31 07:55:13 +01:00
|
|
|
public func getAppFilesDirectory() -> URL {
|
2022-06-24 13:52:20 +01:00
|
|
|
getAppDirectory().appendingPathComponent("app_files", isDirectory: true)
|
2022-05-06 21:10:32 +04:00
|
|
|
}
|
|
|
|
|
2022-11-24 21:18:28 +04:00
|
|
|
public func getAppFilePath(_ fileName: String) -> URL {
|
2022-05-06 21:10:32 +04:00
|
|
|
getAppFilesDirectory().appendingPathComponent(fileName)
|
2022-04-19 12:29:03 +04:00
|
|
|
}
|
|
|
|
|
2022-12-24 11:38:59 +00:00
|
|
|
public func saveFile(_ data: Data, _ fileName: String) -> String? {
|
2022-05-06 21:10:32 +04:00
|
|
|
let filePath = getAppFilePath(fileName)
|
|
|
|
do {
|
|
|
|
try data.write(to: filePath)
|
|
|
|
return fileName
|
|
|
|
} catch {
|
|
|
|
logger.error("FileUtils.saveFile error: \(error.localizedDescription)")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 07:55:13 +01:00
|
|
|
public func removeFile(_ fileName: String) {
|
2022-05-06 21:10:32 +04:00
|
|
|
do {
|
|
|
|
try FileManager.default.removeItem(atPath: getAppFilePath(fileName).path)
|
|
|
|
} catch {
|
|
|
|
logger.error("FileUtils.removeFile error: \(error.localizedDescription)")
|
|
|
|
}
|
|
|
|
}
|