diff --git a/apple/OmnivoreKit/Sources/App/PDFSupport/PDFViewer.swift b/apple/OmnivoreKit/Sources/App/PDFSupport/PDFViewer.swift index c1ef3e84a..d6217081c 100644 --- a/apple/OmnivoreKit/Sources/App/PDFSupport/PDFViewer.swift +++ b/apple/OmnivoreKit/Sources/App/PDFSupport/PDFViewer.swift @@ -141,12 +141,13 @@ import Utils .task { // NOTE: the issue here is the PDF is downloaded, but saved to a URL we don't know about // because it is changed. - if let pdfURL = await viewModel.downloadPDF(dataService: dataService) { + let pdfURL = await viewModel.downloadPDF(dataService: dataService) + if let pdfURL = pdfURL { let document = HighlightedDocument(url: pdfURL, viewModel: viewModel) pdfStateObject.document = document pdfStateObject.coordinator = PDFViewCoordinator(document: document, viewModel: viewModel) } else { - errorMessage = "Unable to download PDF." + errorMessage = "Unable to download PDF: \(pdfURL)" } } } diff --git a/apple/OmnivoreKit/Sources/Models/PageScrapePayload.swift b/apple/OmnivoreKit/Sources/Models/PageScrapePayload.swift index 1b997e82f..106d425b4 100644 --- a/apple/OmnivoreKit/Sources/Models/PageScrapePayload.swift +++ b/apple/OmnivoreKit/Sources/Models/PageScrapePayload.swift @@ -256,7 +256,20 @@ private extension PageScrapePayload { let localFile = UUID().uuidString.lowercased() + ".pdf" dest.appendPathComponent(localFile) do { + print("EXISTING PDF URL", url) + let attr = try? FileManager.default.attributesOfItem(atPath: url.path) + if let attr = attr { + print("EXISTING FILE SIZE", attr[.size]) + } + try FileManager.default.copyItem(at: url, to: dest) + print("COPIED TO URL", dest) + + let attr2 = try? FileManager.default.attributesOfItem(atPath: dest.path) + if let attr2 = attr2 { + print("COPIED FILE SIZE", attr2[.size]) + } + return PageScrapePayload(url: url.absoluteString, localUrl: dest) } catch { print("error copying file locally", error) diff --git a/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift b/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift index 0d3ef1d42..eb464759c 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift @@ -149,7 +149,8 @@ public final class DataService: ObservableObject { case let .pdf(localUrl): linkedItem.contentReader = "PDF" linkedItem.title = PDFUtils.titleFromPdfFile(pageScrape.url) - linkedItem.localPDF = try PDFUtils.moveToLocal(url: localUrl) + 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 diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/SavePDF.swift b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/SavePDF.swift index dd62e5639..c0e9e186e 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Mutations/SavePDF.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Mutations/SavePDF.swift @@ -19,7 +19,7 @@ public extension DataService { let input = InputObjects.UploadFileRequestInput( clientRequestId: OptionalArgument(id), contentType: "application/pdf", - createPageEntry: OptionalArgument(false), + createPageEntry: OptionalArgument(true), url: url ) @@ -83,6 +83,12 @@ public extension DataService { request.httpMethod = "PUT" request.addValue("application/pdf", forHTTPHeaderField: "content-type") + print("UPLOADING PDF", localPdfURL) + let attr = try? FileManager.default.attributesOfItem(atPath: localPdfURL.path) + if let attr = attr { + print("UPLOADING ATTR", attr[.size]) + } + return try await withCheckedThrowingContinuation { continuation in let task = networker.urlSession.uploadTask(with: request, fromFile: localPdfURL) { _, response, _ in if let httpResponse = response as? HTTPURLResponse, 200 ... 299 ~= httpResponse.statusCode { diff --git a/apple/OmnivoreKit/Sources/Services/DataService/OfflineSync.swift b/apple/OmnivoreKit/Sources/Services/DataService/OfflineSync.swift index 840622f67..c5c5e2777 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/OfflineSync.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/OfflineSync.swift @@ -55,6 +55,11 @@ public extension DataService { let uploadRequest = try await uploadFileRequest(id: id, url: url) 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) } else { throw SaveArticleError.badData diff --git a/apple/OmnivoreKit/Sources/Utils/PDFUtils.swift b/apple/OmnivoreKit/Sources/Utils/PDFUtils.swift index 5989b57e4..0d4a4c19c 100644 --- a/apple/OmnivoreKit/Sources/Utils/PDFUtils.swift +++ b/apple/OmnivoreKit/Sources/Utils/PDFUtils.swift @@ -11,6 +11,16 @@ import QuickLookThumbnailing import UIKit public enum PDFUtils { + public static func copyToLocal(url: URL) throws -> String { + let subPath = UUID().uuidString + ".pdf" + let dest = FileManager.default + .urls(for: .documentDirectory, in: .userDomainMask)[0] + .appendingPathComponent(subPath) + + try FileManager.default.copyItem(at: url, to: dest) + return subPath + } + public static func moveToLocal(url: URL) throws -> String { let subPath = UUID().uuidString + ".pdf" let dest = FileManager.default