-
Notifications
You must be signed in to change notification settings - Fork 559
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add SimplenoteImporter * Cap file size at 1 MB Will tweak later * Fix tests
- Loading branch information
Showing
5 changed files
with
79 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
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,59 @@ | ||
import { EventEmitter } from 'events'; | ||
import CoreImporter from '../'; | ||
import { endsWith, isEmpty } from 'lodash'; | ||
|
||
class SimplenoteImporter extends EventEmitter { | ||
constructor({ noteBucket, tagBucket, options }) { | ||
super(); | ||
this.noteBucket = noteBucket; | ||
this.tagBucket = tagBucket; | ||
this.options = options; | ||
} | ||
|
||
importNotes = filesArray => { | ||
const coreImporter = new CoreImporter({ | ||
noteBucket: this.noteBucket, | ||
tagBucket: this.tagBucket, | ||
}); | ||
|
||
if (isEmpty(filesArray)) { | ||
this.emit('status', 'error', 'No file to import.'); | ||
return; | ||
} | ||
|
||
const file = filesArray[0]; | ||
|
||
if (!endsWith(file.name.toLowerCase(), '.json')) { | ||
this.emit('status', 'error', 'File name does not end in ".json".'); | ||
return; | ||
} | ||
|
||
// Limit file size we will read to 1mb | ||
if (file.size > 1000000) { | ||
this.emit('status', 'error', 'File should be less than 1 MB.'); | ||
return; | ||
} | ||
|
||
const fileReader = new FileReader(); | ||
|
||
fileReader.onload = event => { | ||
const fileContent = event.target.result; | ||
|
||
if (!fileContent) { | ||
this.emit('status', 'error', 'File was empty.'); | ||
return; | ||
} | ||
|
||
const dataObj = JSON.parse(fileContent); | ||
const noteCount = | ||
dataObj.activeNotes.length + dataObj.trashedNotes.length; | ||
|
||
coreImporter.importNotes(dataObj, this.options); | ||
this.emit('status', 'complete', noteCount); | ||
}; | ||
|
||
fileReader.readAsText(file); | ||
}; | ||
} | ||
|
||
export default SimplenoteImporter; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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