Skip to content

Commit

Permalink
refactor: Remove duplicate code for save content in store (#778)
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-m authored Oct 5, 2021
1 parent 20a1c4e commit fab42bf
Showing 1 changed file with 31 additions and 47 deletions.
78 changes: 31 additions & 47 deletions lib/stores/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,35 @@ class FilesystemStore {
await fs.writeFile(metadataPath, JSON.stringify(json, null, 2));
}

async putContent(objectPath, content) {
return new Promise((resolve, reject) => {
const writeStream = createWriteStream(objectPath);
const md5Context = crypto.createHash('md5');

if (Buffer.isBuffer(content)) {
writeStream.end(content);
md5Context.update(content);
resolve([content.length, md5Context.digest('hex')]);
} else {
let totalLength = 0;
pipeline(
content,
new Transform({
transform(chunk, encoding, callback) {
md5Context.update(chunk, encoding);
totalLength += chunk.length;
callback(null, chunk);
},
}),
writeStream,
(err) =>
err
? reject(err)
: resolve([totalLength, md5Context.digest('hex')]),
);
}
});
}
// store implementation

reset() {
Expand Down Expand Up @@ -291,34 +320,7 @@ class FilesystemStore {
);

await ensureDir(path.dirname(objectPath));

const [size, md5] = await new Promise((resolve, reject) => {
const writeStream = createWriteStream(objectPath);
const md5Context = crypto.createHash('md5');

if (Buffer.isBuffer(object.content)) {
writeStream.end(object.content);
md5Context.update(object.content);
resolve([object.content.length, md5Context.digest('hex')]);
} else {
let totalLength = 0;
pipeline(
object.content,
new Transform({
transform(chunk, encoding, callback) {
md5Context.update(chunk, encoding);
totalLength += chunk.length;
callback(null, chunk);
},
}),
writeStream,
(err) =>
err
? reject(err)
: resolve([totalLength, md5Context.digest('hex')]),
);
}
});
const [size, md5] = await this.putContent(objectPath, object.content);
await this.putMetadata(object.bucket, object.key, object.metadata, md5);
return { size, md5 };
}
Expand Down Expand Up @@ -407,25 +409,7 @@ class FilesystemStore {

await ensureDir(path.dirname(partPath));

const [size, md5] = await new Promise((resolve, reject) => {
const writeStream = createWriteStream(partPath);
const md5Context = crypto.createHash('md5');
let totalLength = 0;

pipeline(
content,
new Transform({
transform(chunk, encoding, callback) {
md5Context.update(chunk, encoding);
totalLength += chunk.length;
callback(null, chunk);
},
}),
writeStream,
(err) =>
err ? reject(err) : resolve([totalLength, md5Context.digest('hex')]),
);
});
const [size, md5] = await this.putContent(partPath, content);
await fs.writeFile(`${partPath}.md5`, md5);
return { size, md5 };
}
Expand Down

0 comments on commit fab42bf

Please sign in to comment.