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

feat(filesystem): Allow the use of absolute urls on iOS and web #250

Merged
merged 4 commits into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 10 additions & 12 deletions filesystem/ios/Plugin/Filesystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,32 +136,30 @@ import Foundation
/**
* Get the SearchPathDirectory corresponding to the JS string
*/
@objc public func getDirectory(directory: String) -> FileManager.SearchPathDirectory {
public func getDirectory(directory: String?) -> FileManager.SearchPathDirectory? {
switch directory {
case "DOCUMENTS":
return .documentDirectory
case "CACHE":
return .cachesDirectory
default:
jcesarmobile marked this conversation as resolved.
Show resolved Hide resolved
return .documentDirectory
return nil
}
}

/**
* Get the URL for this file, supporting file:// paths and
* files with directory mappings.
*/
@objc public func getFileUrl(at path: String, in directory: String) -> URL? {
if path.starts(with: "file://") {
return URL(string: path)
}

let directory = getDirectory(directory: directory)
@objc public func getFileUrl(at path: String, in directory: String?) -> URL? {
if let directory = getDirectory(directory: directory) {
guard let dir = FileManager.default.urls(for: directory, in: .userDomainMask).first else {
return nil
}

guard let dir = FileManager.default.urls(for: directory, in: .userDomainMask).first else {
return nil
return dir.appendingPathComponent(path)
} else {
return URL(string: path)
}

return dir.appendingPathComponent(path)
}
}
23 changes: 11 additions & 12 deletions filesystem/ios/Plugin/FilesystemPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import Capacitor
@objc(FilesystemPlugin)
public class FilesystemPlugin: CAPPlugin {
private let implementation = Filesystem()
let defaultDirectory = "DOCUMENTS"

/**
* Read a file from the filesystem.
Expand All @@ -20,7 +19,7 @@ public class FilesystemPlugin: CAPPlugin {
handleError(call, "path must be provided and must be a string.")
return
}
let directory = call.getString("directory") ?? defaultDirectory
let directory = call.getString("directory")

guard let fileUrl = implementation.getFileUrl(at: file, in: directory) else {
handleError(call, "Invalid path")
Expand Down Expand Up @@ -53,7 +52,7 @@ public class FilesystemPlugin: CAPPlugin {
return
}

let directory = call.getString("directory") ?? defaultDirectory
let directory = call.getString("directory")

guard let fileUrl = implementation.getFileUrl(at: file, in: directory) else {
handleError(call, "Invalid path")
Expand Down Expand Up @@ -86,7 +85,7 @@ public class FilesystemPlugin: CAPPlugin {
return
}

let directory = call.getString("directory") ?? defaultDirectory
let directory = call.getString("directory")
guard let fileUrl = implementation.getFileUrl(at: file, in: directory) else {
handleError(call, "Invalid path")
return
Expand All @@ -108,7 +107,7 @@ public class FilesystemPlugin: CAPPlugin {
return
}

let directory = call.getString("directory") ?? defaultDirectory
let directory = call.getString("directory")
guard let fileUrl = implementation.getFileUrl(at: file, in: directory) else {
handleError(call, "Invalid path")
return
Expand All @@ -132,7 +131,7 @@ public class FilesystemPlugin: CAPPlugin {
}

let recursive = call.getBool("recursive") ?? false
let directory = call.getString("directory") ?? defaultDirectory
let directory = call.getString("directory")
guard let fileUrl = implementation.getFileUrl(at: path, in: directory) else {
handleError(call, "Invalid path")
return
Expand All @@ -155,7 +154,7 @@ public class FilesystemPlugin: CAPPlugin {
return
}

let directory = call.getString("directory") ?? defaultDirectory
let directory = call.getString("directory")
guard let fileUrl = implementation.getFileUrl(at: path, in: directory) else {
handleError(call, "Invalid path")
return
Expand All @@ -180,7 +179,7 @@ public class FilesystemPlugin: CAPPlugin {
return
}

let directory = call.getString("directory") ?? defaultDirectory
let directory = call.getString("directory")
guard let fileUrl = implementation.getFileUrl(at: path, in: directory) else {
handleError(call, "Invalid path")
return
Expand All @@ -205,7 +204,7 @@ public class FilesystemPlugin: CAPPlugin {
return
}

let directory = call.getString("directory") ?? defaultDirectory
let directory = call.getString("directory")
guard let fileUrl = implementation.getFileUrl(at: path, in: directory) else {
handleError(call, "Invalid path")
return
Expand All @@ -231,7 +230,7 @@ public class FilesystemPlugin: CAPPlugin {
return
}

let directory = call.getString("directory") ?? defaultDirectory
let directory = call.getString("directory")
guard let fileUrl = implementation.getFileUrl(at: path, in: directory) else {
handleError(call, "Invalid path")
return
Expand All @@ -252,7 +251,7 @@ public class FilesystemPlugin: CAPPlugin {
return
}

let directory = call.getString("directory") ?? defaultDirectory
let directory = call.getString("directory")
let toDirectory = call.getString("toDirectory") ?? directory

guard let fromUrl = implementation.getFileUrl(at: from, in: directory) else {
Expand Down Expand Up @@ -281,7 +280,7 @@ public class FilesystemPlugin: CAPPlugin {
return
}

let directory = call.getString("directory") ?? defaultDirectory
let directory = call.getString("directory")
let toDirectory = call.getString("toDirectory") ?? directory

guard let fromUrl = implementation.getFileUrl(at: from, in: directory) else {
Expand Down
11 changes: 4 additions & 7 deletions filesystem/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ import type {
StatResult,
WriteFileOptions,
WriteFileResult,
Directory,
} from './definitions';
import { Directory } from './definitions';

export class FilesystemWeb extends WebPlugin implements FilesystemPlugin {
DEFAULT_DIRECTORY = Directory.Data;
DB_VERSION = 1;
DB_NAME = 'Disc';

Expand Down Expand Up @@ -105,10 +104,10 @@ export class FilesystemWeb extends WebPlugin implements FilesystemPlugin {
directory: Directory | undefined,
uriPath: string | undefined,
): string {
directory = directory || this.DEFAULT_DIRECTORY;
const cleanedUriPath =
uriPath !== undefined ? uriPath.replace(/^[/]+|[/]+$/g, '') : '';
let fsPath = '/' + directory;
let fsPath = '';
if (directory !== undefined) fsPath += '/' + directory;
if (uriPath !== '') fsPath += '/' + cleanedUriPath;
return fsPath;
}
Expand Down Expand Up @@ -351,10 +350,8 @@ export class FilesystemWeb extends WebPlugin implements FilesystemPlugin {
if (entry === undefined) {
entry = (await this.dbRequest('get', [path + '/'])) as EntryObj;
}
if (entry === undefined) throw Error('Entry does not exist.');

return {
uri: entry.path,
uri: entry?.path || path,
};
}

Expand Down