const REPOSITORY_ID = "com.demo.test.sdm";
let sm = new CmisSessionManager(sdmCredentials);
// create Repository if not there
// await sm.createRepositoryIfNotExists(REPOSITORY_ID, "provider", {});
const app = express();
const port = 3000;
app.use(cors());
app.get('/', (req, res) => {
res.send('This is a test Server!');
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
app.get('/download', async (req, res) => {
// create or reuse cmis session
let session = await sm.getOrCreateConnection(REPOSITORY_ID, "provider");
// get object by path req.param.objectPath = "/temp/doc.pdf"
let obj = await session.getObjectByPath(req.param.objectPath);
// get the content Stream
let result = await session.getContentStream(obj.succinctProperties["cmis:objectId"]);
// update the needed Headers
res.header('Content-Type', obj.succinctProperties["cmis:contentStreamMimeType"]);
res.header('Content-Length', obj.succinctProperties["cmis:contentStreamLength"]);
res.header('Content-Disposition', `attachment; filename="${obj.succinctProperties["cmis:name"]}"`);
// pipe the doc store response to the client
result.body.pipe(res);
});
// Set up storage for uploaded files
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'uploads/');
}
});
// Create multer instance with the storage configuration
const upload = multer({storage: storage})
app.post('/upload', upload.single('file'), async (req, res) => {
if (req.file) {
// create or reuse DMS session
let session = await sm.getOrCreateConnection(REPOSITORY_ID, "provider");
// multer collects a file parts and returns temp file path.
// let's create a read stream from that path
let readStream = fs.createReadStream(req.file.path);
// upload the file to DMS
let response = await session.createDocumentFromStream("/temp", readStream, req.file.originalname)
res.status(200).end(response.data);
} else {
res.status(400).end('Uploaded File not found.');
}
});
Results:
Total Data Uploaded : 2 GB
No of files : 10 txt files
Time Taken: 12 mins
Total Memory Used: 3.8 GB
Served Disk Used : 2.1 GB
app.post('/upload-optimised', async (req, res) => {
// get the file metadata from custom headers.
const fileName = req.headers["cs-filename"];
const opType = req.headers["cs-operation"];
const mimeType = req.headers["content-type"];
// create or reuse the DMS session
let session = await sm.getOrCreateConnection(REPOSITORY_ID, "provider");
let response = {success: "false"};
// if operation is "create" then create the document in DMS with initial chuck
if (opType === "create") {
// create a document from the response stream
response = await session.createDocumentFromStream("/temp", req, fileName);
}
// if operation is "append" then append the content an existing file
if (opType === "append") {
const obj = await session.getObjectByPath("/temp/" + fileName);
// get the object id from the object path.
const objId = obj.succinctProperties["cmis:objectId"];
// append the content to the previously created file.
response = await session.appendContentFromStream(objId, req);
}
res.json(response);
});
<html>
<head>
<title>TEST</title>
</head>
<body>
<h1>Upload a File</h1>
<div>
<input id="uploadFile" type="file" name="fileInput">
<input value="Upload" onclick="uploadFile(this)">
</div>
<script>
// trigger when upload button is clicked
function uploadFile(event) {
let elementById = document.getElementById("uploadFile");
// get the selected file
const file = elementById.files[0];
if (file) {
// read the file content and upload in chunks
const reader = new FileReader();
reader.onload = function (event) {
const contents = event.target.result;
console.log('File contents:', contents.length);
uploadFileInChunks(file, contents);
};
reader.readAsText(file);
}
}
async function uploadFileInChunks(file, content) {
// Specify your desired chunk size
const chunkSize = 1024;
// console.log(content);
// total no of chunks to be uploaded, may be created a progress bar
const totalChunks = Math.ceil(content.length / chunkSize);
for (let i = 0; i < totalChunks; i++) {
// calculate start of the chunk
const start = i * chunkSize;
// calculate end of chuck
const end = Math.min(start + chunkSize, content.length);
// get the chunk from the entire content
const chunk = content.slice(start, end);
// Process the chunk
console.log('Chunk', i + 1, 'of', totalChunks, ':', chunk);
// create if first chunk or else append
const operation = i === 0 ? "create" : "append";
const myHeaders = new Headers();
myHeaders.append("cs-filename", file.name);
myHeaders.append("cs-operation", operation);
myHeaders.append("Content-Type", file.type);
const requestOptions = {
method: 'POST',
headers: myHeaders,
body: chunk,
redirect: 'follow'
};
// upload to the server
const response = await fetch("http://localhost:3000/upload-optimised/", requestOptions);
console.log(await response.json());
}
}
</script>
</body>
</html>
Results:
Total Data Uploaded : 2 GB
No of files : 10 txt files
Time Taken: 9 mins
Total Memory Used: 200 Mb
Served Disk Used : 0 bytes
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
9 | |
8 | |
6 | |
5 | |
4 | |
4 | |
4 | |
3 | |
3 | |
3 |