operafr
v1.1.4
Published
Official package to speed up uploads to the open source service **OperaFR**
Downloads
171
Readme
operafr
Official package to speed up uploads to the open source service OperaFR
🔗 Links
Official OperaFR repository: https://github.com/Logic-Layer-Dev/OperaFR
How to use
The package's functions are centered around 3 main methods: instantiate, allocate and upload.
The instantiate function is responsible for creating an object capable of performing the upload tasks. It necessarily needs the following items to be spawned:
const operaUploaderLib = require("operafr");
{...}
const operaUploader = await operaUploaderLib.instantiate({
serverUrl: "localhost",
token: "<token>",
requestPort: 5555,
publishPort: 5556,
})
The requestPort and publishPort can be changed if the service is running on different ports on the target server.
| Key | Explanation | | :---: | :---: | | serverUrl | Server where OperaFR service is running | | token | Authentication token obtained by the login route for the user representing the system. | | requestPort | Port where the Request/Reply service is running | | publishPort | Port where the Pull/Req service is running |
After instantiation, you can now perform the upload steps. To upload a file, you must use the allocate method.
const initialParams = await operaUploader.allocate("<filepath>", {
publicUrl: false,
folderId: <int> //Destiny folder id from server
});
The initialParams will receive this information structure if communication with the server is successful:
{
status: 201,
message: 'File created successfully',
system_filename: '374bcca8-a564-41b9-a61c-8f9fb3793956.<extension>', //Random string
public_url: null //Random string if publicUrl = true
}
The public_url can be used for later consumption of the file in the /files/<public_url> route. The system_filename will be used in the upload method to append the chunks to the correct file.
await operaUploader.upload(initialParams.systemFilename, "<filepath>",
{
chunckSize: (1024 * 1024) * 4 // 4MB example
});
After this call, the file chunks will go to the destination file created on the Opera server. The "await" can be excluded, since it is not necessarily necessary to wait for a response from the server. This way, the client side can be freed from waiting, making everything more dynamic.
The full code:
const operaUploaderLib = require("operafr");
async function main() {
const operaUploader = await operaUploaderLib.instantiate({
serverUrl: "localhost",
token: "<token>",
requestPort: 5555, //Default Port
publishPort: 5556, //Default Port
})
const initialParams = await operaUploader.allocate("<filepath>", {
publicUrl: false,
folderId: 4 //Example ID
});
await operaUploader.upload(initialParams.systemFilename, "<filepath>", {chunckSize: (1024 * 1024) * 4}); // 4MB example
}
main();