Merge pull request #783 from omnivore-app/fix/local-pdf-saving

Improve local PDF saving
This commit is contained in:
Jackson Harper
2022-06-13 12:47:13 -07:00
committed by GitHub
9 changed files with 52 additions and 13 deletions

View File

@ -3,6 +3,7 @@ import CoreData
import Foundation
import Models
import Services
import Utils
public final class PDFViewerViewModel: ObservableObject {
@Published public var errorMessage: String?
@ -93,6 +94,11 @@ public final class PDFViewerViewModel: ObservableObject {
if itemDownloaded {
return pdfItem.localPdfURL
}
if let tempURL = pdfItem.tempPDFURL {
if let localURL = try? PDFUtils.copyToLocal(url: tempURL) {
return tempURL
}
}
if let localURL = try await dataService.fetchPDFData(slug: pdfItem.slug, pageURLString: pdfItem.originalArticleURL) {
return localURL
}

View File

@ -44,6 +44,7 @@
<attribute name="siteName" optional="YES" attributeType="String"/>
<attribute name="slug" attributeType="String"/>
<attribute name="state" optional="YES" attributeType="String"/>
<attribute name="tempPDFURL" optional="YES" attributeType="URI"/>
<attribute name="title" attributeType="String"/>
<attribute name="updatedAt" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<relationship name="highlights" toMany="YES" deletionRule="Cascade" destinationEntity="Highlight" inverseName="linkedItem" inverseEntity="Highlight"/>
@ -91,7 +92,7 @@
</entity>
<elements>
<element name="Highlight" positionX="27" positionY="225" width="128" height="224"/>
<element name="LinkedItem" positionX="-18" positionY="63" width="128" height="434"/>
<element name="LinkedItem" positionX="-18" positionY="63" width="128" height="449"/>
<element name="LinkedItemLabel" positionX="-36" positionY="18" width="128" height="134"/>
<element name="NewsletterEmail" positionX="0" positionY="180" width="128" height="74"/>
<element name="Viewer" positionX="45" positionY="234" width="128" height="89"/>

View File

@ -47,7 +47,7 @@ public extension LinkedItem {
var isReadyToRead: Bool {
if isPDF {
// If its a PDF we verify the local file is available
return PDFUtils.exists(filename: localPDF)
return PDFUtils.exists(filename: localPDF) || PDFUtils.tempExists(tempPDFURL: tempPDFURL)
}
// Check the state and whether we have HTML
return state == "SUCCEEDED"

View File

@ -7,6 +7,7 @@ public struct PDFItem {
public let itemID: String
public let pdfURL: URL?
public let localPDF: String?
public let tempPDFURL: URL?
public let title: String
public let slug: String
public let readingProgress: Double
@ -24,6 +25,7 @@ public struct PDFItem {
itemID: item.unwrappedID,
pdfURL: URL(string: item.unwrappedPageURLString),
localPDF: item.localPDF,
tempPDFURL: item.tempPDFURL,
title: item.unwrappedID,
slug: item.unwrappedSlug,
readingProgress: item.readingProgress,

View File

@ -148,9 +148,8 @@ public final class DataService: ObservableObject {
switch pageScrape.contentType {
case let .pdf(localUrl):
linkedItem.contentReader = "PDF"
linkedItem.tempPDFURL = localUrl
linkedItem.title = PDFUtils.titleFromPdfFile(pageScrape.url)
print("PERSISTING PDF", localUrl)
linkedItem.localPDF = try PDFUtils.copyToLocal(url: localUrl)
case let .html(html: html, title: title, iconURL: iconURL):
linkedItem.contentReader = "WEB"
linkedItem.originalHtml = html

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,9 +228,30 @@ 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 {
if item.isPDF && needsPDFDownload {
try await fetchPDFData(slug: item.slug, pageURLString: item.pageURLString)
}
@ -275,6 +298,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,21 @@ 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
}
public static func tempExists(tempPDFURL: URL?) -> Bool {
if let tempPDFURL = tempPDFURL {
return FileManager.default.fileExists(atPath: tempPDFURL.path)
}
return false
}