Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: field based media/public folders #3208

Merged
merged 15 commits into from
Feb 10, 2020
Merged

Conversation

erezrokah
Copy link
Contributor

@erezrokah erezrokah commented Feb 6, 2020

Fixes #3201

Still requires some manual & automated tests, but will let you have a config like this:

publish_mode: editorial_workflow
media_folder: static/media
public_folder: /media
collections:
  - name: posts
    label: Posts
    label_singular: 'Post'
    folder: content/posts
    create: true
    media_folder: ''
    public_folder: ''
    fields:
      - label: Template
        name: template
        widget: hidden
        default: post
      - label: Title
        name: title
        widget: string
      - label: 'Cover Image'
        name: 'image'
        widget: 'image'
        required: false
        media_library:
          config:
            media_folder: '/{{media_folder}}/posts/images/widget/image'
            public_folder: '{{public_folder}}/posts/images/widget/image'
      - label: Publish Date
        name: date
        widget: datetime
      - label: Entry Image
        name: 'entryimage'
        widget: 'image'
        required: false
      - label: Body
        name: body
        widget: markdown
        media_library:
          config:
            media_folder: '/{{media_folder}}/posts/images/widget/body'
            public_folder: '{{public_folder}}/posts/images/widget/body'

Example PR: https://github.com/erezrokah/netlify-cms-reproductions/pull/74/files

Update

fields:
  - label: Template
    name: template
    widget: hidden
    default: post
  - label: Title
    name: title
    widget: string
  - label: Cover Image
    name: image
    widget: image
    required: false
    media_folder: '/{{media_folder}}/posts/images/widget/image'
    public_folder: '{{public_folder}}/posts/images/widget/image'

Another Update

Fixes #3199

@erquhart
Copy link
Contributor

erquhart commented Feb 7, 2020

The approach in the OP looks right on. Two questions:

  1. Sort of off topic, but are the blank string media_folder/public_folder on the collection necessary?
  2. Can we place these directly on the field? I only ask because the field level media_library config model is to override the top level property of the same name, so these properties are expected to operate at that same level.

Sent with GitHawk

@@ -90,6 +93,19 @@ const sortByScore = (a: fuzzy.FilterResult<EntryValue>, b: fuzzy.FilterResult<En
return 0;
};

export const getFieldsNames = (fields: EntryField[], prefix = '') => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't see this function used anywhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right I need to remove it

@@ -106,6 +106,41 @@ const selectors = {
},
};

const getFieldsMediaLibraryConfigs = (fields: EntryField[]) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using reduce would be cleaner here.

@erezrokah
Copy link
Contributor Author

The approach in the OP looks right on. Two questions:

  1. Sort of off topic, but are the blank string media_folder/public_folder on the collection necessary?
  2. Can we place these directly on the field? I only ask because the field level media_library config model is to override the top level property of the same name, so these properties are expected to operate at that same level.

Sent with GitHawk

  1. They are not necessary at the collection level, just wanted to show how a field without folder settings (entryimage in this case) inherits them.
  2. We can, just will make the code a bit more convoluted in places like this one https://github.com/netlify/netlify-cms/blob/4aba6baf9a65f16bb56ad936414fdf66c980c78f/packages/netlify-cms-widget-file/src/withFileControl.js#L171 where we would need to pass in more parameters on the object.
    Another thing we can do is have them at the field level and transform the config on load

@erezrokah erezrokah force-pushed the feat/field_level_media_folder branch from f79d416 to 517cb06 Compare February 9, 2020 13:14
@erezrokah erezrokah changed the title [WIP] feat: field based media/public folders feat: field based media/public folders Feb 9, 2020
@erquhart erquhart merged commit 97bc0c8 into master Feb 10, 2020
@erquhart erquhart deleted the feat/field_level_media_folder branch February 10, 2020 16:05
@jeremiahfallin
Copy link

jeremiahfallin commented Mar 25, 2020

When I use a file widget with separate media folder, the correct directory pops up when i'm choosing the file, but after i choose the selected file the directory is not added to the path in the md file. Just the file name. I think I might be doing something wrong, but I'm not sure.

I have tried adding it like this:

      - file: "src/pages/board/index.md"
        label: "Board Page"
        name: "board"
        media_folder: '/static/board'

and also to the widget itself:

{ label: File, name: file, media_folder: '/static/board', widget: file },

and neither of those seems to work.

@erezrokah
Copy link
Contributor Author

erezrokah commented Mar 26, 2020

Thanks for reporting @jeremiahfallin, that looks like a bug with setting the default public_folder on fields and files.
Do you mind opening a new issue for that?
I think setting public_folder: '/static/board' explicitly will serve as a workaround.

@jeremiahfallin
Copy link

That worked for me. I opened issue #3483 and included the workaround. Thanks!

@Gashanas
Copy link

Hey guys, I am not sure if this is the right place to leave a comment, but I have noticed an issue regarding these changes.

It seems that getAsset() function not working properly with media_folder and public_folder added. When I am trying to display images in preview cms getAsset does not return the correct file path.

e.g. path of the image image="/assets/features/group-25-2x-min.jpg" after proccessing it through getAsset(image) it returns

{
field: undefined
fileObj: undefined
path: "static/assets/group-25-2x-min.jpg"
url: "/assets/features/group-25-2x-min.jpg"
}

When I am using image widget without media_folder and public_folder getAsset() returns something like this:

{
field: Pe {size: 3, _root: De, __ownerID: undefined, __hash: undefined, __altered: false}
fileObj: File {…}
path: "static/assets/group-25-2x-min.jpg"
url: "blob:http://localhost:8000/c1c8b6c8-0987-4879-a94b-d0649791b575"
}

Everything is working fine with published content, but during the creation I am not able to get correct path to display added image in preview because returned path /assets/features/group-25-2x-min.jpg does not exists it should be converted to blob.

@erezrokah
Copy link
Contributor Author

Can you try calling getAsset with the field as the second argument?
See https://github.com/netlify/netlify-cms/blob/a690e467fa332cca47961f8e671abd53698dfadb/packages/netlify-cms-widget-image/src/ImagePreview.js#L14

If that doesn't work please open a new issue. Field based media/public folders are quite new and not documented properly yet

@gtitov
Copy link

gtitov commented Nov 1, 2020

Maybe I'm doing something wrong but I have a real struggle with this one.

I want my post to have one cover that is stored in assets so Hugo is able to resize it. Also my post have multiple images inside text (markdown widget) that I want to be stored in static so I can add these with the UI. Hope it's clear: cover in assets to resize, images inside text in static to add them without any code.

I came up with this config.yml:

backend:
  name: git-gateway
  branch: master
media_folder: 'static/img/blog'
public_folder: '/img/blog'
collections:
  - name: 'blog'
    label: 'Posts'
    folder: 'content/blog'
    create: true
    slug: '{{year}}{{month}}{{day}}{{hour}}{{minute}}{{second}}'
    editor:
      preview: true
    fields:
      - label: 'Title'
        name: 'title'
        widget: 'string'
      - label: 'Cover'
        name: 'image'
        widget: 'image'
        media_folder: '/assets/covers'
        public_folder: '/covers'
        allow_multiple: false
      - label: 'Post'
        name: 'body'
        widget: 'markdown'

Paths to files appears as I expect them to, like that:

---
title: Test
date: 2020-11-01T19:40:17.107Z
author: gman
image: /covers/lighthouse.jpg
---
Post body
![Sample](/img/blog/desert.jpg)

But the cover is stored in static/img/blog/lighthouse.jpg not in assets/covers/lighthouse.jpg.

So it looks like I can't set field based media folder... Is it even possible to do it as I described? Or I didn't understand docs right? Or I did something wrong?

Here is the repo if needed

@erezrokah
Copy link
Contributor Author

Hi @gtitov can you please open a new issue with the information you shared here?

@gtitov
Copy link

gtitov commented Nov 2, 2020

Done!
#4529

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
6 participants