Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
setFile(key: string, file: IFileData): Promise {
if (file.body === null) {
return Promise.reject(new StorageError("File body must be a string or a Buffer"));
}
let newKey = key;
// date prepend
if (this.config.createDatePrefix) {
// eslint-disable-next-line
const regex = new RegExp(`^${this.config.directory}\/\\d{4}\/\\d{2}\/\\d{2}\/`);
if (!regex.test(newKey)) {
newKey = fecha.format(Date.now(), "YYYY/MM/DD") + "/" + key;
}
}
// directory prepend
const prefix = this.config.directory + "/";
if (this.config.directory !== "" && !newKey.startsWith(prefix)) {
async getKeys(key?: string, filter?: string): Promise> {
let keys = [];
let result = [];
let newResult = {};
let continuationToken = "";
if (typeof key !== "string") {
return Promise.reject(
new StorageError("S3 driver requires that the key parameter is present.")
);
}
// get all objects matching the prefix
do {
newResult = await this.__listBucketContent(key, continuationToken);
result = _.concat(result, newResult.items || []);
continuationToken = newResult.continuationToken;
} while (newResult.continuationToken);
// filter the objects before returning
if (typeof filter === "string") {
const regex = new RegExp(filter, "g");
result.forEach(item => {
if (item.Key.match(regex) !== null) {
getFile(key: string, options?: Object): Promise {
const params = {
Bucket: this.config.bucket,
Key: key
};
if (options !== null && options !== undefined) {
return Promise.reject(
new StorageError("S3 driver doesn't support the options parameter.")
);
}
return this.s3
.getObject(params)
.promise()
.then(data => {
return { body: data.Body, meta: _.omit(data, ["Body"]) };
});
}
setFile(key: string, file: IFileData): Promise {
if (file.body === null) {
return Promise.reject(new StorageError("File body must be a string or a Buffer"));
}
let newKey = key;
if (this.config.createDatePrefix) {
if (!/^\d{4}\/\d{2}\/\d{2}\//.test(newKey)) {
newKey = path.join(fecha.format(Date.now(), "YYYY/MM/DD"), key);
}
}
newKey = _.trimStart(newKey, "/");
const filePath = path.join(this.config.directory, newKey);
const content: string | Buffer = file.body;
return fs.outputFile(filePath, content).then(() => newKey);
}
fs.readFile(filePath, opts, (err: ?ErrnoError, data: string | Buffer) => {
if (err) {
return reject(new StorageError(err.message));
}
resolve({ body: data });
});
});
return this.getMeta(key).then(data => {
if (data === undefined || data === null || !("LastModified" in data)) {
return Promise.reject(
new StorageError("Unable to determine object last modified time.")
);
} else {
const date = new Date(data.LastModified);
return date.getTime();
}
});
}
return this.getMeta(key).then(data => {
if (data === undefined || data === null || "ContentType" in data !== true) {
return Promise.reject(new StorageError("Unable to determine object size."));
} else {
return data.ContentType;
}
});
}
fs.readFile(filePath, opts, (err: ?ErrnoError, data: string | Buffer) => {
if (err) {
return reject(new StorageError(err.message));
}
resolve({ body: data });
});
});