Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve Image Filenames #1485

Merged
merged 3 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions Mlem/App/Logic/ImageFunctions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,28 @@ func fullSizeUrl(url: URL?) -> URL? {
}

/// Downloads the image at the given URL to the file system, returning the path to the downloaded image
func downloadImageToFileSystem(url: URL, fileName: String) async -> URL? {
func downloadImageToFileSystem(url: URL) async -> URL? {
do {
let (data, _) = try await ImagePipeline.shared.data(for: .init(url: url))
var fileType = url.pathExtension
var fileName: String

// image proxies that use url query param don't have pathExtension so we extract it from the embedded url
if fileType.isEmpty,
if url.pathExtension.isEmpty,
let components = URLComponents(url: url, resolvingAgainstBaseURL: true),
let queryItems = components.queryItems,
let baseUrlString = queryItems.first(where: { $0.name == "url" })?.value,
let baseUrl = URL(string: baseUrlString) {
fileType = baseUrl.pathExtension
fileName = baseUrl.lastPathComponent
} else {
fileName = url.lastPathComponent
}

if fileType.isEmpty {
assertionFailure("Empty fileType!")
if fileName.isEmpty {
assertionFailure("Empty fileName!")
return nil
}

let fileUrl = FileManager.default.temporaryDirectory.appending(path: "\(fileName).\(fileType)")
let fileUrl = FileManager.default.temporaryDirectory.appending(path: fileName)
if FileManager.default.fileExists(atPath: fileUrl.absoluteString) {
try FileManager.default.removeItem(at: fileUrl)
}
Expand Down
4 changes: 2 additions & 2 deletions Mlem/App/Views/Shared/Images/Core/DynamicMediaView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,13 @@ struct DynamicMediaView: View {
}

func shareImage(url: URL) async {
if let fileUrl = await downloadImageToFileSystem(url: url, fileName: "image") {
if let fileUrl = await downloadImageToFileSystem(url: url) {
navigation.shareInfo = .init(url: fileUrl)
}
}

func showQuickLook(url: URL) async {
if let fileUrl = await downloadImageToFileSystem(url: url, fileName: "quicklook") {
if let fileUrl = await downloadImageToFileSystem(url: url) {
quickLookUrl = fileUrl
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ struct ThumbnailImageView: View {
}

func shareImage(url: URL) async {
if let fileUrl = await downloadImageToFileSystem(url: url, fileName: "image") {
if let fileUrl = await downloadImageToFileSystem(url: url) {
navigation.shareInfo = .init(url: fileUrl)
}
}

func showQuickLook(url: URL) async {
if let fileUrl = await downloadImageToFileSystem(url: url, fileName: "quicklook") {
if let fileUrl = await downloadImageToFileSystem(url: url) {
quickLookUrl = fileUrl
}
}
Expand Down
Loading