Created
January 28, 2019 14:32
-
-
Save cwhittl/487d57c43f30bcfea2de0af3ff684e1a to your computer and use it in GitHub Desktop.
Media Uploader using adopted-ember-addons/ember-file-upload
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Component from '@ember/component'; | |
import { get, set, computed } from '@ember/object'; | |
import config from 'lucy/config/environment'; | |
import { inject as service } from '@ember/service'; | |
import { task } from 'ember-concurrency'; | |
import { A } from '@ember/array'; | |
const MAX_CONCURRENT_UPLOADS = 5; | |
export default Component.extend({ | |
classNames: ['rc-media-uploader'], | |
session: service(), | |
store: service(), | |
notify: service(), | |
errorMessage: null, | |
acceptableFiles: A(['*']), | |
accept: computed(function() { | |
return get(this, 'acceptableFiles').join(','); | |
}), | |
uploadMedia: task(function* (file) { | |
const filename = get(file, 'name'); | |
const [mediaType] = file.type.split('/'); | |
try { | |
const idToken = get(this, 'session.data.authenticated.idToken'); | |
file.readAsDataURL().then(url => this.addFileURL(file, url)); | |
const { body } = yield file.upload(`${config.APP.API_ENDPOINT}/media/`, { | |
headers: { | |
Authorization: `JWT ${idToken}`, | |
}, | |
fileKey: 'media_file', | |
data: { | |
name: filename, | |
mediaType, | |
}, | |
}); | |
this.store.pushPayload({ medias: [body] }); | |
get(this, 'onMediaAdd')(body.id); | |
get(this, 'notify').success(`${body.name} has uploaded`); | |
} catch (e) { | |
console.log(e); //eslint-disable-line | |
get(this, 'notify').error('There was an error uploading'); | |
} | |
}).maxConcurrency(MAX_CONCURRENT_UPLOADS).enqueue(), | |
addFileURL(file, url) { | |
set(this, file.id, url); | |
}, | |
onMediaAdd: () => { | |
// Do nothing | |
}, | |
actions: { | |
uploadMedia(file) { | |
get(this, 'uploadMedia').perform(file); | |
}, | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment