Skip to content

Commit

Permalink
add source attribute for apps that would like to collect analytics on…
Browse files Browse the repository at this point in the history
… where files came from
  • Loading branch information
tim-evans committed Feb 22, 2017
1 parent b330479 commit 727f1ed
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 9 deletions.
6 changes: 3 additions & 3 deletions addon/components/file-dropzone/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export default Ember.Component.extend({

if (canvas.toBlob) {
canvas.toBlob((blob) => {
let [file] = get(this, 'queue')._addFiles([blob]);
let [file] = get(this, 'queue')._addFiles([blob], 'web');
set(file, 'name', filename);
});
} else {
Expand All @@ -175,7 +175,7 @@ export default Ember.Component.extend({
}
let blob = new Blob([arr], { type: 'image/png' });
blob.name = filename;
let [file] = get(this, 'queue')._addFiles([blob]);
let [file] = get(this, 'queue')._addFiles([blob], 'web');
set(file, 'name', filename);
}
};
Expand All @@ -196,7 +196,7 @@ export default Ember.Component.extend({

// Add file(s) to upload queue.
set(this, 'active', false);
get(this, 'queue')._addFiles(get(this[DATA_TRANSFER], 'files'));
get(this, 'queue')._addFiles(get(this[DATA_TRANSFER], 'files'), 'drag-and-drop');
this[DATA_TRANSFER] = null;
evt.preventDefault();
evt.stopPropagation();
Expand Down
2 changes: 1 addition & 1 deletion addon/components/file-upload/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default Ember.Component.extend({

actions: {
change(files) {
get(this, 'queue')._addFiles(files);
get(this, 'queue')._addFiles(files, 'browse');
}
}
});
60 changes: 57 additions & 3 deletions addon/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,46 @@ export default Ember.Object.extend({
*/
state: 'queued',

/**
The source of the file. This is useful
for applications that want to gather
analytics about how users upload their
content.
This property can be one of the following:
- `browse`
- `drag-and-drop`
- `web`
- `data-url`
- `blob`
`browse` is the source when the file is created
using the native file picker.
`drag-and-drop` is the source when the file was
created using drag and drop from their desktop.
`web` is the source when the file was created
by dragging the file from another webpage.
`data-url` is the source when the file is created
from a data URL using the `fromDataURL` method for
files. This usually means that the file was created
manually by the developer on behalf of the user.
`blob` is the source when the file is created
from a blob using the `fromBlob` method for
files. This usually means that the file was created
manually by the developer.
@property source
@type {String}
@default ''
@readonly
*/
source: '',

upload(url, opts) {
if (['queued', 'failed', 'timed_out'].indexOf(get(this, 'state')) === -1) {
Ember.assert(`The file ${this.id} is in the state "${get(this, 'state')}" and cannot be requeued.`);
Expand Down Expand Up @@ -248,20 +288,34 @@ export default Ember.Object.extend({
@method fromBlob
@param {Blob} blob The blob to create the file from.
@param {String} [source] The source that created the blob.
@return {File} A file object
*/
fromBlob(blob) {
fromBlob(blob, source='blob') {
let file = this.create();
Object.defineProperty(file, 'blob', {
writeable: false,
enumerable: false,
value: blob
});
Object.defineProperty(file, 'source', {
writeable: false,
value: source
});

return file;
},

fromDataURL(dataURL) {
/**
Creates a file object that can be read or uploaded to a
server from a data URL.
@method fromDataURL
@param {String} dataURL The data URL to create the file from.
@param {String} [source] The source of the data URL.
@return {File} A file object
*/
fromDataURL(dataURL, source='data-url') {
let [typeInfo, base64String] = dataURL.split(',');
let mimeType = typeInfo.match(/:(.*?);/)[1];

Expand All @@ -274,6 +328,6 @@ export default Ember.Object.extend({

let blob = new Blob([binaryData], { type: mimeType });

return this.fromBlob(blob);
return this.fromBlob(blob, source);
}
});
4 changes: 2 additions & 2 deletions addon/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ export default Ember.Object.extend({
@method _addFiles
@param {FileList} fileList The event triggered from the DOM that contains a list of files
*/
_addFiles(fileList) {
_addFiles(fileList, source) {
let onfileadd = get(this, 'onfileadd');
let files = [];

for (let i = 0, len = fileList.length || fileList.size; i < len; i++) {
let fileBlob = fileList.item ? fileList.item(i) : fileList[i];
if (fileBlob instanceof Blob) {
let file = File.fromBlob(fileBlob);
let file = File.fromBlob(fileBlob, source);

files.push(file);
this.push(file);
Expand Down

0 comments on commit 727f1ed

Please sign in to comment.