Skip to content

Commit

Permalink
feat(filesystem): Allow the use of absolute urls on iOS and web (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Feb 18, 2021
1 parent 4ec8d06 commit 03ad97c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 37 deletions.
35 changes: 17 additions & 18 deletions filesystem/ios/Plugin/Filesystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,32 +136,31 @@ import Foundation
/**
* Get the SearchPathDirectory corresponding to the JS string
*/
@objc public func getDirectory(directory: String) -> FileManager.SearchPathDirectory {
switch directory {
case "DOCUMENTS":
return .documentDirectory
case "CACHE":
return .cachesDirectory
default:
return .documentDirectory
public func getDirectory(directory: String?) -> FileManager.SearchPathDirectory? {
if let directory = directory {
switch directory {
case "CACHE":
return .cachesDirectory
default:
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

0 comments on commit 03ad97c

Please sign in to comment.