Skip to content

Commit

Permalink
v1.4.2(64)
Browse files Browse the repository at this point in the history
Fix bugs
  • Loading branch information
lithium0003 committed Dec 3, 2019
1 parent 7729c36 commit 704c65f
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 77 deletions.
70 changes: 38 additions & 32 deletions RemoteCloud/RemoteCloud/FileCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ public class FileCache {
group.leave()
return
}
var targetURL: URL?
self.persistentContainer.performBackgroundTask { context in
defer {
group.leave()
}
var targetURL: URL?
let fetchrequest = NSFetchRequest<NSFetchRequestResult>(entityName: "FileCacheItem")
fetchrequest.predicate = NSPredicate(format: "storage == %@ && id == %@ && chunkOffset == 0", storage, id)
do{
guard let item = try context.fetch(fetchrequest).first as? FileCacheItem else {
let items = try context.fetch(fetchrequest)
guard let item = items.first as? FileCacheItem else {
return
}
if orgItem.mdate != item.mdate || orgItem.size != item.orgSize {
Expand Down Expand Up @@ -65,42 +69,42 @@ public class FileCache {
}
}
catch{
return
print(error)
}
}
if let target = targetURL {
do {
let hFile = try FileHandle(forReadingFrom: target)
defer {
do {
if #available(iOS 13.0, *) {
try hFile.close()
} else {
hFile.closeFile()
if let target = targetURL {
do {
let hFile = try FileHandle(forReadingFrom: target)
defer {
do {
if #available(iOS 13.0, *) {
try hFile.close()
} else {
hFile.closeFile()
}
}
catch {
print(error)
}
}
catch {
print(error)
if #available(iOS 13.0, *) {
try hFile.seek(toOffset: UInt64(offset))
} else {
hFile.seek(toFileOffset: UInt64(offset))
}
}
if #available(iOS 13.0, *) {
try hFile.seek(toOffset: UInt64(offset))
} else {
hFile.seek(toFileOffset: UInt64(offset))
}
if size < 0 {
ret = hFile.readDataToEndOfFile()
if size < 0 {
ret = hFile.readDataToEndOfFile()
return
}
ret = hFile.readData(ofLength: Int(size))
return
}
ret = hFile.readData(ofLength: Int(size))
return
}
catch {
print(error)
catch {
print(error)
}
}
}
}
let _ = group.wait(timeout: .now()+2)
let _ = group.wait()
return ret
}

Expand All @@ -123,7 +127,8 @@ public class FileCache {
let fetchrequest = NSFetchRequest<NSFetchRequestResult>(entityName: "FileCacheItem")
fetchrequest.predicate = NSPredicate(format: "storage == %@ && id == %@ && chunkOffset == %lld", storage, id, offset)
do{
guard let item = try context.fetch(fetchrequest).first as? FileCacheItem else {
let items = try context.fetch(fetchrequest)
guard let item = items.first as? FileCacheItem else {
return
}
if orgItem.mdate != item.mdate || orgItem.size != item.orgSize {
Expand Down Expand Up @@ -153,20 +158,21 @@ public class FileCache {
}
}
catch{
print(error)
return
}
}

}
let _ = group.wait(timeout: .now()+2)
let _ = group.wait()
return ret
}

public func saveFile(storage: String, id: String, data: Data) {
if getCacheSize() > cacheMaxSize {
increseFreeSpace()
}

do {
let size = Int64(data.count)
let base = try FileManager.default.url(for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: true).appendingPathComponent("NetCache", isDirectory: true)
Expand Down
13 changes: 6 additions & 7 deletions RemoteCloud/RemoteCloud/RemoteStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ public class RemoteStorageBase: NSObject, RemoteStorage {
var cancelTime = Date(timeIntervalSince1970: 0)

public func cancel() {
cancelTime = Date(timeIntervalSinceNow: 0.1)
cancelTime = Date(timeIntervalSinceNow: 0.5)
}

public func config() -> String {
Expand Down Expand Up @@ -661,16 +661,15 @@ public class RemoteStorageBase: NSObject, RemoteStorage {
if fileId == "" {
let backgroundContext = CloudFactory.shared.data.persistentContainer.newBackgroundContext()
self.deleteChild(parent: fileId, context: backgroundContext)
backgroundContext.performAndWait {
backgroundContext.perform {
try? backgroundContext.save()
self.ListChildren(onFinish: onFinish)
}
self.ListChildren(onFinish: onFinish)
}
else {
var path = ""
let backgroundContext = CloudFactory.shared.data.persistentContainer.newBackgroundContext()

backgroundContext.performAndWait {
backgroundContext.perform {
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "RemoteData")
fetchRequest.predicate = NSPredicate(format: "id == %@ && storage == %@", fileId, self.storageName ?? "")
if let result = try? backgroundContext.fetch(fetchRequest) {
Expand All @@ -683,7 +682,7 @@ public class RemoteStorageBase: NSObject, RemoteStorage {
self.deleteChild(parent: fileId, context: backgroundContext)
}
}
backgroundContext.performAndWait {
backgroundContext.perform {
if path != "" {
try? backgroundContext.save()
self.ListChildren(fileId: fileId, path: path, onFinish: onFinish)
Expand All @@ -702,7 +701,7 @@ public class RemoteStorageBase: NSObject, RemoteStorage {
else {
var ids: [String] = []
let backgroundContext = CloudFactory.shared.data.persistentContainer.newBackgroundContext()
backgroundContext.performAndWait {
backgroundContext.perform {
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "RemoteData")
fetchRequest.predicate = NSPredicate(format: "path == %@", path)
if let result = try? backgroundContext.fetch(fetchRequest), let items = result as? [RemoteData] {
Expand Down
48 changes: 20 additions & 28 deletions RemoteCloud/RemoteCloud/Storages/WebDAVStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -313,15 +313,6 @@ public class WebDAVStorage: NetworkStorage, URLSessionTaskDelegate, URLSessionDa
return .WebDAV
}

public override func cancel() {
if let acceptRange = acceptRange, !acceptRange {
cancelTime = Date(timeIntervalSinceNow: 10)
}
else {
super.cancel()
}
}

var cache_accessUsername = ""
var accessUsername: String {
if let name = storageName {
Expand Down Expand Up @@ -377,6 +368,7 @@ public class WebDAVStorage: NetworkStorage, URLSessionTaskDelegate, URLSessionDa
var recvData: [Int: Data] = [:]
var headerHandler: [Int: (URLResponse)->URLSession.ResponseDisposition] = [:]

let wholeQueue = DispatchQueue(label: "WholeReading")
var wholeReading: [URL] = []

public func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
Expand Down Expand Up @@ -1040,9 +1032,8 @@ public class WebDAVStorage: NetworkStorage, URLSessionTaskDelegate, URLSessionDa
}
}
//print(url)

if wholeReading.contains(url) {
DispatchQueue.global().asyncAfter(deadline: .now()+Double.random(in: 1..<5)) {
if wholeQueue.sync(execute: { wholeReading.contains(url) }) {
DispatchQueue.global().asyncAfter(deadline: .now()+Double.random(in: 0..<1)) {
if self.cancelTime.timeIntervalSinceNow > 0 {
self.cancelTime = Date(timeIntervalSinceNow: 0.5)
onFinish?(nil)
Expand All @@ -1065,33 +1056,34 @@ public class WebDAVStorage: NetworkStorage, URLSessionTaskDelegate, URLSessionDa
return
}
lastCall = Date()
wholeReading += [url]

wholeQueue.async {
self.wholeReading += [url]
}

var request: URLRequest
request = URLRequest(url: url)

os_log("%{public}@", log: log, type: .debug, "readFile(WebDAV:\(storageName ?? "") \(fileId) whole read")
os_log("%{public}@", log: log, type: .debug, "readFile(WebDAV:\(storageName ?? "") \(fileId) whole read \(start ?? 0) \(length ?? -1)")

let task = dataSession.dataTask(with: request)
let timer1 = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { t in
if self.cancelTime.timeIntervalSinceNow > 0 {
print("cancel")
self.cancelTime = Date(timeIntervalSinceNow: 1)
onFinish?(nil)
task.cancel()
return
var timer1: Timer?
DispatchQueue.main.async {
timer1 = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { t in
if self.cancelTime.timeIntervalSinceNow > 0 {
print("cancel")
task.cancel()
self.cancelTime = Date(timeIntervalSinceNow: 0.5)
onFinish?(nil)
return
}
}
}
dataTasks[task.taskIdentifier] = { data, error in
self.callSemaphore.signal()
timer1.invalidate()
var waittime = self.callWait
if let error = error {
print(error)
if (error as NSError).code == -1009 {
waittime += 30
}
}
self.callSemaphore.signal()
timer1?.invalidate()
if let d = data {
CloudFactory.shared.cache.saveFile(storage: self.storageName!, id: fileId, data: d)
let s = Int(start ?? 0)
Expand Down
4 changes: 2 additions & 2 deletions ccViewer/CryptCloudViewer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = ccViewer/ccViewer.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 62;
CURRENT_PROJECT_VERSION = 64;
DERIVE_MACCATALYST_PRODUCT_BUNDLE_IDENTIFIER = YES;
DEVELOPMENT_TEAM = 7A9X38B4YU;
"ENABLE_HARDENED_RUNTIME[sdk=macosx*]" = YES;
Expand Down Expand Up @@ -838,7 +838,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = ccViewer/ccViewer.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 62;
CURRENT_PROJECT_VERSION = 64;
DERIVE_MACCATALYST_PRODUCT_BUNDLE_IDENTIFIER = YES;
DEVELOPMENT_TEAM = 7A9X38B4YU;
"ENABLE_HARDENED_RUNTIME[sdk=macosx*]" = YES;
Expand Down
11 changes: 5 additions & 6 deletions ccViewer/ccViewer/Base.lproj/LaunchScreen.storyboard
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="15505" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
<device id="retina4_7" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="15510"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand All @@ -20,6 +18,7 @@
<subviews>
<imageView userInteractionEnabled="NO" contentMode="scaleAspectFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="app" translatesAutoresizingMaskIntoConstraints="NO" id="bfX-Rm-C0u">
<rect key="frame" x="59.5" y="205.5" width="256" height="256"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="width" constant="256" id="M6P-aU-fXF"/>
<constraint firstAttribute="height" constant="256" id="bfj-je-7va"/>
Expand All @@ -40,6 +39,6 @@
</scene>
</scenes>
<resources>
<image name="app" width="245.75999450683594" height="245.75999450683594"/>
<image name="app" width="340" height="340"/>
</resources>
</document>
13 changes: 11 additions & 2 deletions ccViewer/ccViewer/TableViewControllerItems.swift
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ class TableViewControllerItems: UITableViewController, UISearchResultsUpdating,
super.didMove(toParent: parent)
if parent == nil {
gone = false
semaphore.signal()
}
}

Expand Down Expand Up @@ -934,7 +935,10 @@ class TableViewControllerItems: UITableViewController, UISearchResultsUpdating,
if newroot.count == 0 {
self.activityIndicator.startAnimating()
DispatchQueue.global().async {
CloudFactory.shared[self.storageName]?.list(fileId: self.result[indexPath.row].id ?? "") {
CloudFactory.shared[self.storageName]?.list(fileId: self.result[indexPath.row].id ?? "") { [weak self] in
guard let self = self else {
return
}
DispatchQueue.main.async {
self.activityIndicator.stopAnimating()
self.semaphore.signal()
Expand Down Expand Up @@ -964,7 +968,10 @@ class TableViewControllerItems: UITableViewController, UISearchResultsUpdating,
if newroot.count == 0 {
activityIndicator.startAnimating()

(CloudFactory.shared[storageName] as? RemoteSubItem)?.listsubitem(fileId: result[indexPath.row].id ?? "") {
(CloudFactory.shared[storageName] as? RemoteSubItem)?.listsubitem(fileId: result[indexPath.row].id ?? "") { [weak self] in
guard let self = self else {
return
}
DispatchQueue.main.async {
self.activityIndicator.stopAnimating()
self.semaphore.signal()
Expand Down Expand Up @@ -1314,6 +1321,7 @@ class TableViewControllerItems: UITableViewController, UISearchResultsUpdating,
guard self.downloadProgress.isLive else {
self.semaphore.signal()
stream.isLive = false
item.cancel()
return
}
DispatchQueue.main.async {
Expand Down Expand Up @@ -1359,6 +1367,7 @@ class TableViewControllerItems: UITableViewController, UISearchResultsUpdating,
guard self.downloadProgress.isLive else {
self.semaphore.signal()
stream.isLive = false
item.cancel()
return
}
DispatchQueue.main.async {
Expand Down
1 change: 1 addition & 0 deletions ccViewer/ccViewer/TableViewControllerItemsEdit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ class TableViewControllerItemsEdit: UITableViewController, UISearchResultsUpdati
override func didMove(toParent parent: UIViewController?) {
if parent == nil {
gone = false
semaphore.signal()
}
}

Expand Down
1 change: 1 addition & 0 deletions ccViewer/ccViewer/TableViewControllerPlaylist.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class TableViewControllerPlaylist: UITableViewController, UISearchResultsUpdatin
super.didMove(toParent: parent)
if parent == nil {
gone = false
semaphore.signal()
}
}

Expand Down
2 changes: 2 additions & 0 deletions ccViewer/ccViewer/ViewControllerImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ class ViewControllerImage: UIViewController, UIScrollViewDelegate, UIDocumentInt
self.isDownloading = false
guard self.downloadProgress.isLive else {
stream.isLive = false
self.items[idx].cancel()
return
}
DispatchQueue.main.async {
Expand Down Expand Up @@ -362,6 +363,7 @@ class ViewControllerImage: UIViewController, UIScrollViewDelegate, UIDocumentInt
}) { data in
guard self.downloadProgress.isLive else {
stream.isLive = false
self.items[self.itemIdx].cancel()
return
}
DispatchQueue.main.async {
Expand Down

0 comments on commit 704c65f

Please sign in to comment.