Delete Folder From File system in phonegap for IOS [duplicate] - javascript

I'm trying to delete a directory and it's contents with PhoneGap on Android using:
deleteDirectory = function deleteDirectory(uri) {
uri = uri.substring(0, uri.lastIndexOf('/'));
return $.Deferred(function (def) {
fileSystem.root.getDirectory(uri, {
create: false
}, function (directory) {
directory.removeRecursively();
def.resolve();
}, function (error) {
resolveError("Error deleting directory: ", error, def);
});
}).promise();
}
with the following error: File No Modification Allowed Error
I've confirmed this permission is set:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Where else should I be looking?

I have done it with this approach:
function ClearDirectory() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);
function fail(evt) {
alert("FILE SYSTEM FAILURE" + evt.target.error.code);
}
function onFileSystemSuccess(fileSystem) {
fileSystem.root.getDirectory(
"yours/dir/ect/ory",
{create : true, exclusive : false},
function(entry) {
entry.removeRecursively(function() {
console.log("Remove Recursively Succeeded");
}, fail);
}, fail);
}
}

From this answer:
I'd suggest using resolveLocalFileSystemURL if you want to access
locations under cordova.file.* (eg cordova.file.dataDirectory), which
is most of the time (if not always), and use requestFileSystem if you
need to have access to the root of the filesystem.
This also saves some lines of code and is more readable:
deleteFolder(fileName: string) {
const uri = `${cordova.file.dataDirectory}${fileName}`;
window.resolveLocalFileSystemURL(uri, (dirEntry: DirectoryEntry) => {
dirEntry.removeRecursively(
() => console.log('successfully deleted the folder and its content'),
e => console.error('there was an error deleting the directory', e.toString())
)
});
}
And here an awaitable version:
deleteFolder(fileName: string): Promise<void> {
const promise = new Promise<void>((resolve, reject) => {
const uri = `${cordova.file.dataDirectory}${fileName}`;
window.resolveLocalFileSystemURL(uri, (dirEntry: DirectoryEntry) => {
dirEntry.removeRecursively(() => resolve(), e => reject(e));
}, e => reject(e));
});
return promise;
}

Related

Showing file system tab in my Chrome devtools

I am not able to show the FileSystem tab in my dev tool
I am using this code to add content to a file but i am not able to view the file in my dev tools
also i tried the following but didn't work: filesystem:https://localhost:3000/persistent/log.txt
export class ErrorLoging {
static init = () => {
var mself = this;
navigator.webkitPersistentStorage.requestQuota(1024*1024, function() {
window.webkitRequestFileSystem(window.PERSISTENT , 1024*1024, mself.onFs);
})
}
static onFs = (fs) => {
fs.root.getFile('/log.txt', { create: true }, (fileEntry) => {
// Create a FileWriter object for our FileEntry.
fileEntry.createWriter((fileWriter) => {
fileWriter.onwrite = (e) => {
console.log('Write completed.');
};
fileWriter.onerror = (e) => {
console.log('Write failed: ' + e.toString());
};
var blob = new Blob(["Lorem Ipsum"], {type: "text/plain"});
fileWriter.write(blob);
});
});
}
}

Download file from URL to server including login header in NodeJS

I have a script that will download a file from a third party URL, to a local folder on my server.
function download(url, dest) {
const fs = require("fs");
const http = require("https");
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(dest, { flags: "wx" });
const request = http.get(url, response => {
if (response.statusCode === 200) {
response.pipe(file);
} else {
file.close();
fs.unlink(dest, () => {}); // Delete temp file
reject(`Server responded with ${response.statusCode}: ${response.statusMessage}`);
}
});
request.on("error", err => {
file.close();
fs.unlink(dest, () => {}); // Delete temp file
reject(err.message);
});
file.on("finish", () => {
resolve();
});
file.on("error", err => {
file.close();
if (err.code === "EEXIST") {
reject("File already exists");
} else {
fs.unlink(dest, () => {}); // Delete temp file
reject(err.message);
}
});
});
}
What makes this case different is that this script only downloads from a URL it has public access to. But now I'm trying to download a file from a link that is protected by 2 fields, username and password.
How can I easily implement a header that will include the "username" and "password" into this script when accessing the download URL?

callback is not a function - castv2

I'm following this http://siglerdev.us/blog/2021/02/26/google-home-message-broadcast-system-node-js/31 which uses this library castv2-client to send messages to my google home. It works. I get the messages no problem, but the code throws
C:\Users\Phil\Documents\google home\node_modules\castv2-client\lib\controllers\receiver.js:72
callback(null, response.status.volume);
^
TypeError: callback is not a function
at C:\Users\Phil\Documents\google home\node_modules\castv2-client\lib\controllers\receiver.js:72:5 ver.js:72
at fn.onmessage (C:\Users\Phil\Documents\google home\node_modules\castv2-client\lib\controllers\request-response.js:27:7)
at fn.emit (events.js:203:15)
at Channel.onmessage (C:\Users\Phil\Documents\google home\node_modules\castv2-client\lib\controllers\controller.js:16:10) s\receiver.js:72:5
at Channel.emit (events.js:198:13) lib\controllers\request-response.js:27:7)
at Client.onmessage (C:\Users\Phil\Documents\google home\node_modules\castv2\lib\channel.js:23:10) ient\lib\controllers\controller.js:16:10)
at Client.emit (events.js:203:15)
at PacketStreamWrapper.onpacket (C:\Users\Phil\Documents\google home\node_module\channel.js:23:10)s\castv2\lib\client.js:81:10)
at PacketStreamWrapper.emit (events.js:198:13) s\castv2\lib\client.js:81:10)
at TLSSocket.<anonymous> (C:\Users\Phil\Documents\google home\node_modules\castv2\lib\packet-stream-wrapper.js:28:16)
What's wrong with the code that is throwing this AND/OR how can I fix it so it's either more graceful in catching error and doesn't throw since the message still delivers to google home or fix it to not throw this at all?
I appreciate any help!
I believe it's here in the castv2-client library that it's referencing, but I haven't been able to make it happy.
ReceiverController.prototype.launch = function(appId, callback) {
this.request({ type: 'LAUNCH', appId: appId }, function(err, response) {
if(err) return callback(err);
if(response.type === 'LAUNCH_ERROR') {
return callback(new Error('Launch failed. Reason: ' + response.reason));
}
callback(null, response.status.applications || []);
});
};
my code
var Client = require('castv2-client').Client;
var DefaultMediaReceiver = require('castv2-client').DefaultMediaReceiver;
const googleTTS = require('google-tts-api');
var App = {
playin: false,
DeviceIp: "",
Player: null,
GoogleHome: function (host, url) {
var client = new Client();
client.connect(host, function () {
client.launch(DefaultMediaReceiver, function (err, player) {
client.setVolume({ level: 1 });
var media = {
contentId: url,
contentType: 'audio/mp3',
streamType: 'BUFFERED'
};
App.Player = player;
App.Player.load(media, { autoplay: true }, function (err, status) {
App.Player.on('status', function (status) {
if (status.playerState === "IDLE" && App.playin === false) {
client.close();
}
});
});
});
});
client.on('error', function (err) {
console.log('Error: %s', err.message);
client.close();
});
},
run: function (ip, text) {
App.DeviceIp = ip;
const url = googleTTS.getAudioUrl(text, {
lang: 'en-US',
slow: false,
host: 'https://translate.google.com',
});
App.GoogleHome(App.DeviceIp, url, function (res) {
console.log(res);
})
},
broadcast: function(text){
const ips = '192.168.0.15'.split(","); //From config, 192.168.68.105,192.168.68.107,192.168.68.124
for (var s of ips){
App.run(s, text);
}
}
}
App.broadcast("Broadcasted to all of the devices"); //Only works if you did step 4.5
The error you reported:
C:\Users\Phil\Documents\google home\node_modules\castv2-client\lib\controllers\receiver.js:72
callback(null, response.status.volume);
^
TypeError: callback is not a function
at C:\Users\Phil\Documents\google home\node_modules\castv2-client\lib\controllers\receiver.js:72:5
seems to be related to the invocation of the method setVolume in your client:
client.setVolume({ level: 1 });
Please, consider review the source code of receiver.js in the castv2-client library:
ReceiverController.prototype.setVolume = function(options, callback) {
var data = {
type: 'SET_VOLUME',
volume: options // either `{ level: 0.5 }` or `{ muted: true }`
};
this.request(data, function(err, response) {
if(err) return callback(err);
callback(null, response.status.volume);
});
};
The library is claiming because you aren't providing a proper callback when invoking that function.
I have never used the library but probably providing something similar to the following callback could be of help:
client.setVolume({ level: 1 }, function(err, volume) {
if (err) {
// Handle error as appropriate
console.log('Error on setVolume:', err);
} else {
console.log('Volume:', volume)
}
});
Your final code would look like this:
var Client = require('castv2-client').Client;
var DefaultMediaReceiver = require('castv2-client').DefaultMediaReceiver;
const googleTTS = require('google-tts-api');
var App = {
playin: false,
DeviceIp: "",
Player: null,
GoogleHome: function (host, url) {
var client = new Client();
client.connect(host, function () {
client.launch(DefaultMediaReceiver, function (err, player) {
client.setVolume({ level: 1 }, function(err, volume) {
if (err) {
// Handle error as appropriate
console.log('Error on setVolume:', err);
} else {
console.log('Volume:', volume)
}
});
var media = {
contentId: url,
contentType: 'audio/mp3',
streamType: 'BUFFERED'
};
App.Player = player;
App.Player.load(media, { autoplay: true }, function (err, status) {
App.Player.on('status', function (status) {
if (status.playerState === "IDLE" && App.playin === false) {
client.close();
}
});
});
});
});
client.on('error', function (err) {
console.log('Error: %s', err.message);
client.close();
});
},
run: function (ip, text) {
App.DeviceIp = ip;
const url = googleTTS.getAudioUrl(text, {
lang: 'en-US',
slow: false,
host: 'https://translate.google.com',
});
App.GoogleHome(App.DeviceIp, url, function (res) {
console.log(res);
})
},
broadcast: function(text){
const ips = '192.168.0.15'.split(","); //From config, 192.168.68.105,192.168.68.107,192.168.68.124
for (var s of ips){
App.run(s, text);
}
}
}
App.broadcast("Broadcasted to all of the devices"); //Only works if you did step 4.5

Ending imap-simple connection within recursion

I currently have a script that checks for an incoming email (in a mailbox) every 30 seconds, using a recursion.
The package I'm using for this testing is imap-simple.
The below script currently does this as required;
var imaps = require('imap-simple');
const { connect } = require('net');
var config = {
imap: {
user: 'qatestspecialist#outlook.com',
password: 'specialistQa',
host: 'imap-mail.outlook.com',
port: 993,
tls: true,
authTimeout: 30000
}
};
module.exports = {
'delete any existing emails...': function () {
imaps.connect(config).then(function (connection) {
connection.openBox('INBOX').then(function () {
var searchCriteria = ['ALL'];
var fetchOptions = { bodies: ['TEXT'], struct: true };
return connection.search(searchCriteria, fetchOptions);
//Loop over each message
}).then(function (messages) {
let taskList = messages.map(function (message) {
return new Promise((res, rej) => {
var parts = imaps.getParts(message.attributes.struct);
parts.map(function (part) {
return connection.getPartData(message, part)
.then(function (partData) {
//Display e-mail body
if (part.disposition == null && part.encoding != "base64"){
console.log(partData);
}
//Mark message for deletion
connection.addFlags(message.attributes.uid, "\Deleted", (err) => {
if (err){
console.log('Problem marking message for deletion');
rej(err);
}
res(); //Final resolve
});
});
});
});
});
return Promise.all(taskList).then(() => {
connection.imap.closeBox(true, (err) => { //Pass in false to avoid delete-flagged messages being removed
if (err){
console.log(err);
}
});
connection.end();
});
});
});
},
'send email to seller and wait for mailbox notification': function (browser) {
// script to send an email to the mailbox...
},
'get new email info': function(browser) {
const createPromise = ms => new Promise((resolve, reject) => {
setTimeout(() => resolve(ms), ms)
});
function findUnseenEmails(connection) {
return connection.openBox('INBOX').then(function () {
var searchCriteria = ['UNSEEN'];
var fetchOptions = {
bodies: ['HEADER', 'TEXT'],
markSeen: false
};
return connection.search(searchCriteria, fetchOptions).then(function (results) {
var subjects = results.map(function (res) {
return res.parts.filter(function (part) {
return part.which === 'HEADER';
})
[0].body.subject[0];
});
return subjects.length > 0 ? subjects : createPromise(30000).then(function() { return findUnseenEmails(connection);
});
});
});
}
imaps.connect(config).then(function (connection) {
return findUnseenEmails(connection)
})
.then((subjects) => console.log(JSON.stringify(subjects)));
},
'Closing the browser': function (browser) {
browser.browserEnd();
}
};
This waits for an email and then displays the email 'header'.
However, the imap connection does not close, and stays open which is stopping my test suite from completing as the associated test never actually finishes.
I've tried adding the imap-simple command connection.end() in several places after the
imaps.connect(config).then(function (connection) {
return findUnseenEmails(connection)
})
part of the script, but it doesn't work.
So I'm just wondering if anyone knows where I should be adding this connection.end() command in order for the connection to be closed once an email has been received?
Any help would be greatly appreciated.
This has now been resolved in another post, using the following;
if (subjects.length > 0) {
connection.end();
return subjects;
} else {
return createPromise(5000).then(function() { return findUnseenEmails(connection)});
}

Cordova/Javascript: method as callback function

Moin,
I feel a little stupid. I try to convert the following function into methods of an object:
function listDir(path){
window.resolveLocalFileSystemURL(path,
function (fileSystem) {
var reader = fileSystem.createReader();
reader.readEntries(
function (entries) {
console.log(entries);
},
function (err) {
console.log(err);
}
);
}, function (err) {
console.log(err);
}
);
}
Source: Cordova list all files from application directory (WWW)
But any way I try it does not work. I don't know how to use the methods as callback function, especially how to set the arguments of the function. With my code I expect the output:
debug: listdir
debug: filesystem
debug: entries
[Array or something]
But I just get:
debug: listdir
debug: filesystem
This ist my Code:
function Filelist() {}
Filelist.prototype = {
methodErr: function (err) {
console.log('debug: error');
console.log(err);
},
methodEntries: function (entries) {
console.log('debug: entries');
console.log(entries);
},
methodFilesystem: function (fileSystem) {
console.log('debug: filesystem');
var reader = fileSystem.createReader();
reader.readEntries(this.methodEntries, this.methodErr);
},
methodListDir: function (path) {
console.log('debug: listdir');
window.resolveLocalFileSystemURL(
path,
this.methodFilesystem,
this.methodErr
);
}
}
fl = new Filelist();
$('.klicken').click(function () {
fl.methodListDir(cordova.file.applicationDirectory);
});
Where is the bug?
Thanks in advance!
Jan
I've got the solution:
I have to bind this in the callback method:
...
reader.readEntries(this.methodEntries.bind(this), this.methodErr.bind(this));
...
window.resolveLocalFileSystemURL(
path,
this.methodFilesystem.bind(this),
this.methodErr.bind(this)
);
...
Now it works :)

Categories