is file system is running on diffrent thread in javascript - javascript

I hvae used file system of javascript, and what i noticed is that the file system creates the file after the read is been performed, but physically call to readFile is after the call to writeFile.
readFile
var readFile = function () {
console.log("readfile" + new Date());
var that = this;
var result = "";
var successCallback = function (fs) {
console.log("Inside readFile()");
fs.root.getFile('rosters.txt', {create: false, exclusive: false},
function (fileEntry) {
fileEntry.file(function (file) {
var reader = new FileReader();
reader.onloadend = function(e) {
console.log(this.result);
result = this.result;
that.showRosters(this.result);
}; //onloadend() ENDS HERE
reader.readAsText(file);
}, //fileEntry success callback ENDS HERE
function () { console.log("Error occurred during reading file");
result = "Error"; } //error callback for fileEntry.file
); //fileEntry.file ENDS HERE
}, //fs.roor.getFile success callback ENDS HERE
function () { console.log("File Not Found"); } ); //error callback fs.root
}
var errorCallback = function (e) {
console.log(e.toString());
}
this.EMarshal.reqFs.call(window,this.EMarshal.persistent, 5*1024*1024 /*5MB*/, successCallback, errorCallback);
return result
}
writeFile
var writefile = function (rosters) {
console.log("writeFile" + new Date());
var that = this;
console.log("Rosters" + rosters);
var successCallback = function (fs) {
console.log('Opened file system: ' + fs.name);
fs.root.getFile('rosters.txt', {create: true, exclusive: false},
function(fileEntry) {
console.log("File Created");
fileEntry.createWriter( function(writer) {
that.gotFileWriter(writer,rosters);
console.log("write");
}
,function() { console.log("Failed");
this.EMarshal.fail() }
);
},
function (e) {
console.log(e.toString());
} );
}
var errorCallback = function (e) {
console.log(e.toString);
};
console.log('Error: ' + msg);
}
this.EMarshal.reqFs.call(window,this.EMarshal.persistent, 5*1024*1024 /*5MB*/, successCallback, errorCallback);
}
for gotFileWriter
var gotFileWriter = function (writer,roster) {
writer.seek(writer.length);
writer.truncate(0);
roster = "tripid:" + this.tripid + roster;
writer.write(roster);
}
I tried to get the time, then also readFile is running before writeFile.
Is javascript is running 2 threads here ?
If anybody can explain why this happening it will be helpfull.

Try to check for the name property in the js-module. I have this problem while using the same plugin, it should me same as the Plugin name.
If it doesn't work switch to other one PhoneGap-SMS Plugin. This will be useful.
If you will read this document about the plugins in cordova you will get the idea, how these plugins are built. Cordova Plugin Documentation

Related

Unexpected Parsing Error On AWS Lambda JS

I think this is a syntax error but I'm having trouble finding documentation. I keep getting 'Parsing Error: Unexpected Token {". It says its to do with the 'YesIntent', but won't give specifics. I'm new to JS, but I can't see what could be the problem. Every '{' has a matching '}'.
Any insights would be appreciated. Thank you.
const Alexa = require("alexa-sdk");
const appId = ''; //'';
exports.handler = function(event, context, callback) {
const alexa = Alexa.handler(event, context);
alexa.appId = appId;
alexa.registerHandlers(handlers);
alexa.execute();
};
const handlers = {
'LaunchRequest': function() {
this.emit('YesIntent');
},
'YesIntent': function() {
getData(callback(title) {
this.response.speak('Here are your data ' + title);
this.emit(':responseReady');
}),
};
function getData() {
var ddb = new AWS.DynamoDB.DocumentClient({
region: 'us-west-1'
});
var params = {
TableName: 'WallyFlow_StartTime',
Key: 'TimeStamp',
};
ddb.get(params, function(err, data) {
if (err) {
callback(err, null);
} else {
title = data.Item.title;
}
});
}
Sorry, in this style you need more braces :) Updated to:
'YesIntent': function () {
getData( {
callback(title) {
this.response.speak('Here are your data ' + title);
this.emit(':responseReady');
}})
}};
I suspect it should be something like this. callback should be the name of the parameter to the getData() function, not something you call in the argument. The argument to getData() should be a function.
And getData() should call the callback function in the non-error case as well as the error case.
You also need an extra } to end the handlers object, and the end of the statement that calls getData() should be ;, not ,.
const handlers = {
'LaunchRequest': function() {
this.emit('YesIntent');
},
'YesIntent': function() {
getData(function(title) {
this.response.speak('Here are your data ' + title);
this.emit(':responseReady');
});
}
};
function getData(callback) {
var ddb = new AWS.DynamoDB.DocumentClient({
region: 'us-west-1'
});
var params = {
TableName: 'WallyFlow_StartTime',
Key: 'TimeStamp',
};
ddb.get(params, function(err, data) {
if (err) {
callback(err, null);
} else {
title = data.Item.title;
callback(title);
}
});
}

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 :)

How to download any file from Byte Array in Cordova application?

I have a cordova - ionic application.
I want to download a file from webservice and the file may be any type(JPG,PDG,DOCX etc). I cannot download the file from direct URL. So the app is taking byte array of the file from Webservice.
Anybody know how to download the file in Mobile from the Byte Array. Please help me.
you can use the
cordova-plugin-file and the
cordova-plugin-file-opener2 plugin.
https://github.com/apache/cordova-plugin-file
https://github.com/pwlin/cordova-plugin-file-opener2
in function with the webservice your code should look like that:
var bytes = new Uint8Array(data.d);
app.writePDFToFile(fileName.split(fileName.split, bytes);
and here is teh function forcing the download:
writePDFToFile: function (fileName, data) {
try {
window.resolveLocalFileSystemURL(cordova.file.externalApplicationStorageDirectory, function (directoryEntry) {
directoryEntry.getFile(fileName, { create: true }, function (fileEntry) {
fileEntry.createWriter(function (fileWriter) {
fileWriter.onwriteend = function (e) {
//window.open(cordova.file.externalApplicationStorageDirectory + fileName, '_system', 'location=yes');
cordova.plugins.fileOpener2.open(cordova.file.externalApplicationStorageDirectory + fileName, 'application/pdf',
{
error: function (e) {
console.log('Error status: ' + e.status + ' - Error message: ' + e.message);
},
success: function () {
console.log('file opened successfully');
}
}
);
};
fileWriter.onerror = function (e) {
alert(e);
};
var blob = new Blob([data], { type: 'application/pdf' });
fileWriter.write(blob);
}, function onerror(e) {
alert(e);
});
}, function onerror(e) {
alert(e);
});
}, function onerror(e) {
alert(e);
});
} catch (e) {
alert(e);
}
},
Hope this will help!

Cordova file upload

I want to upload a video to YouTube from my phone device storage. However when I upload the file it comes through as blank. When I use the same upload code but with a web file, it works. Wondering where I am going wrong!
Method one
everything uploads correctly and the video plays on YouTube.
loadWebFile('assets/intro.mpg');
function loadWebFile(url) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'blob';
xhr.onload = function (e) {
uploadFile(xhr.response); // type: Blob
};
xhr.onerror = function (e) {
console.log('loadWebFile.onerror', e);
};
xhr.send();
};
Method two
The video title and description appears on YouTube, but the video is blank. I'm definitely passing through a valid file.
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
if (window.webkitStorageInfo) {
window.webkitStorageInfo.requestQuota(access, 1024 * 1024, function (bytes) {
if (window.requestFileSystem) {
window.requestFileSystem(access, bytes, function (filesystem) {
loadFile('/Movies/intro.mpg');
}, me.onError);
} else {
window.alert('requestFileSystem not supported');
}
}, me.onError);
} else {
window.alert('webkitStorageInfo not supported');
}
// this sends an empty video to YouTube
function loadFile(path) {
filesystem.root.getFile(path, null, function (fileEntry) {
fileEntry.file(function (file) {
uploadFile(file); // type: File
});
}, function (e) {
console.log('loadFile.error', e);
});
}
Both methods share the same upload function:
// uploads using the YouTube script
// https://github.com/youtube/api-samples/blob/master/javascript/cors_upload.js
function uploadFile(file) {
var metadata = {
snippet: {
title: 'Video title',
description: 'Video description',
tags: 'Video tags',
categoryId: 22
},
status: {
privacyStatus: 'unlisted'
}
};
var uploader = new MediaUploader({
baseUrl: 'https://www.googleapis.com/upload/youtube/v3/videos',
file: file,
token: accessToken,
metadata: metadata,
params: {
part: Object.keys(metadata).join(',')
},
onError: function (e) {
console.log('onError', JSON.parse(e));
},
onProgress: function (e) {
console.log('onProgress', e);
},
onComplete: function (e) {
console.log('onComplete', JSON.parse(e));
}
});
uploader.upload();
};
I've have an example project with some of the code (minus the upload script) here:
https://github.com/kmturley/cordova-files
Here another working solution. I tested right now and it does work. You need standard cordova-plugin-file
function uploadFileToServer (fileUri, fileName, remoteUrl, callback) {
window.resolveLocalFileSystemURL(fileUri, function (fileEntry) {
fileEntry.file(function (file) {
var reader = new FileReader()
reader.onloadend = function () {
var blob = new Blob([new Uint8Array(this.result)], { type: 'application/octet-stream' })
var fd = new FormData()
fd.append('file', blob, fileName)
var xhr = new XMLHttpRequest()
xhr.open('POST', remoteUrl, true)
xhr.onload = function () {
if (xhr.status === 200) {
if (typeof callback === 'function') { callback() }
} else {
if (typeof callback === 'function') { callback(xhr.status) }
}
}
xhr.onerror = function (err) {
if (typeof callback === 'function') { callback(err) }
}
xhr.send(fd)
}
reader.readAsArrayBuffer(file)
}, function (err) {
if (typeof callback === 'function') { callback(err) }
})
})
}
You call it:
uploadFileToServer('file:///storage/emulated/0/Android/data/myfile.jpg',
'myfile.jpg',
'https://example.com/upload_url',
(err) => {
if (err) {
console.error('Error uploading file', err)
} else {
console.log('Upload done it with success')
}
})
The oficial file-transfer plugin is deprecated and when I'm writing this, its test script fails.
This made me use a pure javascript approach and it does work
function uploadFile (localPath, fileName, remoteUrl, callback) {
// loads local file with http GET request
var xhrLocal = new XMLHttpRequest()
xhrLocal.open('get', localPath)
xhrLocal.responseType = 'blob'
xhrLocal.onerror = () => {
callback(Error('An error ocurred getting localpath on' + localPath))
}
xhrLocal.onload = () => {
// when data is loaded creates a file reader to read data
var fr = new FileReader()
fr.onload = function (e) {
// fetch the data and accept the blob
console.log(e)
fetch(e.target.result)
.then(res => res.blob())
.then((res) => {
// now creates another http post request to upload the file
var formData = new FormData()
formData.append('imagefile', res, fileName)
// post form data
const xhrRemote = new XMLHttpRequest()
xhrRemote.responseType = 'json'
// log response
xhrRemote.onerror = () => {
callback(Error('An error ocurred uploading the file to ' + remoteUrl))
}
xhrRemote.onload = () => {
if (typeof callback === 'function') {
callback(null, 'File uploaded successful, ' + xhrRemote.response)
}
}
// create and send the reqeust
xhrRemote.open('POST', remoteUrl)
xhrRemote.send(formData)
})
}
fr.readAsDataURL(xhrLocal.response) // async call
}
xhrLocal.send()
}
Now just call it something like this
uploadFile('file:///storage/emulated/0/Android/data/myfile.jpg',
'myfile.jpg',
'https://example.com/upload_url',
(err, res) => {
if (err) {
console.error(err)
} else {
console.log(res)
}
})
So to upload files I realised for:
web files use a CORS upload:
https://github.com/youtube/api-samples/blob/master/javascript/cors_upload.js
local device files us the cordova file transfer plugin:
https://github.com/apache/cordova-plugin-file-transfer
The code i'm now using for local files which uploads the file and sets correct metadata:
function uploadVideo(fileURL) {
var options = new FileUploadOptions();
options.fileKey = 'file';
options.fileName = fileURL.substr(fileURL.lastIndexOf('/') + 1);
options.mimeType = 'video/mpg';
options.chunkedMode = false;
options.headers = {
Authorization: 'Bearer ' + accessToken
};
options.params = {
"": {
snippet: {
title: 'Video title',
description: 'Video description',
tags: 'Video tags',
categoryId: 22
},
status: {
privacyStatus: 'unlisted'
}
}
};
var ft = new FileTransfer();
ft.upload(fileURL, 'https://www.googleapis.com/upload/youtube/v3/videos?part=snippet,status', function (data) {
console.log('upload success', data);
}, function (e) {
console.log('upload error', e);
}, options, true);
ft.onprogress = function (progressEvent) {
console.log('onprogress: ' + ((progressEvent.loaded / progressEvent.total) * 100) + '%');
};
}
And I also had to modify the plugin to allow the metadata to be passed through to YouTube using a single request:
FileTransfer.java
lines 374 - 376
beforeData.append("Content-Disposition: form-data; name=\"").append(key.toString()).append("\";");
beforeData.append(" filename=\"").append("file.json").append('"').append(LINE_END);
beforeData.append("Content-Type: ").append("application/json").append(LINE_END).append(LINE_END);
If you do modify the plugin, remember cordova caches this code. I use this command to force it to update the plugin:
cordova platform remove android; cordova platform add android;

Sending images to the client with nodejs and socket.io

with the following code I'm able to send an Image to the client with socket.io, but I want also to send the size as a second argument. I would like to understand why the argument "size" is getting always undefined when I try to pass it to the socket.emit function. socket.emit('firstChunkSent', data, size). Can you pls fix it ?
var io = require('../server').io
io.sockets.on('connection', function (socket) {
socket.on('imageRequest', function () {
getTheNewImage(function(data,size){
socket.emit('firstChunkSent', data, size)
});
});
});
function getTheNewImage(callback){
var filename = 'image.gif';
var base64FileSize = fs.stat(filename,function(err,stats){
if (err) { throw err; }
return stats.size
});
var readable = fs.createReadStream(filename, { encoding: 'base64' });
readable.on('readable', function() {
var getImageData = function(){
while (null !== (base64ImageData = readable.read())) {
return base64ImageData
}
}
callback(getImageData(),base64FileSize)
});
readable.on('end', function() {
console.log('there will be no more data.')
});
readable.on('error', function(err) {
console.log('here is the error: '+err)
readable.end(err);
});
return stats.size
Need to use the callback
function getTheNewImage(callback){
var filename = 'image.gif';
var base64FileSize = fs.stat(filename,function(err,stats){
if (err) return callback(new Error(err));
callback(null, stats.size)
});
getTheNewImage(function(data,size)
First param will be the error, not data

Categories