WIP: PDF URLs

This commit is contained in:
Jackson Harper
2022-06-08 20:11:09 -07:00
parent 7522f22f51
commit 5537766a45
6 changed files with 40 additions and 4 deletions

View File

@ -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)"
}
}
}

View File

@ -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)

View File

@ -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

View File

@ -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 {

View File

@ -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

View File

@ -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