Handle changing IDs when local PDFs are saved

This commit is contained in:
Jackson Harper 2022-06-12 14:36:35 -07:00
parent 5a251bfe88
commit 7084f18932
4 changed files with 48 additions and 11 deletions

View file

@ -4,6 +4,7 @@ import Models
import SwiftGraphQL
public struct UploadFileRequestPayload {
public let pageId: String
public let uploadID: String?
public let uploadFileID: String?
public let urlString: String?
@ -29,6 +30,7 @@ public extension DataService {
uploadFileRequestSuccess: .init {
.success(
payload: UploadFileRequestPayload(
pageId: (try $0.createdPageId()) ?? id,
uploadID: try $0.id(),
uploadFileID: try $0.uploadFileId(),
urlString: try $0.uploadSignedUrl()

View file

@ -54,18 +54,14 @@ public extension DataService {
try await updateLinkedItemStatus(id: id, newId: nil, status: .isSyncing)
let uploadRequest = try await uploadFileRequest(id: id, url: url)
print("UPLOAD REQUEST, ORIGINAL ID, NEW ID", id, uploadRequest.pageId)
if let urlString = uploadRequest.urlString, let uploadUrl = URL(string: urlString) {
let attr = try? FileManager.default.attributesOfItem(atPath: localPdfURL.path)
if let attr = attr {
print("ATTR", attr[.size])
}
try await uploadFile(id: id, localPdfURL: localPdfURL, url: uploadUrl)
try await uploadFile(id: uploadRequest.pageId, localPdfURL: localPdfURL, url: uploadUrl)
} else {
throw SaveArticleError.badData
}
try await updateLinkedItemStatus(id: id, newId: nil, status: .isNSync)
try await updateLinkedItemStatus(id: id, newId: uploadRequest.pageId, status: .isNSync)
try backgroundContext.performAndWait {
try backgroundContext.save()
}

View file

@ -195,6 +195,8 @@ public extension DataService {
}
internal func persistArticleContent(item: InternalLinkedItem, htmlContent: String, highlights: [InternalHighlight]) async throws {
var needsPDFDownload = false
try await backgroundContext.perform { [weak self] in
guard let self = self else { return }
let fetchRequest: NSFetchRequest<Models.LinkedItem> = LinkedItem.fetchRequest()
@ -226,19 +228,53 @@ public extension DataService {
linkedItem.isArchived = item.isArchived
linkedItem.contentReader = item.contentReader
linkedItem.serverSyncStatus = Int64(ServerSyncStatus.isNSync.rawValue)
if item.isPDF {
needsPDFDownload = true
// Check if we already have the PDF item locally. Either in temporary
// space, or in the documents directory
if let localPDF = existingItem?.localPDF {
if PDFUtils.exists(filename: localPDF) {
linkedItem.localPDF = localPDF
needsPDFDownload = false
}
}
if let tempPDFURL = existingItem?.tempPDFURL {
linkedItem.localPDF = try? PDFUtils.moveToLocal(url: tempPDFURL)
PDFUtils.exists(filename: linkedItem.localPDF)
if linkedItem.localPDF != nil {
needsPDFDownload = false
}
}
}
}
if item.isPDF {
try await fetchPDFData(slug: item.slug, pageURLString: item.pageURLString)
if needsPDFDownload {
print("PDF does not exist, downloading", item.id, item.title)
try await backgroundContext.perform {
let fetchRequest: NSFetchRequest<Models.LinkedItem> = LinkedItem.fetchRequest()
fetchRequest.predicate = NSPredicate(format: "id == %@", item.id)
let existingItem = try? self.backgroundContext.fetch(fetchRequest).first
print("EXISTING ITEM", existingItem)
}
try await fetchPDFData(slug: item.slug, pageURLString: item.pageURLString)
} else {
print("PDF already exists, not downloading", item.id)
}
}
try await backgroundContext.perform { [weak self] in
do {
try self?.backgroundContext.save()
logger.debug("ArticleContent saved succesfully")
// logger.debug("ArticleContent saved succesfully")
} catch {
self?.backgroundContext.rollback()
logger.debug("Failed to save ArticleContent")
// logger.debug("Failed to save ArticleContent")
throw error
}
}
@ -275,6 +311,7 @@ public extension DataService {
try data.write(to: tempPath)
let localPDF = try PDFUtils.moveToLocal(url: tempPath)
localPdfURL = PDFUtils.localPdfURL(filename: localPDF)
linkedItem.tempPDFURL = nil
linkedItem.localPDF = localPDF
try self?.backgroundContext.save()
} catch {

View file

@ -35,12 +35,14 @@ public enum PDFUtils {
let url = FileManager.default
.urls(for: .documentDirectory, in: .userDomainMask)[0]
.appendingPathComponent(filename)
return url
}
public static func exists(filename: String?) -> Bool {
if let filename = filename, let localPdfURL = localPdfURL(filename: filename) {
return FileManager.default.fileExists(atPath: localPdfURL.absoluteString)
let result = FileManager.default.fileExists(atPath: localPdfURL.path)
return result
}
return false
}