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

fix(filesystem): respect downloadFile recursive option on Android and iOS #2324

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
Expand Up @@ -340,6 +340,7 @@ private JSObject doDownloadInBackground(String urlString, PluginCall call, Bridg
Boolean disableRedirects = call.getBoolean("disableRedirects");
Boolean shouldEncode = call.getBoolean("shouldEncodeUrlParams", true);
Boolean progress = call.getBoolean("progress", false);
Boolean recursive = call.getBoolean("recursive", false);

String method = call.getString("method", "GET").toUpperCase(Locale.ROOT);
String path = call.getString("path");
Expand All @@ -348,6 +349,10 @@ private JSObject doDownloadInBackground(String urlString, PluginCall call, Bridg
final URL url = new URL(urlString);
final File file = getFileObject(path, directory);

if(recursive) {
file.getParentFile().mkdirs();
}

HttpRequestHandler.HttpURLConnectionBuilder connectionBuilder = new HttpRequestHandler.HttpURLConnectionBuilder()
.setUrl(url)
.setMethod(method)
Expand Down
3 changes: 2 additions & 1 deletion filesystem/ios/Sources/FilesystemPlugin/Filesystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ import Capacitor
// swiftlint:disable function_body_length
@objc public func downloadFile(call: CAPPluginCall, emitter: @escaping ProgressEmitter, config: InstanceConfiguration?) throws {
let directory = call.getString("directory", "DOCUMENTS")
let recursive = call.getBool("recursive", false)
guard let path = call.getString("path") else {
call.reject("Invalid file path")
return
Expand Down Expand Up @@ -222,7 +223,7 @@ import Capacitor
let dest = dir!.appendingPathComponent(path)
CAPLog.print("Attempting to write to file destination: \(dest.absoluteString)")

if !FileManager.default.fileExists(atPath: dest.deletingLastPathComponent().absoluteString) {
if recursive && !FileManager.default.fileExists(atPath: dest.deletingLastPathComponent().absoluteString) {
try FileManager.default.createDirectory(at: dest.deletingLastPathComponent(), withIntermediateDirectories: true, attributes: nil)
}

Expand Down