You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
376 lines
14 KiB
376 lines
14 KiB
//
|
|
// Created by Marco Schmickler on 13.11.21.
|
|
// Copyright (c) 2021 Marco Schmickler. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
class LocalManager {
|
|
static let sharedInstance = LocalManager()
|
|
|
|
var settings = KSettings()
|
|
|
|
var authenticated = false
|
|
|
|
var favorites = MediaItem(name: "fav", path: "", root: "", type: ItemType.FAVROOT)
|
|
|
|
var externalURL = ""
|
|
|
|
func loadSettings() {
|
|
do {
|
|
let jsonData = try FileHelper.getData(name: "settings.json")
|
|
let model = try JSONDecoder().decode(KSettingsModel.self, from: jsonData)
|
|
settings = KSettings(model: model)
|
|
} catch {
|
|
print(error.localizedDescription)
|
|
}
|
|
}
|
|
|
|
func saveSettings() {
|
|
let json = settings.toJSON()
|
|
let url = FileHelper.getDocumentsDirectory().appendingPathComponent("settings.json")
|
|
do {
|
|
print(url)
|
|
try json.write(to: url, atomically: true, encoding: .utf8)
|
|
let input = try String(contentsOf: url)
|
|
print(input)
|
|
} catch {
|
|
print(json)
|
|
}
|
|
}
|
|
|
|
func deleteLocal(selectedItem: MediaItem) {
|
|
if (selectedItem.local) {
|
|
print(selectedItem.playerURL?.absoluteString)
|
|
do {
|
|
try FileManager.default.removeItem(at: selectedItem.playerURL!)
|
|
} catch {
|
|
print(error)
|
|
}
|
|
}
|
|
}
|
|
|
|
func saveFavDir(url: URL, item: MediaItem) -> Void {
|
|
DatabaseManager.sharedInstance.saveItemMetaData(item)
|
|
|
|
for c in item.children {
|
|
let t = c.time
|
|
let ms = Int(t * 1000)
|
|
let p = url.appendingPathExtension("\(ms).jpg")
|
|
let pt = url.appendingPathExtension("\(ms)_thumb.jpg")
|
|
print(p)
|
|
|
|
if FileManager.default.fileExists(atPath: pt.absoluteString) {
|
|
print("contained")
|
|
} else {
|
|
if let id = c.image, let imageData = id.jpegData(compressionQuality: 1.0) {
|
|
do {
|
|
try imageData.write(to: p)
|
|
} catch {
|
|
print("error")
|
|
}
|
|
|
|
let thumb = id.scaleToSize(15 * 16, height: 15 * 9)
|
|
if let imageDataT = thumb.jpegData(compressionQuality: 1.0) {
|
|
do {
|
|
try imageDataT.write(to: pt)
|
|
} catch {
|
|
}
|
|
c.image = thumb
|
|
c.thumbUrl = pt.absoluteString
|
|
}
|
|
|
|
c.loaded = true
|
|
}
|
|
}
|
|
}
|
|
|
|
let json = item.toJSON()
|
|
|
|
do {
|
|
var jsonurl = url
|
|
if url.pathExtension != "json" {
|
|
jsonurl = url.appendingPathExtension("json")
|
|
}
|
|
try json.write(to: jsonurl, atomically: true, encoding: .utf8)
|
|
let input = try String(contentsOf: jsonurl)
|
|
print(input)
|
|
} catch {
|
|
print(json)
|
|
}
|
|
}
|
|
|
|
func loadDir(path: String) -> [MediaItem] {
|
|
var res = [MediaItem]()
|
|
var items = [String:MediaItem]()
|
|
let url = FileHelper.getDocumentsDirectory().appendingPathComponent(path)
|
|
|
|
if let enumerator = FileManager.default.enumerator(at: url, includingPropertiesForKeys: [.isRegularFileKey], options: [.skipsHiddenFiles, .skipsPackageDescendants]) {
|
|
for case let fileURL as URL in enumerator {
|
|
do {
|
|
let fileAttributes = try fileURL.resourceValues(forKeys: [.isRegularFileKey])
|
|
if fileAttributes.isRegularFile! {
|
|
if (fileURL.pathExtension == "mp4") {
|
|
let fi = MediaItem(name: fileURL.lastPathComponent, path: "", root: "fav", type: ItemType.PICFOLDER)
|
|
fi.local = true
|
|
fi.loaded = true
|
|
fi.externalURL = fileURL.absoluteString
|
|
// fi.thumbUrl = fileURL.absoluteString + ".jpg"
|
|
|
|
items[fileURL.lastPathComponent] = fi
|
|
res.append(fi)
|
|
print(fileURL.absoluteString)
|
|
}
|
|
}
|
|
}
|
|
catch {
|
|
print(error)
|
|
}
|
|
}
|
|
}
|
|
if let enumerator = FileManager.default.enumerator(at: url, includingPropertiesForKeys: [.isRegularFileKey], options: [.skipsHiddenFiles, .skipsPackageDescendants]) {
|
|
for case let fileURL as URL in enumerator {
|
|
do {
|
|
let fileAttributes = try fileURL.resourceValues(forKeys: [.isRegularFileKey])
|
|
if fileAttributes.isRegularFile! {
|
|
if (fileURL.pathExtension == "jpg") {
|
|
let name = fileURL.lastPathComponent
|
|
let end = name.substringBefore(".mp4")
|
|
if let i = items[end + ".mp4"] {
|
|
if i.thumbUrl == nil {
|
|
i.thumbUrl = fileURL.absoluteString
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch {
|
|
print(error)
|
|
}
|
|
}
|
|
}
|
|
return res
|
|
}
|
|
|
|
func loadFavDirs(_ url: URL, completionHandler: @escaping Weiter) -> Void {
|
|
var res = [MediaItem]()
|
|
|
|
var loadedJson = [String]()
|
|
|
|
var itemsMap = Dictionary<String, MediaItem>()
|
|
|
|
let fi = MediaItem(name: "images", path: "", root: "fav", type: ItemType.PICFOLDER)
|
|
fi.local = true
|
|
fi.loaded = true
|
|
|
|
res.append(fi)
|
|
|
|
if let enumerator = FileManager.default.enumerator(at: url, includingPropertiesForKeys: [.isRegularFileKey], options: [.skipsHiddenFiles, .skipsPackageDescendants]) {
|
|
for case let fileURL as URL in enumerator {
|
|
do {
|
|
let fileAttributes = try fileURL.resourceValues(forKeys: [.isRegularFileKey])
|
|
if fileAttributes.isRegularFile! {
|
|
if (fileURL.pathExtension == "jpg" && fileURL.absoluteString.contains("/images/")) {
|
|
let p = fileURL.absoluteString.substringAfter("/Documents")
|
|
|
|
let path = p.substringBefore("/")
|
|
|
|
let fp = fileURL.absoluteString.substringAfter("/Documents/images/")
|
|
|
|
let fpath = fp.substringBefore("/")
|
|
|
|
var folder = itemsMap[fpath]
|
|
|
|
if folder == nil {
|
|
let f = MediaItem(name: fpath, path: "images", root: "fav", type: ItemType.PICS)
|
|
f.local = true
|
|
f.loaded = true
|
|
folder = f
|
|
itemsMap[fpath] = folder!
|
|
fi.children.append(f)
|
|
}
|
|
|
|
let m = MediaItem(name: fileURL.lastPathComponent, path: path, root: "fav", type: ItemType.PICS)
|
|
m.local = true
|
|
m.loaded = true
|
|
m.thumbUrl = fileURL.absoluteString
|
|
|
|
let jsonURL = fileURL.absoluteString.replacingOccurrences(of: ".jpg", with: ".json")
|
|
|
|
do {
|
|
if let u = URL(string: jsonURL) {
|
|
let jsonData = try Data(contentsOf: u)
|
|
let item = try JSONDecoder().decode(MediaModel.self, from: jsonData)
|
|
m.scale = item.scale
|
|
m.offset = item.offset
|
|
m.size = item.size
|
|
|
|
DatabaseManager.sharedInstance.saveItemMetaData(m)
|
|
}
|
|
} catch {
|
|
|
|
}
|
|
|
|
// var folder = itemsMap[path]
|
|
//
|
|
// if folder == nil {
|
|
// folder = MediaItem(name: item.path, path: item.path, root: item.root, type: ItemType.FOLDER)
|
|
// folder!.loaded = true
|
|
// folder!.local = item.local
|
|
// itemsMap[path] = folder!
|
|
// items.append(folder!)
|
|
// }
|
|
|
|
folder!.children.append(m)
|
|
|
|
// print(fileURL.absoluteString)
|
|
}
|
|
|
|
let p = fileURL.absoluteString.substringAfter("/Documents")
|
|
|
|
if (fileURL.pathExtension == "json") {
|
|
|
|
}
|
|
|
|
if (fileURL.pathExtension == "mp4") {
|
|
print(fileURL.absoluteString)
|
|
|
|
let jsonURL = fileURL.appendingPathExtension("json")
|
|
|
|
do {
|
|
let jsonData = try Data(contentsOf: jsonURL)
|
|
let item = try JSONDecoder().decode(MediaModel.self, from: jsonData)
|
|
let mediaItem = MediaItem(model: item)
|
|
mediaItem.externalURL = fileURL.absoluteString
|
|
mediaItem.localURL = jsonURL
|
|
mediaItem.local = true
|
|
for i in mediaItem.children {
|
|
i.externalURL = fileURL.absoluteString
|
|
i.local = true
|
|
}
|
|
let pathComponents = fileURL.pathComponents
|
|
mediaItem.path = pathComponents[pathComponents.count-2]
|
|
DatabaseManager.sharedInstance.saveItemMetaData(mediaItem)
|
|
res.append(mediaItem)
|
|
} catch {
|
|
let path = p.substringBefore("/")
|
|
|
|
let m = MediaItem(name: fileURL.lastPathComponent, path: path, root: "fav", type: ItemType.VIDEO)
|
|
m.local = true
|
|
m.loaded = true
|
|
m.thumbUrl = fileURL.appendingPathExtension("jpg").absoluteString
|
|
m.externalURL = fileURL.absoluteString
|
|
|
|
res.append(m)
|
|
}
|
|
}
|
|
}
|
|
} catch {
|
|
print(error, fileURL)
|
|
}
|
|
}
|
|
}
|
|
|
|
let m = MediaItem(name: "new", path: "new", root: "fav", type: ItemType.FOLDER)
|
|
m.local = true
|
|
res.append(m)
|
|
|
|
completionHandler(res)
|
|
}
|
|
|
|
/*
|
|
exclude mp4 from backup
|
|
create images folder
|
|
extract zip to images folder
|
|
*/
|
|
func prepareLocalFolder() {
|
|
do {
|
|
let i = try FileHelper.createDir(name: "incoming")
|
|
|
|
let files = FileHelper.listFiles(name: "")
|
|
for f in files {
|
|
if f.pathExtension == "mp4" {
|
|
var dest = i.appendingPathComponent(f.lastPathComponent)
|
|
try FileManager.default.moveItem(at: f, to: dest)
|
|
try FileHelper.setExcludeFromiCloudBackup(&dest, isExcluded: true)
|
|
}
|
|
}
|
|
} catch {
|
|
}
|
|
|
|
do {
|
|
let i = try FileHelper.createDir(name: "images")
|
|
|
|
let files = FileHelper.listFiles(name: "download")
|
|
for f in files {
|
|
print(f)
|
|
if f.pathExtension == "zip" {
|
|
var dest = i.appendingPathComponent(f.lastPathComponent)
|
|
|
|
do {
|
|
try FileManager.default.createDirectory(at: dest, withIntermediateDirectories: true, attributes: nil)
|
|
try FileManager.default.unzipItem(at: f, to: dest)
|
|
} catch {
|
|
print("Extraction of ZIP archive failed with error:\(error)")
|
|
}
|
|
try FileManager.default.removeItem(at: f)
|
|
try FileHelper.setExcludeFromiCloudBackup(&dest, isExcluded: true)
|
|
}
|
|
}
|
|
} catch {
|
|
}
|
|
}
|
|
|
|
// old
|
|
func loadFav2Dirs(_ rootParam: String, completionHandler: @escaping Weiter) -> Void {
|
|
let files = FileHelper.listFiles(name: "1")
|
|
var res = [MediaItem]()
|
|
for f in files {
|
|
if f.pathExtension == "mp4" {
|
|
let m = MediaItem(name: f.lastPathComponent, path: "", root: "1", type: ItemType.VIDEO)
|
|
|
|
m.local = true
|
|
res.append(m)
|
|
}
|
|
}
|
|
|
|
completionHandler(res)
|
|
}
|
|
|
|
//old
|
|
func loadFav1Dirs(_ rootParam: String, completionHandler: @escaping Weiter) -> Void {
|
|
var res = [MediaItem]()
|
|
|
|
do {
|
|
let jsonData = try FileHelper.getData(name: "fav.json")
|
|
let items = try JSONDecoder().decode(MediaModel.self, from: jsonData)
|
|
let loaded = MediaItem(model: items)
|
|
res = loaded.children
|
|
} catch {
|
|
print(error.localizedDescription)
|
|
}
|
|
|
|
// res.append(currentFav)
|
|
completionHandler(res)
|
|
}
|
|
|
|
func fixDocumentURL(_ path: String?) -> String? {
|
|
if path == nil {
|
|
return path
|
|
}
|
|
|
|
if path!.contains("file://") {
|
|
if path!.contains("/Application") {
|
|
let p = path!.substringAfter("/Documents/")
|
|
let fixed = FileHelper.getDocumentsDirectory().appendingPathComponent(p)
|
|
return fixed.absoluteString
|
|
}
|
|
else {
|
|
let p = path!.substringAfter("/Documents")
|
|
return externalURL + p
|
|
}
|
|
}
|
|
return path
|
|
}
|
|
|
|
}
|