markmarijnissen / cordova-promise-fs Goto Github PK
View Code? Open in Web Editor NEWWraps the Cordova File API in convenient functions (that return a Promise)
License: MIT License
Wraps the Cordova File API in convenient functions (that return a Promise)
License: MIT License
Hi,
I am newer to JS program. When use this library, how can I get the downloaded filename, when I try to download a queue url, like:
for each( queue[] )
promiseFS.download(url, savePath, { retry: retry }, progress ).then(onDone, onFail);
Thanks advance!
Hi. I have a Cordova application and I would like to download pdf/video files and open them via fileopener on an android device. Because of permission issues, I have to download them to an external/shared location: cordova.file.externalDataDirectory.
This script used to work before, but on this new device I've got, it has some issues. I can't download files to the cordova.file.externalDataDirectory. If I set relative path it downloads it successfully to the cordova.file.dataDirectory, but I can't open it. If I set absolute path to the "to" parameter it breaks.
cordova: v5.6
android: v4.4.2
device: Hudl 2
*This works: *
from = 'https://mywebsite.co.uk/test.pdf';
to = 'file:///storage/emulated/0/Android/data/en.myapp/files/test.pdf';
fileTransfer = new FileTransfer();
fileTransfer.download(
from,
to,
function(entry) {
console.log("download complete: " + entry.toURL());
},
function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
},
false
);
//gives:
//download complete: file:///storage/emulated/0/Android/data/en.myapp/files/test.pdf
//works fine tested with fileopener
*This doesn't work: *
from = 'https://mywebsite.co.uk/test.pdf';
to = 'file:///storage/emulated/0/Android/data/en.myapp/files/test.pdf';
fs = CordovaPromiseFS({
persistent: true, // or false
storageSize: 0, // storage size in bytes - 0 unlimited
concurrency: 2, // how many concurrent uploads/downloads?
Promise: Promise // Your favorite Promise/A+ library!
});
fs.download(from, to, function(e){console.log(e)}, function(e){console.log(e)})
//gives and object. Snippet:
...
_settledValue: FileEntry
filesystem: FileSystem
fullPath: "/file:/storage/emulated/0/Android/data/en.myapp/files/test.pdf"
isDirectory: false
isFile: true
name: "test.pdf"
nativeURL: "file:///data/data/en.myapp/files/files/file%3A/storage/emulated/0/Android/data/en.myapp/files/test.pdf"
__proto__: utils.extend.F
...
dir()
fails consistently on Windows 10 using the latest cordova-plugin-file, 4.3.3.
The core issue seems to be:
dir()
passes its path to normalize()
, which adds a "/" to the end, so "foo/bar" becomes "foo/bar/"
on Windows 10, the getDirectory()
method of FileProxy.js splits that path based on the last "/" in the path. See the split into name
and wpath
here: https://github.com/apache/cordova-plugin-file/blob/master/src/windows/FileProxy.js#L758
so "foo/bar/" splits into "foo/bar" and ""
FileProxy.js then gets the first part, "foo/bar", successfully
FileProxy.js then attempts to get the second part, "", relative to the first, but since "" isn't a valid filename, it throws an exception
We're working around this currently by adding a "." to our directory names when we're running on Windows. This fakes normalize()
into thinking our directories are files and it therefore doesn't add the final "/". That works, but is pretty ugly.
Download fails on Android when url's contain '../' in it. I'm not entirely sure but IIRC this doesn't seem to be an issue on iOS.
I want to make a PR with a fix but I wanted to check with you if you agree with me that this should be fixed in the function 'normalize'.
Is it okay with you if I improve the function normalize so that it will return a normalized path?
Like this:
"http://10.10.1.104:9000/site//Mobile/../css/tinymce-content.css"
into
"http://10.10.1.104:9000/site/css/tinymce-content.css"
Hi!
In certain Android versions (e.g. 4.3) the blob constructor does not work. Using the constructor, an error is thrown: "TypeError: Illegal constructor".
We use cordova-promise-fs in combination with cordova-app-loader, and sometimes on our Android 4.3 test device, the update failes because the blob constructor is used.
The problematic part of the code is the following:
/* write contents to a file */
function write(path,blob,mimeType) {
return ensure(dirname(path))
.then(function() { return file(path,{create:true}); })
.then(function(fileEntry) {
return new Promise(function(resolve,reject){
fileEntry.createWriter(function(writer){
writer.onwriteend = resolve;
writer.onerror = reject;
if(typeof blob === 'string') {
blob = new Blob([blob],{type: mimeType || 'text/plain'});
} else if(blob instanceof Blob !== true){
blob = new Blob([JSON.stringify(blob,null,4)],{type: mimeType || 'application/json'});
}
writer.write(blob);
},reject);
});
});
}
Though deprecated, the BlobBuilder should be used if creating the Blob via constructor fails. See https://developer.mozilla.org/en-US/docs/Web/API/BlobBuilder
I'll try to create a pull request in the next days...
I'm developing a Cordova application and wanted to be able to debug it without a device (using the browser platform). I finally managed to get it working but had to perform several steps:
I had to force the isCordova check to false in order to be able to work with the library. Otherwise I received the following error (Could not get Cordova FileSystem: FileError {code: "Missing Command Error"}
)
I also needed to explicitly request file quota to be able to work with persistent storage. I did it by executing webkitStorageInfo.requestQuota(webkitStorageInfo.PERSISTENT, options.storageSize);
after the fs initialization.
In order to be able to correctly save downloaded binary files I had to set the respose type of the xhr fnction to blob (xhr.responseType='blob';
) and write(file,xhr.response).then(win,fail);
. In my tests it worked for every file type, but it could be changed to work with text, array, etc, depending on the options passed to the download function.
Finally, I had to launch chrome with the flags --disable-web-security --allow-file-access-from-files --user-data-dir=datadir
and manually allow file storage in the browser
I think it would be great to be able to configure the behaviour for the items 1,2,3 in the options of CordovaPromiseFS.
In any case, many thanks as cordova-promise-fs it is a wonderful library :)
When this command is run: await fs.download(sourceFile, destFile);
This error is returned, "Can't find variable: FileTransfer".
Does this library require cordova-plugin-file-transfer (deprecated)? If so, are there plans to switch to using XMLHttpRequest?
Thanks for creating this package!
Hi. I've been using cordova-app-loader
, with the usual code:
var fs = new CordovaPromiseFS({
Promise: Promise
});
var loader = new CordovaAppLoader({
fs: fs,
serverRoot: 'http://example.com/',
localRoot: 'app',
cacheBuster: true,
checkTimeout: 10000
});
This works fine. However, I added another CordovaPromiseFS instance to access another part of the filesystem and it broke the app loader. Now it tries to download files to file:///app/app.js
, which obviously won't work.
I ran eslint on cordova-promise-fs/index.js
and it seems that the following variables are undeclared and thus global:
CDV_URL_ROOT
CDV_INTERNAL_URL_ROOT
I think this is a causing the two instances to interfere. Is there a reason for this or can it just be fixed with a couple of var
s?
Thanks.
Hi,
Not sure if this is related to this particular module (maybe a lower level problem) but I'm experiencing file corruption on iOS.
I'm using fs.write() to replace the whole content of a file as a way to save data.
The function maybe called several times in a short period and no particular "waiting for previous write" mechanism has been implemented.
This is working perfectly well on Android but on iOS I sometime get a corrupted file with the whole content plus part off the content after, like if a write appended part of the data instead of replacing it.
Of course this ends in data corruption as the JSON content cannot be parsed anymore :(
I've tried to fs.remove() the file before fs.write() but fs.remove() sometimes fails (with Error code 1 even if mustExists is false)...
Questions:
Thanks a lot,
Julien
Hello. Is anyone able to tell me how you would use this with the cordova-plugin-camera
plugin to move the temp file to a permanent location?
I'm try to use this wrapper library to build a simple logger system to log system status of the app into a file locally.
But I got require is not defined
issue. If I just delete 'Promise: require('promiscuous')', I will work, but I've tried to log some thing into a created file. But when I try to read it immediately, it's just empty. By the fs.deviceready
method, I just got [object Promise]
.
Any suggestion? Thanks! - Su
I was going to do a pull request which would allow appending to existing files.
Should I add a bool parameter to write
which defaults to false or add a new method?
function write(path,blob,mimeType,append)
or
function append(path,blob,mimeType)
please. :)
Hi,
In the list
function, the promise is resolved before the subdirectories are read. So you only return the files read in the root directory.
I've tried to fix it but I've never been a "promise genius" and I can't get it to work properly
See this issue: markmarijnissen/cordova-app-loader#90
Many thanks for this library. Its just what I needed to get cross platform filesystem with promises working in Cordova/Electron(Chrome).
To get it working with using JSPM and ES6 I had to import like this:
import * as CordovaPromiseFS from 'cordova-promise-fs';
and to create an instance I had to:
let fs = CordovaPromiseFS.default({Promise: window.Promise});
I've yet to look into why that was required over the defaults but its working great now and I thought this might help others who are trying the same. Without defining the Promise
option I was getting an exception.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.