Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Merge c000a79 into 1963a89
Browse files Browse the repository at this point in the history
  • Loading branch information
paolobueno committed Oct 25, 2017
2 parents 1963a89 + c000a79 commit 9f92a58
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 61 deletions.
7 changes: 4 additions & 3 deletions client/filestore-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@
"branches": 80
},
"devDependencies": {
"@types/proxyquire": "^1.3.27",
"@types/cordova-plugin-file": "0.0.3",
"@types/cordova-plugin-file-transfer": "0.0.3",
"@types/mocha": "^2.2.41",
"proxyquire": "^1.8.0",
"@types/proxyquire": "^1.3.27",
"del-cli": "^1.0.0",
"mocha": "^3.4.2",
"nyc": "^11.0.1",
"proxyquire": "^1.8.0",
"source-map-support": "^0.4.15",
"ts-node": "^3.0.4",
"typescript": "^2.3.4"
}
}

94 changes: 36 additions & 58 deletions client/filestore-client/src/CordovaFileSupport.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,46 @@
import * as Bluebird from 'bluebird';

/**
* @param url
* @param fileId
*/
* @param url
* @param fileId
*/
export function downloadFileFromServer(url, fileId) {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {
console.info('file system open: ' + fs.name);
fs.root.getFile(fileId, { create: true, exclusive: false }, function(fileEntry) {
console.info('fileEntry is file? ' + fileEntry.isFile.toString());
const oReq = new XMLHttpRequest();
// Make sure you add the domain name to the Content-Security-Policy <meta> element.
oReq.open('GET', url + '/' + fileId, true);
// Define how you want the XHR data to come back
oReq.responseType = 'blob';
oReq.onload = function(oEvent) {
const blob = oReq.response; // Note: not oReq.responseText
if (blob) {
// Create a URL based on the blob, and set an <img> tag's src to it.
const objUrl = window.URL.createObjectURL(blob);
// document.getElementById('bot-img').src = url;
// Or read the data with a FileReader
// var reader = new FileReader();
// reader.addEventListener('loaded', function() {
// reader.result contains the contents of blob as text
// });
// reader.readAsText(blob);
} else {
console.error('we didnt get an XHR response!');
}
};
oReq.send(null);
}, function(err) { console.error('error getting file! ' + err); });
}, function(err) { console.error('error getting persistent fs! ' + err); });
}
return new Bluebird<FileSystem>((resolve, reject) => window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, resolve, reject))
.then(fs => new Bluebird<FileEntry>((resolve, reject) =>
fs.root.getFile(fileId, { create: true, exclusive: false }, resolve, reject)))
.then(fileEntry => {
return fetch(url).then(response => response.blob()).then(blob => window.URL.createObjectURL(blob));
});
};

/**
* Upload file using local file URI. Used for uploads on mobile devices (cordova based)
*/
export function uploadFile(url, fileURI) {
* Upload file using local file URI. Used for uploads on mobile devices (cordova based)
*/
export function uploadFile(url, fileURI): Promise<Response> {
if (arguments.length < 2) {
return Bluebird.reject('userId and fileURI parameters are required.');
} else {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {
console.info('file system open: ' + fs.name);
// TODO support metadata here
fs.root.getFile(fileURI, { create: true, exclusive: false }, function(fileEntry) {
fileEntry.file(function(file) {
const reader = new FileReader();
reader.onloadend = function() {
// Create a blob based on the FileReader 'result', which we asked to be retrieved as an ArrayBuffer
const blob = new Blob([new Uint8Array(this.result)], { type: 'image/png' });
const oReq = new XMLHttpRequest();
oReq.open('POST', url, true);
oReq.onload = function(oEvent) {
// all done!
};
// Pass the blob in to XHR's send method
oReq.send(blob);
};
// Read the file as an ArrayBuffer
reader.readAsArrayBuffer(file);
}, function(err) { console.error('error getting fileentry file!' + err); });
}, function(err) { console.error('error getting file! ' + err); });
}, function(err) { console.error('error getting persistent fs! ' + err); });
}

return new Bluebird<FileSystem>((resolve, reject) =>
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, resolve, reject))
.then(fs => new Bluebird<FileEntry>((resolve, reject) =>
fs.root.getFile(fileURI, { create: false, exclusive: false }, resolve, reject)))
.then(fileEntry => new Bluebird<Response>(function(resolve, reject) {
fileEntry.file(function(file) {
const reader = new FileReader();
reader.addEventListener("loadend", function() {
// Create a blob based on the FileReader 'result', which we asked to be retrieved as an ArrayBuffer
const blob = new Blob([new Uint8Array(this.result)], { type: 'image/jpg' });
const data = new FormData();
// data.append("other metadata", "Hello wtrocki!");
data.append("file", blob);
return fetch(url, {
method: 'post',
body: data
}).then(resolve).catch(reject);
});
// Read the file as an ArrayBuffer
reader.readAsArrayBuffer(file);
}, reject);
}));
}

0 comments on commit 9f92a58

Please sign in to comment.