apache / cordova-plugin-file

Apache Cordova File Plugin
https://cordova.apache.org/
Apache License 2.0
739 stars 757 forks source link

browser: cordova.file.applicationDirectory returns an http:// URL instead of a file:// one #619

Open gonzoMD opened 1 month ago

gonzoMD commented 1 month ago

Bug Report

Problem

cordova.file.applicationDirectory returns an "http://" URL instead of a "file://" one. Therefore I am unable to list files inside a directory with window.resolveLocalFileSystemURL.

What is expected to happen?

cordova.file.applicationDirectory returns a "file://" URL which I can use further.

What does actually happen?

it offers an "http://" URL and window.resolveLocalFileSystemURL fails with Error 5.

Bildschirmfoto 2024-04-03 um 17 35 09

Information

On my Android device everything works as expected, I get a "file://" URL, which gets properly resolved

Command or Code

I use the Onsen UI framework, but this works with normal html buttons too <ons-button onclick="alert(cordova.file.applicationDirectory)">test</ons-button>

Environment, Platform, Device

Browser platform Safari 17.4.1

Version information

Cordova Packages:

    cli: 12.0.0
        common: 5.0.0
        create: 5.0.0
        lib: 12.0.1
            common: 5.0.0
            fetch: 4.0.0
            serve: 4.0.1

Project Installed Platforms:

    android: 12.0.1
    browser: 7.0.0

Project Installed Plugins:

    cordova-plugin-file: 8.0.1
    cordova-sqlite-evmax-build-free: 0.0.10-dev
    cordova-sqlite-storage: 6.1.0

Environment:

    OS: macOS Sonoma 14.4.1 (23E224) (darwin 23.4.0) x64
    Node: v21.6.1
    npm: 10.2.4

android Environment:

    android:
[=======================================] 100% Fetch remote repository...       
Available Android targets:
----------
id: 1 or "android-33"
     Name: Android API 33, extension level 3
     Type: Platform
     API level: 33
     Revision: 3
----------
id: 2 or "android-34"
     Name: Android API 34, extension level 7
     Type: Platform
     API level: 34
     Revision: 2

Project Setting Files:

    config.xml:
<?xml version='1.0' encoding='utf-8'?>
<widget id="com.rnaumann.schwalbenshop" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>Schwalbenshop</name>
    <description>Sample Apache Cordova App</description>
    <author email="dev@cordova.apache.org" href="https://cordova.apache.org">
        Apache Cordova Team
    </author>
    <content src="index.html" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
     <icon src="swallow.png" />
</widget>

    package.json:
--- Start of Cordova JSON Snippet ---
{
  "platforms": [
    "browser",
    "android"
  ],
  "plugins": {
    "cordova-sqlite-evmax-build-free": {},
    "cordova-sqlite-storage": {},
    "cordova-plugin-file": {
      "ANDROIDX_WEBKIT_VERSION": "1.4.0"
    }
  }
}
--- End of Cordova JSON Snippet ---

Checklist

breautek commented 1 month ago

I don't think this is a bug... but probably an undocumented caveat with the browser implementation.

applicationDirectory is the directory of your app installation. On iOS/Android, this refers to the read-only directory of your unpacked application bundle. The browser has no direct equivalency of this as your "application" isn't stored locally on disk. Hence why it returns the location origin instead. There is also no HTTP api to list the contents listing your app files for the browser platform so listing browser app files is something that isn't really achievable.

Conceptually, https://localhost (or whatever what the site address) makes the most sense for applicationDirectory constant on the browser platform.