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

Per Collection Media Folder , Images Not Deleted When Post Is Deleted #4069

Closed
goldcoders opened this issue Jul 25, 2020 · 6 comments
Closed
Labels
duplicate status: confirmed type: bug code to address defects in shipped code

Comments

@goldcoders
Copy link

goldcoders commented Jul 25, 2020

Per Collection Media Folder Always leave the Images Not Deleted...
Therefore It Remained in the Folder and only the index.md is deleted when you delete a post entry.

My Config
- name: "blog"
    identifier_field: title
    label: "Blog"
    label_singular: "Post"
    folder: "exampleSite/content/blog"
    filter: { field: "index", value: false }
    path: "{{slug}}/index" # used for bundled post
    create: true
    media_folder: "resources"
    public_folder: "blog/{{slug}}/resources"
    # show_preview_links must be set to true show preview_path
    preview_path: "blog/{{fields.slug}}"
    editor:
      preview: false
    fields:
      - { label: "Publish Date", name: "date", widget: "datetime" }
      - { label: "Draft", name: "draft", widget: "boolean", default: false }
      - { label: "Title", name: "title", widget: "string" }
      - { label: "Slug", name: "slug", widget: "hidden" }
      - { label: "Description", name: "description", widget: "string" }
      - { label: "Featured Image", name: "featured_image", widget: image }
      - label: Resources
        name: resources
        widget: list
        required: false
        fields:
          - label: "Image"
            name: "src"
            widget: "image"
          - label: "Name"
            name: "name"
            widget: "string"
      - { label: "Content", name: "body", widget: "markdown" }

Is there a possibility to use the CMS EVENTS ?

is there a onDelete event where in you can ask specify to delete also the folder and the image

@erezrokah erezrokah added type: bug code to address defects in shipped code status: confirmed duplicate labels Jul 26, 2020
@erezrokah
Copy link
Contributor

Closing this as duplicate of #3615

There are no events yet on deletion yet and probably it's better just to fix the bug.
We just need to think of a good strategy to delete those files without breaking other content that might be in the same directory or using these images.

@erezrokah
Copy link
Contributor

There are no events yet on deletion yet and probably it's better just to fix the bug.

Sorry, my mistake here. There are preUnpublish and postUnpublish events:
https://www.netlifycms.org/docs/beta-features/#registering-to-cms-events

However, you'd need to read the access token from local storage and use the git service API to inside the event handler to delete those files.

@goldcoders
Copy link
Author

lets say i get the access token in local staroge , how would that help me make git hub api call?
as far as i know the token stored is from netlify identity under gotrue.user key....

Im just using the cms in local, yeah im using netlify-cms-proxy, so I dont need github api call...
is there a quick and dirty way to tell netlify cms to recursively delete the collection folder?
right now its just deleting the file index.md , it is treating the collection as a single file

@erezrokah
Copy link
Contributor

The token from gotrue.user can be used to access git-gateway directly (that's what the CMS uses to access the repo).
With netlify-cms-proxy you could send a request to the local proxy server to delete a file (you can see those requests in the browser traffic when you delete an entry).

The way the CMS deletes files is not exposed as an API, so the quick and dirty way is described above

@goldcoders
Copy link
Author

hi can you give an example how to achieve this, i really wanna implement removing the images

there is a deleteMedia commit type , the available template tags are path, author-login, author-name

CMS.registerEventListener({
  name: 'postUnpublish',
  handler: ({ author, entry,path }) => console.log(path,JSON.stringify({ author, data: entry.get('data') })),
});

what are the available method with entry? im thinking if there is set and get, would there be also delete?

@goldcoders
Copy link
Author

Ive read the code , there is no deleteFolder only deleteFiles...

This is ok on normal set up , but if you manage per folder page collection...

then this wont really work, there are artifacts left like images, and the folder itself...
only the index.md file is remove ,

I mean i can add to path all the images , since i can get it in resources key
then loop on and get the link then push it to paths array....

but thats it, i still have an artifacts left an empty folder which should be delete...

I wish there is an easier solution to this by adding deleteFolder when
Per Collection Media Folder is set on the config...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate status: confirmed type: bug code to address defects in shipped code
Projects
None yet
Development

No branches or pull requests

2 participants