mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-08-02 17:24:57 +02:00
Update:Remove read-chunk dependency
This commit is contained in:
parent
78079b2e60
commit
416db7c981
11 changed files with 155 additions and 87 deletions
42
server/libs/readChunk/index.js
Normal file
42
server/libs/readChunk/index.js
Normal file
|
@ -0,0 +1,42 @@
|
|||
'use strict';
|
||||
|
||||
// https://github.com/sindresorhus/read-chunk
|
||||
|
||||
const fs = require('fs');
|
||||
const pify = require('./pify');
|
||||
const withOpenFile = require('./withOpenFile');
|
||||
|
||||
const fsReadP = pify(fs.read, { multiArgs: true });
|
||||
|
||||
const readChunk = (filePath, startPosition, length) => {
|
||||
const buffer = Buffer.alloc(length);
|
||||
|
||||
return withOpenFile(filePath, 'r', fileDescriptor =>
|
||||
fsReadP(fileDescriptor, buffer, 0, length, startPosition)
|
||||
)
|
||||
.then(([bytesRead, buffer]) => {
|
||||
if (bytesRead < length) {
|
||||
buffer = buffer.slice(0, bytesRead);
|
||||
}
|
||||
|
||||
return buffer;
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = readChunk;
|
||||
// TODO: Remove this for the next major release
|
||||
module.exports.default = readChunk;
|
||||
|
||||
module.exports.sync = (filePath, startPosition, length) => {
|
||||
let buffer = Buffer.alloc(length);
|
||||
|
||||
const bytesRead = withOpenFile.sync(filePath, 'r', fileDescriptor =>
|
||||
fs.readSync(fileDescriptor, buffer, 0, length, startPosition)
|
||||
);
|
||||
|
||||
if (bytesRead < length) {
|
||||
buffer = buffer.slice(0, bytesRead);
|
||||
}
|
||||
|
||||
return buffer;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue