keystone icon indicating copy to clipboard operation
keystone copied to clipboard

Image previews from local file is not working after uploading

Open maheraldous opened this issue 6 years ago • 5 comments

Bug report

Describe the bug

Image previews from local file is not working after uploading A clear and concise description of the bug. Image previews work from local file but it is show that there is not previw after I uploaded it.

Product.js file

const { CloudinaryAdapter, LocalFileAdapter } = require('@keystonejs/file-adapters');
const { Decimal, Text, File } = require('@keystonejs/fields');
const { CloudinaryImage } = require('@keystonejs/fields-cloudinary-image');
require('dotenv').config();

const adapter = new CloudinaryAdapter({
    cloudName: "delegbtc8",
    apiKey: "276618124358484",
    apiSecret: "bDzRXt4fmEZRxP6kF9b-ncDhggI",
    folder: "cloudinary://276618124358484:bDzRXt4fmEZRxP6kF9b-ncDhggI@delegbtc8"
});

const fileAdapter = new LocalFileAdapter({
    src: './client/src/images',
    path: '/client/src/images',
});
module.exports = {
    fields: {
        CloudinaryImage: {
            type: CloudinaryImage,
            adapter
        },
        Image: {
            type: File,
            adapter: fileAdapter,
            hooks: {
                beforeChange: async ({ existingItem }) => {
                    if (existingItem && existingItem.file) {
                        await fileAdapter.delete(existingItem.file);
                    }
                },
            },
        },
        title: {
            type: Text,
            isRequired: true
        },
        description: {
            type: Text,
            isRequired: true
        },
        price: {
            type: Decimal,
            isRequired: true
        }
    },
    hooks: {
        afterDelete: async ({ existingItem }) => {
            if (existingItem.file) {
            await fileAdapter.delete(existingItem.file);
            }
        },
    },
};

During the upload it show the preview see: https://prnt.sc/us1n8a

After uploading, the preview dose not work see: https://prnt.sc/us1o89

I can see the image that I uploaded in the image folder but not it's preview in the Admin UI.

maheraldous avatar Oct 02 '20 17:10 maheraldous

have you used a StaticApp for /client/src/images path? they both are needed. right click the image and open in new tab, you will see how url is constructed

gautamsi avatar Oct 02 '20 18:10 gautamsi

@gautamsi and @timleslie

The path is http://localhost:3000/client/src/images/5f8b5d1a92586d272855aca0-test-image.jpg and it give me Cannot GET /client/src/images/5f8b5d1a92586d272855aca0-test-image.jpg page.

index.js in the root
module.exports = {
  keystone,
  apps: [
    new GraphQLApp(),
    new StaticApp({
      path: '/',
      src: 'client/src',
      fallback: 'client/public/index.html'
    }),
    new AdminUIApp({
      name: PROJECT_NAME,
      enableDefaultRoute: true,
      authStrategy
    }),
  ],
  configureExpress: app => {
    app.set('trust proxy', 1);
  }
};
lists/Product.js

const { CloudinaryAdapter, LocalFileAdapter } = require('@keystonejs/file-adapters');
const { Decimal, Text, File, Slug, Select, Relationship } = require('@keystonejs/fields');
const { byTracking, atTracking } = require('@keystonejs/list-plugins');
const { AuthedRelationship } = require('@keystonejs/fields-authed-relationship');
const { CloudinaryImage } = require('@keystonejs/fields-cloudinary-image');
const { Wysiwyg } = require('@keystonejs/fields-wysiwyg-tinymce');
const Stars = require('../fields/Stars');
const MultiCheck = require('../fields/MultiCheck');
require('dotenv').config();

const adapter = new CloudinaryAdapter({
    cloudName: "***",
    apiKey: "***",
    apiSecret: "***",
    folder: "***"
});

const fileAdapter = new LocalFileAdapter({
    src: './client/src/images/',
    path: '/client/src/images/',
});

// Access control functions
const userIsAdmin = ({ authentication: { item: user } }) => {
    !!user && !!user.isAdmin;
};

const userOwnsItem = ({ authentication: { item: user } }) => {
    if (!user) {
        return false;
    }

    // Instead of a boolean, you can return a GraphQL query:
    // https://www.keystonejs.com/api/access-control#graphqlwhere
    return { id: user.id };
};

const userIsAdminOrOwner = auth => {
    const isAdmin = access.userIsAdmin(auth);
    const isOwner = access.userOwnsItem(auth);
    return isAdmin ? isAdmin : isOwner;
};

const access = { userIsAdmin, userOwnsItem, userIsAdminOrOwner };

module.exports = {
    schemaDoc: 'A list of things which need to be done',
    fields: {
        CloudinaryImage: {
            type: CloudinaryImage,
            adapter
        },
        Image: {
            type: File,
            adapter: fileAdapter,
            thumb: true,
            hooks: {
                beforeChange: async ({ existingItem }) => {
                    if (existingItem && existingItem.file) {
                        await fileAdapter.delete(existingItem.file);
                    }
                },
            },
        },
        title: {
            type: Text,
            isRequired: true,
        },
        slug: {
            type: Slug,
            from: 'title',
        },
        content: {
            type: Wysiwyg,
            isRequired: true,
        },
        price: {
            type: Decimal,
            isRequired: true,
        },
        rating: {
            type: Stars,
            starCount: 5,
        },
        tags: {
            type: MultiCheck,
            options: ['Action', 'Comedy', 'Drama'],
            defaultValue: [true, false, false],
        },
        categories: {
            type: Relationship,
            isRequired: true,
            ref: 'Category.products',
            many: false,
        },
        owner: {
            type: Relationship,
            isRequired: true,
            ref: 'User.products',
            many: false,
            // access: {
            //     read: access.userIsAdmin,
            //     update: access.userIsAdmin,
            //     create: access.userIsAdmin,
            //     delete: access.userIsAdmin,
            //     auth: true
            // },
        },
        status: {
            type: Select,
            defaultValue: 'draft',
            options: [
                { label: 'Draft', value: 'draft' },
                { label: 'Published', value: 'published' },
            ],
        },
        // someDate: {
        //     type: CalendarDay,
        //     format: 'do MMMM yyyy',
        //     dateFrom: '1901-01-01',
        //     dateTo: formatISO(new Date(), { representation: 'date' }),
        // },
    },
    hooks: {
        afterDelete: async ({ existingItem }) => {
            if (existingItem.file) {
            await fileAdapter.delete(existingItem.file);
            }
        },
    },
    plugins: [
        atTracking({
            createdAtField: 'publishedAt',
            updatedAtField: 'updatedAt',
            format: 'MM/DD/YYYY h:mm A',
            access: {
                read: true,
                create: true,
                update: true,
                auth: true,
            },
        }),
        // byTracking({
        //     createdByField: 'createdBy',
        //     updatedByField: 'updatedBy',
        //     ref: 'User',
        //     access: {
        //         read: true,
        //         create: true,
        //         update: true,
        //         auth: true,
        //     },
        // }),
    ],
};

maheraldous avatar Oct 17 '20 21:10 maheraldous

It looks like there hasn't been any activity here in over 6 months. Sorry about that! We've flagged this issue for special attention. It wil be manually reviewed by maintainers, not automatically closed. If you have any additional information please leave us a comment. It really helps! Thank you for you contribution. :)

stale[bot] avatar Feb 21 '21 23:02 stale[bot]

Keystone 5 has officially moved into active maintenance mode as we push towards the next major new version Keystone Next, you can find out more information about this transition here.

In an effort to sustain the project going forward, we're cleaning up and closing old issues such as this one. If you feel this issue is still relevant for Keystone Next, please let us know.

bladey avatar Apr 08 '21 04:04 bladey

@maheraldous did you solve this problem?

SzymonNiemiec avatar May 13 '21 07:05 SzymonNiemiec