How to use webiny-file-storage - 10 common examples

To help you get started, we’ve selected a few webiny-file-storage examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github webiny / webiny-js / packages-utils / webiny-file-storage-s3 / src / index.js View on Github external
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)) {
github webiny / webiny-js / examples / api / src / import / users / import.js View on Github external
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();
github webiny / webiny-js / examples / api / src / import / users / import.js View on Github external
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();
github webiny / webiny-js / packages-utils / webiny-file-storage-s3 / src / index.js View on Github external
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) {
github webiny / webiny-js / examples / cloud-api / src / app / middleware.js View on Github external
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)
                });
            }
        },
github webiny / webiny-js / packages-utils / webiny-file-storage-s3 / src / index.js View on Github external
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"]) };
            });
    }
github webiny / webiny-js / packages-utils / webiny-file-storage-local / src / index.js View on Github external
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);
    }
github webiny / webiny-js / packages / webiny-file-storage-local / src / index.js View on Github external
fs.readFile(filePath, opts, (err: ?ErrnoError, data: string | Buffer) => {
                if (err) {
                    return reject(new StorageError(err.message));
                }

                resolve({ body: data });
            });
        });
github webiny / webiny-js / packages-utils / webiny-file-storage-s3 / src / index.js View on Github external
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();
            }
        });
    }
github webiny / webiny-js / packages-utils / webiny-file-storage-s3 / src / index.js View on Github external
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;
            }
        });
    }

webiny-file-storage

A plugginable file storage package.

MIT
Latest version published 6 years ago

Package Health Score

66 / 100
Full package analysis

Similar packages