From 6ab5c9b3b0513d640e7eefa58a8bb69934f30603 Mon Sep 17 00:00:00 2001 From: Peter Date: Tue, 22 Nov 2016 21:30:16 +0300 Subject: [PATCH] no message --- Postbox/Coding.swift | 4 + Postbox/MediaBox.swift | 91 ++++++++++++++++++----- Postbox/PeerNameIndexRepresentation.swift | 4 +- 3 files changed, 80 insertions(+), 19 deletions(-) diff --git a/Postbox/Coding.swift b/Postbox/Coding.swift index 9c1608895f..6399702203 100644 --- a/Postbox/Coding.swift +++ b/Postbox/Coding.swift @@ -31,6 +31,10 @@ public func declareEncodable(_ type: Any.Type, f: @escaping(Decoder) -> Coding) typeStore.dict[murMurHashString32("\(type)")] = f } +public func persistentHash32(_ string: String) -> Int32 { + return murMurHashString32(string) +} + private let emptyMemory = malloc(1)! public class MemoryBuffer: Equatable, CustomStringConvertible { diff --git a/Postbox/MediaBox.swift b/Postbox/MediaBox.swift index 6d6ebb2f4b..ba2ddab1b8 100644 --- a/Postbox/MediaBox.swift +++ b/Postbox/MediaBox.swift @@ -201,8 +201,14 @@ public final class MediaBox { } if fetchingData { - //status = .Fetching(progress: Float(currentSize) / Float(resourceSize)) - status = .Fetching(progress: 0.0) + let currentSize = fileSize(paths.partial) ?? 0 + + if let resourceSize = resource.size { + status = .Fetching(progress: Float(currentSize) / Float(resourceSize)) + } else { + status = .Fetching(progress: 0.0) + } + } else { status = .Remote } @@ -239,24 +245,50 @@ public final class MediaBox { } public func resourceData(_ resource: MediaResource, pathExtension: String? = nil, complete: Bool = true) -> Signal { - assert(pathExtension == nil) return Signal { subscriber in let disposable = MetaDisposable() self.concurrentQueue.async { let paths = self.storePathsForId(resource.id) if let completeSize = fileSize(paths.complete) { - subscriber.putNext(MediaResourceData(path: paths.complete, size: completeSize, complete: true)) - subscriber.putCompletion() + if let pathExtension = pathExtension { + let symlinkPath = paths.complete + ".\(pathExtension)" + if fileSize(symlinkPath) == nil { + let _ = try? FileManager.default.createSymbolicLink(atPath: symlinkPath, withDestinationPath: URL(fileURLWithPath: paths.complete).lastPathComponent) + } + subscriber.putNext(MediaResourceData(path: symlinkPath, size: completeSize, complete: true)) + subscriber.putCompletion() + } else { + subscriber.putNext(MediaResourceData(path: paths.complete, size: completeSize, complete: true)) + subscriber.putCompletion() + } } else { self.dataQueue.async { let resourceId = WrappedMediaResourceId(resource.id) var currentContext: ResourceDataContext? = self.dataContexts[resourceId] if let currentContext = currentContext, currentContext.data.complete { - subscriber.putNext(currentContext.data) - subscriber.putCompletion() + if let pathExtension = pathExtension { + let symlinkPath = paths.complete + ".\(pathExtension)" + if fileSize(symlinkPath) == nil { + let _ = try? FileManager.default.createSymbolicLink(atPath: symlinkPath, withDestinationPath: URL(fileURLWithPath: paths.complete).lastPathComponent) + } + subscriber.putNext(MediaResourceData(path: symlinkPath, size: currentContext.data.size, complete: currentContext.data.complete)) + subscriber.putCompletion() + } else { + subscriber.putNext(currentContext.data) + subscriber.putCompletion() + } } else if let completeSize = fileSize(paths.complete) { - subscriber.putNext(MediaResourceData(path: paths.complete, size: completeSize, complete: true)) - subscriber.putCompletion() + if let pathExtension = pathExtension { + let symlinkPath = paths.complete + ".\(pathExtension)" + if fileSize(symlinkPath) == nil { + let _ = try? FileManager.default.createSymbolicLink(atPath: symlinkPath, withDestinationPath: URL(fileURLWithPath: paths.complete).lastPathComponent) + } + subscriber.putNext(MediaResourceData(path: symlinkPath, size: completeSize, complete: true)) + subscriber.putCompletion() + } else { + subscriber.putNext(MediaResourceData(path: paths.complete, size: completeSize, complete: true)) + subscriber.putCompletion() + } } else { let dataContext: ResourceDataContext if let currentContext = currentContext { @@ -270,15 +302,33 @@ public final class MediaBox { let index: Bag<(MediaResourceData) -> Void>.Index if complete { index = dataContext.completeDataSubscribers.add { data in - subscriber.putNext(data) - subscriber.putCompletion() + if let pathExtension = pathExtension, data.complete { + let symlinkPath = paths.complete + ".\(pathExtension)" + if fileSize(symlinkPath) == nil { + let _ = try? FileManager.default.createSymbolicLink(atPath: symlinkPath, withDestinationPath: URL(fileURLWithPath: paths.complete).lastPathComponent) + } + subscriber.putNext(MediaResourceData(path: symlinkPath, size: data.size, complete: data.complete)) + subscriber.putCompletion() + } else { + subscriber.putNext(data) + subscriber.putCompletion() + } } subscriber.putNext(MediaResourceData(path: dataContext.data.path, size: 0, complete: false)) } else { index = dataContext.progresiveDataSubscribers.add { data in - subscriber.putNext(data) - if data.complete { + if let pathExtension = pathExtension, data.complete { + let symlinkPath = paths.complete + ".\(pathExtension)" + if fileSize(symlinkPath) == nil { + let _ = try? FileManager.default.createSymbolicLink(atPath: symlinkPath, withDestinationPath: URL(fileURLWithPath: paths.complete).lastPathComponent) + } + subscriber.putNext(MediaResourceData(path: symlinkPath, size: data.size, complete: data.complete)) subscriber.putCompletion() + } else { + subscriber.putNext(data) + if data.complete { + subscriber.putCompletion() + } } } subscriber.putNext(dataContext.data) @@ -455,8 +505,12 @@ public final class MediaBox { let index: Bag.Index = dataContext.fetchSubscribers.add(Void()) if dataContext.fetchDisposable == nil { - //let status: MediaResourceStatus = .Fetching(progress: Float(currentSize) / Float(resourceSize)) - let status: MediaResourceStatus = .Fetching(progress: 0.0) + let status: MediaResourceStatus + if let resourceSize = resource.size { + status = .Fetching(progress: Float(currentSize) / Float(resourceSize)) + } else { + status = .Fetching(progress: 0.0) + } self.statusQueue.async { if let statusContext = self.statusContexts[resourceId] { statusContext.status = status @@ -526,8 +580,11 @@ public final class MediaBox { if updatedData.complete { status = .Local } else { - //status = .Fetching(progress: Float(updatedSize) / Float(resourceSize)) - status = .Fetching(progress: 0.0) + if let resourceSize = resource.size { + status = .Fetching(progress: Float(updatedSize) / Float(resourceSize)) + } else { + status = .Fetching(progress: 0.0) + } } self.statusQueue.async { diff --git a/Postbox/PeerNameIndexRepresentation.swift b/Postbox/PeerNameIndexRepresentation.swift index ba6c28db35..4b9ee60c50 100644 --- a/Postbox/PeerNameIndexRepresentation.swift +++ b/Postbox/PeerNameIndexRepresentation.swift @@ -11,7 +11,7 @@ public enum PeerNameIndex { } extension PeerIndexNameRepresentation { - func indexName(_ index: PeerNameIndex) -> String { + public func indexName(_ index: PeerNameIndex) -> String { switch self { case let .title(title): return title @@ -25,7 +25,7 @@ extension PeerIndexNameRepresentation { } } - func match(query: String) -> Bool { + public func match(query: String) -> Bool { switch self { case let .title(title): return title.lowercased().hasPrefix(query)