This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from ckeditor/t/1
Feature: Initial implementation. Closes #1.
- Loading branch information
Showing
17 changed files
with
1,788 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** | ||
* @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md. | ||
*/ | ||
|
||
/** | ||
* @module upload/filereader | ||
*/ | ||
|
||
/* globals window */ | ||
|
||
import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin'; | ||
import mix from '@ckeditor/ckeditor5-utils/src/mix'; | ||
|
||
/** | ||
* FileReader class - wrapper over native FileReader. | ||
*/ | ||
export default class FileReader { | ||
constructor() { | ||
const reader = new window.FileReader(); | ||
|
||
/** | ||
* Instance of native FileReader. | ||
* | ||
* @private | ||
* @member {FileReader} #_reader | ||
*/ | ||
this._reader = reader; | ||
|
||
/** | ||
* Number of bytes loaded. | ||
* | ||
* @readonly | ||
* @observable | ||
* @member {Number} #loaded | ||
*/ | ||
this.set( 'loaded', 0 ); | ||
|
||
reader.onprogress = evt => { | ||
this.loaded = evt.loaded; | ||
}; | ||
} | ||
|
||
/** | ||
* Returns error that occurred during file reading. | ||
* | ||
* @returns {Error} | ||
*/ | ||
get error() { | ||
return this._reader.error; | ||
} | ||
|
||
/** | ||
* Reads provided file. | ||
* | ||
* @param {File} file Native File object. | ||
* @returns {Promise} Returns a promise that will resolve with file's contents. Promise can be rejected in case of | ||
* error or when reading process is aborted. | ||
*/ | ||
read( file ) { | ||
const reader = this._reader; | ||
this.total = file.size; | ||
|
||
return new Promise( ( resolve, reject ) => { | ||
reader.onload = () => { | ||
resolve( reader.result ); | ||
}; | ||
|
||
reader.onerror = () => { | ||
reject( 'error' ); | ||
}; | ||
|
||
reader.onabort = () => { | ||
reject( 'aborted' ); | ||
}; | ||
|
||
this._reader.readAsDataURL( file ); | ||
} ); | ||
} | ||
|
||
/** | ||
* Aborts file reader. | ||
*/ | ||
abort() { | ||
this._reader.abort(); | ||
} | ||
} | ||
|
||
mix( FileReader, ObservableMixin ); |
Oops, something went wrong.