Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import Foundation
import Testing
import WordPressAPI
import WordPressKit

@testable import WordPress

/// `URL.path()` percent-encodes by default, unlike the legacy `url.path` property, so the
/// upload API was handed a path that doesn't exist on disk whenever the filename needed
/// encoding. A space is enough, which makes it reachable with ordinary content.
struct MediaCreateParamsFilePathTests {

@Test func decodesPercentEncodingInTheFilePath() throws {
let media = RemoteMedia()
media.localURL = URL(fileURLWithPath: "/tmp/media/screen recording 1.mp4")

let params = try #require(MediaCreateParams(media: media))

#expect(params.filePath == "/tmp/media/screen recording 1.mp4")
}

/// Characters beyond the space get encoded too, so check one of those as well.
@Test func decodesPercentEncodingBeyondSpaces() throws {
let media = RemoteMedia()
media.localURL = URL(fileURLWithPath: "/tmp/media/100% done.jpg")

let params = try #require(MediaCreateParams(media: media))

#expect(params.filePath == "/tmp/media/100% done.jpg")
}

/// A name needing no encoding has to survive untouched.
@Test func leavesAnOrdinaryPathAlone() throws {
let media = RemoteMedia()
media.localURL = URL(fileURLWithPath: "/tmp/media/IMG_0001.jpg")

let params = try #require(MediaCreateParams(media: media))

#expect(params.filePath == "/tmp/media/IMG_0001.jpg")
}
}
8 changes: 6 additions & 2 deletions WordPress/Classes/Services/MediaServiceRemoteCoreREST.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ private extension RemoteMedia {
}
}

private extension MediaCreateParams {
// Not `private`: the file-path handling below is covered by
// `MediaCreateParamsFilePathTests`.
extension MediaCreateParams {
init?(media: RemoteMedia) {

guard let localURL = media.localURL else {
Expand All @@ -198,7 +200,9 @@ private extension MediaCreateParams {
caption: media.caption,
description: media.descriptionText,
postId: media.postID?.int64Value,
filePath: localURL.path()
// `path()` percent-encodes by default, so a filename with a space (a screen
// recording, say) becomes a path that doesn't exist on disk.
filePath: localURL.path(percentEncoded: false)
)
}
}
Expand Down