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)) {
export default async () => {
Entity.driver = new MySQLDriver({ connection });
const authentication = new Authentication({
identities: [{ identity: User }]
});
// Configure default storage
const localDriver = new LocalDriver({
directory: __dirname + "/storage",
createDatePrefix: false,
publicUrl: "https://cdn.domain.com"
});
const storage = new Storage(localDriver);
// Register attributes
registerSecurityAttributes(authentication);
registerBufferAttribute();
registerFileAttributes({ entity: File, storage });
registerImageAttributes({ entity: Image });
console.log("Importing data...");
for (let i = 0; i < importData.length; i++) {
const { entity, data } = await importData[i]();
for (let j = 0; j < data.length; j++) {
const obj = data[j];
const instance = new entity();
try {
instance.populate(obj);
await instance.save();
export default async () => {
Entity.driver = new MySQLDriver({ connection });
const authentication = new Authentication({
identities: [{ identity: User }]
});
// Configure default storage
const localDriver = new LocalDriver({
directory: __dirname + "/storage",
createDatePrefix: false,
publicUrl: "https://cdn.domain.com"
});
const storage = new Storage(localDriver);
// Register attributes
registerSecurityAttributes(authentication);
registerBufferAttribute();
registerFileAttributes({ entity: File, storage });
registerImageAttributes({ entity: Image });
console.log("Importing data...");
for (let i = 0; i < importData.length; i++) {
const { entity, data } = await importData[i]();
for (let j = 0; j < data.length; j++) {
const obj = data[j];
const instance = new entity();
try {
instance.populate(obj);
await instance.save();
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) {
attributes: ({
/* passwordAttribute,
identityAttribute,
bufferAttribute,*/
fileAttributes,
imageAttributes
}) => {
/* identityAttribute();
passwordAttribute();
bufferAttribute();*/
fileAttributes({
entity: File,
storage: new Storage(localDriver)
});
imageAttributes({
entity: Image,
processor: imageProcessor(),
quality: 90,
storage: new Storage(localDriver)
});
}
},
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;
}
});
}