Skip to content

Commit 8773619

Browse files
authored
fix(storage): update storage to support downloading if a file already exist locally (#2825)
* fix(storage): update to support downloading a file that already exists on local device * chore(storage): fix test use case naming
1 parent 13cdff8 commit 8773619

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

AmplifyPlugins/Storage/Sources/AWSS3StoragePlugin/Support/Internal/FileSystem.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,7 @@ class FileSystem {
175175
}
176176

177177
func moveFile(from sourceFileURL: URL, to destinationURL: URL) throws {
178-
guard !FileManager.default.fileExists(atPath: destinationURL.path) else {
179-
throw Failure.fatalError(errorDescription: "File already exists at destination: \(destinationURL.path)")
180-
}
178+
removeFileIfExists(fileURL: destinationURL)
181179
try FileManager.default.moveItem(atPath: sourceFileURL.path, toPath: destinationURL.path)
182180
}
183181

AmplifyPlugins/Storage/Tests/AWSS3StoragePluginTests/Support/Internal/FileSystemTests.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,39 @@ extension Bytes {
4646

4747
class FileSystemTests: XCTestCase {
4848

49+
func testMoveFile_Succeeds_WhenMoveFileExecuted() throws {
50+
let fs = FileSystem()
51+
let directoryURL = fs.createTemporaryDirectoryURL()
52+
try fs.createDirectory(at: directoryURL)
53+
let bytes = Bytes.megabytes(3)
54+
let data = bytes.generateRandomData()
55+
let sourceFileUrl = try fs.createTemporaryFile(data: data, baseURL: directoryURL)
56+
XCTAssertTrue(fs.fileExists(atURL: sourceFileUrl))
57+
let destinationUrl = fs.documentsURL.appendingPathComponent("destination.tmp")
58+
try fs.moveFile(from: sourceFileUrl, to: destinationUrl)
59+
XCTAssertTrue(fs.fileExists(atURL: destinationUrl))
60+
fs.removeDirectoryIfExists(directoryURL: sourceFileUrl)
61+
fs.removeDirectoryIfExists(directoryURL: destinationUrl)
62+
}
63+
64+
func testMoveFile_Succeeds_WhenFileAlreadyExists() throws {
65+
let fs = FileSystem()
66+
let directoryURL = fs.createTemporaryDirectoryURL()
67+
try fs.createDirectory(at: directoryURL)
68+
let bytes = Bytes.megabytes(3)
69+
let data = bytes.generateRandomData()
70+
var sourceFileUrl = try fs.createTemporaryFile(data: data, baseURL: directoryURL)
71+
XCTAssertTrue(fs.fileExists(atURL: sourceFileUrl))
72+
let destinationUrl = fs.documentsURL.appendingPathComponent("destination.tmp")
73+
try fs.moveFile(from: sourceFileUrl, to: destinationUrl)
74+
XCTAssertTrue(fs.fileExists(atURL: destinationUrl))
75+
sourceFileUrl = try fs.createTemporaryFile(data: data, baseURL: directoryURL)
76+
try fs.moveFile(from: sourceFileUrl, to: destinationUrl)
77+
XCTAssertTrue(fs.fileExists(atURL: destinationUrl))
78+
fs.removeDirectoryIfExists(directoryURL: sourceFileUrl)
79+
fs.removeDirectoryIfExists(directoryURL: destinationUrl)
80+
}
81+
4982
func testFileExists() throws {
5083
let fs = FileSystem()
5184
let directoryURL = fs.createTemporaryDirectoryURL()

0 commit comments

Comments
 (0)