-
Notifications
You must be signed in to change notification settings - Fork 0
save, load and renew cursor #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4ab5711
save, load and renew cursor
53e3b64
Restored test suite
Neamar 003f52d
some optimisations
7239bed
no more gobal variable
53a8862
improve architecture
9ffcd36
typo
e741f6e
typo
9c2dec2
improve architecture
8d0643a
fix cb error
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| "use strict"; | ||
| var fs = require("fs"); | ||
|
|
||
| var getSavePath = function(dir) { | ||
| var homeDir = process.env.HOMEPATH || process.env.HOME; | ||
| if(!getSavePath.hasMkdir) { | ||
| try { | ||
| fs.mkdirSync(homeDir + "/.anyfetch-file-watcher/"); | ||
| } | ||
| catch(err) {} | ||
| getSavePath.hasMkdir = true; | ||
| } | ||
| return homeDir + "/.anyfetch-file-watcher/" + dir.trim().replace(/(\/|\\)/g, ''); | ||
| }; | ||
| getSavePath.hasMkdir = false; | ||
|
|
||
|
|
||
| var saveCursor = function(dir, newCursor, cb) { | ||
| fs.writeFile(getSavePath(dir), JSON.stringify(newCursor), cb); | ||
| }; | ||
|
|
||
| module.exports = saveCursor; | ||
| module.exports.getSavePath = getSavePath; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,12 @@ | ||
| "use strict"; | ||
|
|
||
| var Anyfetch = require('anyfetch'); | ||
| var AnyfetchClient = require('anyfetch'); | ||
| var fs = require('fs'); | ||
| var path = require('path'); | ||
| var async = require('async'); | ||
|
|
||
| module.exports = function uploadFile(filePath, accessToken, baseIdentifier, cb) { | ||
| var anyfetch = new Anyfetch(); | ||
| var uploadFile = function(dir, filePath, accessToken, baseIdentifier, cb) { | ||
| var anyfetch = new AnyfetchClient(); | ||
| anyfetch.setAccessToken(accessToken); | ||
|
|
||
| // Send a document to anyFetch | ||
|
|
@@ -16,10 +17,22 @@ module.exports = function uploadFile(filePath, accessToken, baseIdentifier, cb) | |
| var fileConfig = function() { | ||
| // Wrap this in a function to avoid creating the stream before reading it. | ||
| return { | ||
| file: fs.createReadStream(filePath), | ||
| filename: path.baseName(filePath), | ||
| file: fs.createReadStream(dir + filePath), | ||
| filename: path.basename(dir + filePath), | ||
| }; | ||
| }; | ||
|
|
||
| anyfetch.sendDocumentAndFile(document, fileConfig, cb); | ||
| anyfetch.sendDocumentAndFile(document, fileConfig, function(err) { | ||
| cb(err); | ||
| }); | ||
| }; | ||
|
|
||
| var queue = async.queue(function worker(task, cb) { | ||
| uploadFile(task.dir, task.filePath, task.accessToken, task.baseIdentifier, cb); | ||
| }, 4); | ||
|
|
||
| module.exports = function pushToQueue(task) { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (task, cb) => see documentation for async.queue |
||
| queue.push(task); | ||
| }; | ||
|
|
||
| module.exports.uploadFile = uploadFile; | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| "use strict"; | ||
| var fs = require('fs'); | ||
| var async = require('async'); | ||
| var os = require('os'); | ||
|
|
||
| var retrieveFiles = require('./helpers/list-files').retrieveFiles; | ||
| var pushToQueue = require('./helpers/upload'); | ||
| var saveCursor = require('./helpers/save-path'); | ||
| var getSavePath = saveCursor.getSavePath; | ||
|
|
||
|
|
||
| module.exports = function update(dir, accessToken, cb) { | ||
| async.waterfall([ | ||
| function getOldCursor(cb) { | ||
| fs.readFile(getSavePath(dir), function(err, cursor) { | ||
| if(err && err.code !== 'ENOENT') { | ||
| cb(err); | ||
| } | ||
| else { | ||
| cb(null, cursor); | ||
| } | ||
| }); | ||
| }, | ||
| function updateCursor(oldCursor, cb) { | ||
| retrieveFiles(dir, oldCursor, cb); | ||
| }, | ||
| function saveNewCursor(filesToUpload, newCursor, cb) { | ||
| saveCursor(dir, newCursor, function(err) { | ||
| cb(err, filesToUpload); | ||
| }); | ||
| }, | ||
| function uploadFiles(files, cb) { | ||
| files.forEach(function(key) { | ||
| var task = { | ||
| 'dir': dir, | ||
| 'filePath': key, | ||
| 'accessToken': accessToken, | ||
| 'baseIdentifier': os.hostname() | ||
| }; | ||
| pushToQueue(task); | ||
| }); | ||
| cb(); | ||
| } | ||
| ], cb); | ||
| }; |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| 'use strict'; | ||
|
|
||
| require('should'); | ||
|
|
||
| var uploadFile = require('../../lib/helpers/upload.js').uploadFile; | ||
| var Anyfetch = require('anyfetch'); | ||
|
|
||
| describe('uploadFile', function() { | ||
|
|
||
| process.env.ANYFETCH_API_URL = 'http://localhost:1338'; | ||
| var countFile = 0; | ||
| var mockServerHandler = function(url){ | ||
| if (url.indexOf("/file") !== -1) { | ||
| countFile += 1; | ||
| } | ||
| }; | ||
| var apiServer; | ||
|
|
||
| before(function() { | ||
| // Create a fake HTTP server | ||
| apiServer = Anyfetch.debug.createTestApiServer(mockServerHandler); | ||
| apiServer.listen(1338); | ||
| }); | ||
|
|
||
| after(function(){ | ||
| apiServer.close(); | ||
| }); | ||
|
|
||
| it('should upload the file', function(done) { | ||
|
|
||
| uploadFile(__dirname + "/..", "/sample-directory/txt1.txt", "randomAccessToken", "randomBaseIdentifier", function(err) { | ||
| if(err) { | ||
| throw err; | ||
| } | ||
| countFile.should.be.eql(1); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should.eql |
||
| done(); | ||
| }); | ||
|
|
||
| }); | ||
| }); | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| 'use strict'; | ||
|
|
||
| require('should'); | ||
|
|
||
| var path = require('path'); | ||
| var Anyfetch = require('anyfetch'); | ||
| var fs = require('fs'); | ||
|
|
||
| var update = require('../lib/index.js'); | ||
| var getSavePath = require('../lib/helpers/save-path.js').getSavePath; | ||
|
|
||
|
|
||
| describe('update() function', function() { | ||
| process.env.ANYFETCH_API_URL = 'http://localhost:1338'; | ||
| var countFile = 0; | ||
| var mockServerHandler = function(url){ | ||
| if (url.indexOf("/file") !== -1) { | ||
| countFile += 1; | ||
| } | ||
| }; | ||
|
|
||
| var apiServer; | ||
| before(function() { | ||
| // Create a fake HTTP server | ||
| apiServer = Anyfetch.debug.createTestApiServer(mockServerHandler); | ||
| apiServer.listen(1338); | ||
| }); | ||
|
|
||
| after(function(){ | ||
| apiServer.close(); | ||
|
|
||
| // Clean cursor | ||
|
|
||
| fs.unlinkSync(getSavePath(path.resolve(__dirname + "/../test/sample-directory"))); | ||
| }); | ||
|
|
||
| it('should update account', function(done) { | ||
| update(path.resolve(__dirname + "/../test/sample-directory"), "randomAccessToken", function(err) { | ||
| if(err) { | ||
| throw err; | ||
| } | ||
| setTimeout(function() { | ||
| countFile.should.eql(5); | ||
| done(); | ||
| }, 200); | ||
| }); | ||
|
|
||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
config/configuration.js