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
|
|
|
|
import SwiftUI
|
|
|
|
|
2022-04-25 12:44:24 +04:00
|
|
|
// maximum image file size to be auto-accepted
|
2022-05-06 21:10:32 +04:00
|
|
|
let maxImageSize: Int64 = 236700
|
2022-04-25 12:44:24 +04:00
|
|
|
|
2022-05-06 21:10:32 +04:00
|
|
|
let maxFileSize: Int64 = 8000000
|
2022-05-04 09:10:36 +04:00
|
|
|
|
2022-04-19 12:29:03 +04:00
|
|
|
func getDocumentsDirectory() -> URL {
|
2022-05-06 21:10:32 +04:00
|
|
|
FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
|
2022-04-19 12:29:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func getAppFilesDirectory() -> URL {
|
2022-05-06 21:10:32 +04:00
|
|
|
getDocumentsDirectory().appendingPathComponent("app_files", isDirectory: true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAppFilePath(_ fileName: String) -> URL {
|
|
|
|
getAppFilesDirectory().appendingPathComponent(fileName)
|
2022-04-19 12:29:03 +04:00
|
|
|
}
|
|
|
|
|
2022-05-07 16:25:04 +04:00
|
|
|
func getLoadedFilePath(_ file: CIFile?) -> String? {
|
2022-04-19 12:29:03 +04:00
|
|
|
if let file = file,
|
2022-05-07 16:25:04 +04:00
|
|
|
file.loaded,
|
2022-04-19 12:29:03 +04:00
|
|
|
let savedFile = file.filePath {
|
2022-05-06 21:10:32 +04:00
|
|
|
return getAppFilePath(savedFile).path
|
2022-04-19 12:29:03 +04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-07 16:25:04 +04:00
|
|
|
func getLoadedImage(_ file: CIFile?) -> UIImage? {
|
|
|
|
if let filePath = getLoadedFilePath(file) {
|
2022-04-19 12:29:03 +04:00
|
|
|
return UIImage(contentsOfFile: filePath)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-04 16:08:40 +04:00
|
|
|
|
2022-05-06 21:10:32 +04:00
|
|
|
func saveFileFromURL(_ url: URL) -> String? {
|
|
|
|
let savedFile: String?
|
|
|
|
if url.startAccessingSecurityScopedResource() {
|
|
|
|
do {
|
|
|
|
let fileData = try Data(contentsOf: url)
|
|
|
|
let fileName = uniqueCombine(url.lastPathComponent)
|
|
|
|
savedFile = saveFile(fileData, fileName)
|
|
|
|
} catch {
|
|
|
|
logger.error("FileUtils.saveFileFromURL error: \(error.localizedDescription)")
|
|
|
|
savedFile = nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logger.error("FileUtils.saveFileFromURL startAccessingSecurityScopedResource returned false")
|
|
|
|
savedFile = nil
|
|
|
|
}
|
|
|
|
url.stopAccessingSecurityScopedResource()
|
|
|
|
return savedFile
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveImage(_ uiImage: UIImage) -> String? {
|
|
|
|
if let imageDataResized = resizeImageToDataSize(uiImage, maxDataSize: maxImageSize) {
|
2022-05-07 16:25:04 +04:00
|
|
|
let timestamp = Date().getFormattedDate("yyyyMMdd_HHmmss")
|
|
|
|
let fileName = uniqueCombine("IMG_\(timestamp).jpg")
|
2022-05-06 21:10:32 +04:00
|
|
|
return saveFile(imageDataResized, fileName)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-07 16:25:04 +04:00
|
|
|
extension Date {
|
|
|
|
func getFormattedDate(_ format: String) -> String {
|
|
|
|
let df = DateFormatter()
|
|
|
|
df.dateFormat = format
|
|
|
|
df.locale = Locale(identifier: "US")
|
|
|
|
return df.string(from: self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-06 21:10:32 +04:00
|
|
|
private func saveFile(_ data: Data, _ fileName: String) -> String? {
|
|
|
|
let filePath = getAppFilePath(fileName)
|
|
|
|
do {
|
|
|
|
try data.write(to: filePath)
|
|
|
|
return fileName
|
|
|
|
} catch {
|
|
|
|
logger.error("FileUtils.saveFile error: \(error.localizedDescription)")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func uniqueCombine(_ fileName: String) -> String {
|
|
|
|
func tryCombine(_ fileName: String, _ n: Int) -> String {
|
|
|
|
let name = fileName.deletingPathExtension
|
|
|
|
let ext = fileName.pathExtension
|
|
|
|
let suffix = (n == 0) ? "" : "_\(n)"
|
|
|
|
let f = "\(name)\(suffix).\(ext)"
|
|
|
|
return (FileManager.default.fileExists(atPath: getAppFilePath(f).path)) ? tryCombine(fileName, n + 1) : f
|
|
|
|
}
|
|
|
|
return tryCombine(fileName, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension String {
|
|
|
|
var ns: NSString {
|
|
|
|
return self as NSString
|
|
|
|
}
|
|
|
|
var pathExtension: String {
|
|
|
|
return ns.pathExtension
|
|
|
|
}
|
|
|
|
var deletingPathExtension: String {
|
|
|
|
return ns.deletingPathExtension
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeFile(_ fileName: String) {
|
|
|
|
do {
|
|
|
|
try FileManager.default.removeItem(atPath: getAppFilePath(fileName).path)
|
|
|
|
} catch {
|
|
|
|
logger.error("FileUtils.removeFile error: \(error.localizedDescription)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-04 16:08:40 +04:00
|
|
|
// image utils
|
|
|
|
|
|
|
|
func dropImagePrefix(_ s: String) -> String {
|
|
|
|
dropPrefix(dropPrefix(s, "data:image/png;base64,"), "data:image/jpg;base64,")
|
|
|
|
}
|
|
|
|
|
|
|
|
private func dropPrefix(_ s: String, _ prefix: String) -> String {
|
|
|
|
s.hasPrefix(prefix) ? String(s.dropFirst(prefix.count)) : s
|
|
|
|
}
|
|
|
|
|
|
|
|
func cropToSquare(_ image: UIImage) -> UIImage {
|
|
|
|
let size = image.size
|
|
|
|
let side = min(size.width, size.height)
|
|
|
|
let newSize = CGSize(width: side, height: side)
|
|
|
|
var origin = CGPoint.zero
|
|
|
|
if size.width > side {
|
|
|
|
origin.x -= (size.width - side) / 2
|
|
|
|
} else if size.height > side {
|
|
|
|
origin.y -= (size.height - side) / 2
|
|
|
|
}
|
|
|
|
return resizeImage(image, newBounds: CGRect(origin: .zero, size: newSize), drawIn: CGRect(origin: origin, size: size))
|
|
|
|
}
|
|
|
|
|
2022-05-06 21:10:32 +04:00
|
|
|
func resizeImageToDataSize(_ image: UIImage, maxDataSize: Int64) -> Data? {
|
2022-05-04 16:08:40 +04:00
|
|
|
var img = image
|
|
|
|
var data = img.jpegData(compressionQuality: 0.85)
|
|
|
|
var dataSize = data?.count ?? 0
|
|
|
|
while dataSize != 0 && dataSize > maxDataSize {
|
|
|
|
let ratio = sqrt(Double(dataSize) / Double(maxDataSize))
|
|
|
|
let clippedRatio = min(ratio, 2.0)
|
|
|
|
img = reduceSize(img, ratio: clippedRatio)
|
|
|
|
data = img.jpegData(compressionQuality: 0.85)
|
|
|
|
dataSize = data?.count ?? 0
|
|
|
|
}
|
|
|
|
logger.debug("resizeImageToDataSize final \(dataSize)")
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
2022-05-06 21:10:32 +04:00
|
|
|
func resizeImageToStrSize(_ image: UIImage, maxDataSize: Int64) -> String? {
|
2022-05-04 16:08:40 +04:00
|
|
|
var img = image
|
|
|
|
var str = compressImageStr(img)
|
|
|
|
var dataSize = str?.count ?? 0
|
|
|
|
while dataSize != 0 && dataSize > maxDataSize {
|
|
|
|
let ratio = sqrt(Double(dataSize) / Double(maxDataSize))
|
|
|
|
let clippedRatio = min(ratio, 2.0)
|
|
|
|
img = reduceSize(img, ratio: clippedRatio)
|
|
|
|
str = compressImageStr(img)
|
|
|
|
dataSize = str?.count ?? 0
|
|
|
|
}
|
|
|
|
logger.debug("resizeImageToStrSize final \(dataSize)")
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
|
|
|
func compressImageStr(_ image: UIImage, _ compressionQuality: CGFloat = 0.85) -> String? {
|
|
|
|
if let data = image.jpegData(compressionQuality: compressionQuality) {
|
|
|
|
return "data:image/jpg;base64,\(data.base64EncodedString())"
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
private func reduceSize(_ image: UIImage, ratio: CGFloat) -> UIImage {
|
|
|
|
let newSize = CGSize(width: floor(image.size.width / ratio), height: floor(image.size.height / ratio))
|
|
|
|
let bounds = CGRect(origin: .zero, size: newSize)
|
|
|
|
return resizeImage(image, newBounds: bounds, drawIn: bounds)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func resizeImage(_ image: UIImage, newBounds: CGRect, drawIn: CGRect) -> UIImage {
|
|
|
|
let format = UIGraphicsImageRendererFormat()
|
|
|
|
format.scale = 1.0
|
|
|
|
format.opaque = true
|
|
|
|
return UIGraphicsImageRenderer(bounds: newBounds, format: format).image { _ in
|
|
|
|
image.draw(in: drawIn)
|
|
|
|
}
|
|
|
|
}
|