mirror of
https://github.com/sismics/docs.git
synced 2024-11-21 21:47:57 +01:00
file importer
This commit is contained in:
parent
cc6e1b4052
commit
f70cded634
1
.gitignore
vendored
1
.gitignore
vendored
@ -10,3 +10,4 @@
|
|||||||
/.project
|
/.project
|
||||||
*.iml
|
*.iml
|
||||||
node_modules
|
node_modules
|
||||||
|
import_test
|
||||||
|
@ -47,6 +47,7 @@ Features
|
|||||||
- Document sharing by URL
|
- Document sharing by URL
|
||||||
- RESTful Web API
|
- RESTful Web API
|
||||||
- Fully featured Android client
|
- Fully featured Android client
|
||||||
|
- [Mass files importer](https://github.com/sismics/docs/tree/master/docs-importer) (single or scan mode) ![New!](https://www.sismics.com/public/img/new.png)
|
||||||
- Tested to 100k documents
|
- Tested to 100k documents
|
||||||
|
|
||||||
Download
|
Download
|
||||||
|
22
docs-importer/README.md
Normal file
22
docs-importer/README.md
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
File Importer
|
||||||
|
=============
|
||||||
|
|
||||||
|
This tool can be used to do a single import of files or to periodically scan for files in an input folder.
|
||||||
|
|
||||||
|
Requirements
|
||||||
|
------------
|
||||||
|
- NodeJS 9
|
||||||
|
- NPM
|
||||||
|
|
||||||
|
Usage
|
||||||
|
-----
|
||||||
|
```console
|
||||||
|
npm install
|
||||||
|
node main.js
|
||||||
|
```
|
||||||
|
|
||||||
|
A wizard will ask you for the import configuration and write it in `~/.config/preferences/com.sismics.docs.importer.pref`
|
||||||
|
|
||||||
|
Daemon mode
|
||||||
|
-----------
|
||||||
|
The daemon mode scan the input directory every 30 seconds for new files. Once a file is found and imported, it is **deleted**.
|
@ -8,11 +8,12 @@ const fs = require('fs');
|
|||||||
const request = require('request').defaults({
|
const request = require('request').defaults({
|
||||||
jar: true
|
jar: true
|
||||||
});
|
});
|
||||||
// request.debug = true;
|
|
||||||
|
|
||||||
// Load preferences
|
// Load preferences
|
||||||
const prefs = new preferences('com.sismics.docs.importer',{
|
const prefs = new preferences('com.sismics.docs.importer',{
|
||||||
importer: {}
|
importer: {
|
||||||
|
daemon: false
|
||||||
|
}
|
||||||
}, {
|
}, {
|
||||||
encrypt: false,
|
encrypt: false,
|
||||||
format: 'yaml'
|
format: 'yaml'
|
||||||
@ -91,7 +92,7 @@ const askCredentials = () => {
|
|||||||
remember: true
|
remember: true
|
||||||
}
|
}
|
||||||
}, function (error, response) {
|
}, function (error, response) {
|
||||||
if (!response || response.statusCode !== 200) {
|
if (error || !response || response.statusCode !== 200) {
|
||||||
spinner.fail('Username or password incorrect');
|
spinner.fail('Username or password incorrect');
|
||||||
askCredentials();
|
askCredentials();
|
||||||
return;
|
return;
|
||||||
@ -123,16 +124,107 @@ const askPath = () => {
|
|||||||
text: 'Checking import path',
|
text: 'Checking import path',
|
||||||
spinner: 'flips'
|
spinner: 'flips'
|
||||||
}).start();
|
}).start();
|
||||||
fs.lstat(answers.path, (err, stats) => {
|
fs.lstat(answers.path, (error, stats) => {
|
||||||
if (err || !stats.isDirectory()) {
|
if (error || !stats.isDirectory()) {
|
||||||
spinner.fail('Please enter a valid directory path');
|
spinner.fail('Please enter a valid directory path');
|
||||||
|
askPath();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
recursive(answers.path, function (err, files) {
|
fs.access(answers.path, fs.W_OK | fs.R_OK, (error) => {
|
||||||
spinner.succeed(files.length + ' files in this directory');
|
if (error) {
|
||||||
// TODO Then?
|
spinner.fail('This directory is not writable');
|
||||||
|
askPath();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
recursive(answers.path, function (error, files) {
|
||||||
|
spinner.succeed(files.length + ' files in this directory');
|
||||||
|
askDaemon();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Ask for daemon mode
|
||||||
|
const askDaemon = () => {
|
||||||
|
console.log('');
|
||||||
|
|
||||||
|
inquirer.prompt([
|
||||||
|
{
|
||||||
|
type: 'confirm',
|
||||||
|
name: 'daemon',
|
||||||
|
message: 'Do you want to run the importer in daemon mode (it will poll the input directory for new files, import and delete them)?',
|
||||||
|
default: prefs.importer.daemon === true
|
||||||
|
}
|
||||||
|
]).then(answers => {
|
||||||
|
// Save daemon
|
||||||
|
prefs.importer.daemon = answers.daemon;
|
||||||
|
|
||||||
|
start();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// Start the import
|
||||||
|
const start = () => {
|
||||||
|
if (prefs.importer.daemon) {
|
||||||
|
console.log('\nPolling the input folder for new files...');
|
||||||
|
|
||||||
|
let resolve = () => {
|
||||||
|
importFiles(true, () => {
|
||||||
|
setTimeout(resolve, 30000);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
resolve();
|
||||||
|
} else {
|
||||||
|
importFiles(false, () => {});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Import the files
|
||||||
|
const importFiles = (remove, filesImported) => {
|
||||||
|
recursive(prefs.importer.path, function (error, files) {
|
||||||
|
if (files.length === 0) {
|
||||||
|
filesImported();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let index = 0;
|
||||||
|
let resolve = () => {
|
||||||
|
const file = files[index++];
|
||||||
|
if (file) {
|
||||||
|
importFile(file, remove, resolve);
|
||||||
|
} else {
|
||||||
|
filesImported();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// Import a file
|
||||||
|
const importFile = (file, remove, resolve) => {
|
||||||
|
const spinner = ora({
|
||||||
|
text: 'Importing: ' + file,
|
||||||
|
spinner: 'flips'
|
||||||
|
}).start();
|
||||||
|
|
||||||
|
request.put({
|
||||||
|
url: prefs.importer.baseUrl + '/api/file',
|
||||||
|
formData: {
|
||||||
|
file: fs.createReadStream(file)
|
||||||
|
}
|
||||||
|
}, function (error, response) {
|
||||||
|
if (error || !response || response.statusCode !== 200) {
|
||||||
|
spinner.fail('Upload failed for ' + file + ': ' + error);
|
||||||
|
resolve();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
spinner.succeed('Upload successful for ' + file);
|
||||||
|
if (remove) {
|
||||||
|
fs.unlinkSync(file);
|
||||||
|
}
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user