Code Monkey home page Code Monkey logo

ghost-purge-images's Introduction

ghost-purge-images

Display or clean unused uploaded files: images, documents, audio, video & thumbnail files of your Ghost blog

๐ŸŽ Versions compatibility

Which Ghost version are you running?

Ghost version ghost-purge-images version How to install
3, 4, 5 latest npm install -g ghost-purge-images
>= 2.16 2.1.1 npm install -g [email protected]
<= 2.15.x 2.0.3 npm install -g [email protected]
0.7.2 - 1.x 1.0.0 npm install -g [email protected]

โœจ Update to the last version

In your blog server, login via ssh and run:

npm install -g ghost-purge-images

๐Ÿ’ก This command will install or update ghost-purge-images globally in your server, it doesn't matter in which folder you run it

๐Ÿค“ Observations

  • Due Content & Admin API, some images associated to the next resouces will be removed:
    • Authors with zero published posts

    • Tags that are not associated with a post

    • Drafts (both posts/pages) will be removed too if you are not running Ghost v2.16 or newer

      ๐Ÿ’ก Why? Drafts are only available in Ghost Admin API included in Ghost 2.16

  • Backup your content folder first!

๐Ÿ”‘ Keys

This tool use:

Get the keys following this steps:

  • On your Ghost admin, click on Integrations at the left menu
  • Click on Add custom integration and set any name
  • Copy the Content API & Admin API Keys to use them

๐Ÿ’ก Need help? Check this step-by-step guide to create them

๐Ÿš€ Execute

Login into your server via ssh:

  • Where you have installed Ghost, usually:

cd /var/www/ghost

  • Run a command, for example:

ghost-purge-images display --content-key=YOUR_CONTENT_KEY --admin-key=YOUR_ADMIN_KEY

โšก๏ธ Commands

display

Print the list of all uploaded images that currently are not being used in any post, page or metadata

  • Example: ghost-purge-images display --content-key=YOUR_CONTENT_KEY --admin-key=YOUR_ADMIN_KEY

ghost-purge-images display screenshot

purge

Delete all the unused files.

โš ๏ธ WARNING: Take a backup before run this

  • Example: ghost-purge-images purge --content-key=YOUR_CONTENT_KEY --admin-key=YOUR_ADMIN_KEY

ghost-purge-images purge screenshot

๐Ÿงฉ Optional Params

  • url

๐Ÿ’ก By default, the URL in config.production.json will be used. This may be useful for Docker or custom configurations

Example: ... --url=https://yourblog.com/

  • images-path

๐Ÿ’ก The default images path is content/images

You can change it with the optional param ... --images-path=your/custom/path/here

  • media-path

๐Ÿ’ก The default media path is content/media

You can change it with the optional param ... --media-path=your/custom/path/here

  • exclude-ext

๐Ÿ’ก No default value, all extensions are included

You can exclude one or more file extensions. Examples:

  • Excluding .webp files, adding the optional param ... --exclude-ext=webp
  • Excluding .jpg and .gif files (add a comma), adding the optional param ... --exclude-ext=jpg,gif

๐ŸŽฏ Troubleshooting

Error: EACCES: permission denied, unlink ANY_FILE_PATH

Permissions issue on your server, try to run in your Ghost folder:

sudo chmod -R a+rw content

๐Ÿ“‹ Changelog

See CHANGELOG.md

๐Ÿ‘‹ Credits

David Burgos from Ghostboard.io

๐Ÿ‘ Contributors

โœ’๏ธ License

See LICENSE file

ghost-purge-images's People

Contributors

dburgos avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

ghost-purge-images's Issues

Delete profile images from unpublished authors

If there are no published posts by an author, then the author's profile and cover photos are deleted.

I have a draft that ready to be published by a new author. The script deleted their profile and cover iamges.

display command shows all images to be removed โ˜ ๏ธ

ghost-purge-images v3.0.3
Ghost version: v3.6
Reported by @Caucasus-and-Mercury
Related issue #6

display command shows all uploaded images to be removed
I can't reproduce it with Ghost 3.3.0 & ghost-purge-images 3.0.3
Need to investigate, the issue may be related to the API endpoint

- /var/www/ghost/content/images/2019/04/siberiannationalcostumes_yakutiarussia_01.jpg (0.09 MB)
- /var/www/ghost/content/images/2019/04/vladimir-kolodenko---aeroport.JPG (0.04 MB)

๐Ÿ“Š Summary:
- 2771 files of 2771 uploaded images (100.00%)
- Total space: 969.57MB

โ” Want to delete this files? Run `ghost-purge-images purge key=YOUR_KEY`
๐ŸŽ Open source tool by David Burgos from https://ghostboard.io
ghost-mgr@ghost-caucasusandmercury:/var/www/ghost$ 

Compatibility with V5

Is this compatible with Ghost v5?
I haven't upgraded yet but just curious if this has been looked into before I investigate.
Thanks

Ghost Cloud Storage

When using a storage adapter in Ghost, the purge script reports :

Completed but nothing to do here...
Congrats, all images are being used ๐Ÿ‘

Regards.

Timeout Error

Hi :)

I generated my API keys but I get the following error: โŒ Error: Error: connect ETIMEDOUT MyServerIPAddress:443

Thx

Dependencies warnings on install

npm WARN deprecated [email protected]: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated [email protected]: this library is no longer supported
npm WARN deprecated [email protected]: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.

Ignore Webp files

Hi,

Thank-you for this script that saves my life ! :-)
All my images are also compressed in Webp when they are uploaded in jpg.

I would like to avoid the script to remove all the used Webp images that are indirectly used because the original file is still use by a content.

Do you known how to implement this rules quickly ?

Thank-you,

Doesn't work for me...

Sorry, still doesn't work for me. I'm following the instructions very very carefully. I'm getting this:

root@ghost-caucasusandmercury:~# cd /var/www/ghost
root@ghost-caucasusandmercury:/var/www/ghost# ghost-purge-images display --content-key=69891a65807dc1931c98406ded --admin-key=5cefed645c7944048554b2d5:b50084fd0cdda8fa7cb93fb2f17c2537cd294aa1f540b3f307ca6dbedc48da8b
๐Ÿ”‘ Required parameter CONTENT_KEY is missing!
๐Ÿ”‘ Required parameter ADMIN_KEY is missing!
โ“ Need a key?

  • On your Ghost admin > Integrations > Create custom integration
  • Copy Content API & Admin API Keys

On a side note, I'm having Ghost-CLI version: 1.9.9 as I'm unable to update it for some reason.

Any idea?

โŒ Error: API Pages - Not valid response

Ghost: 3.33.0 - "Official" docker image

Docker file:

FROM ghost:latest                                                                                                                                                                
USER root                                                                                                                                                                        
RUN npm install -g --production --loglevel verbose --no-save ghost-purge-images                                                                                                  

Command issued inside the container (docker exec...)
Command output:

โŒ Error: API Pages - Not valid response

Ghost log:

app_1  | [2020-09-15 16:55:43] INFO "GET /ghost/api/v3/content/authors/?limit=100&page=1&fields=cover_image,profile_image&key=03978e1fdea21f903d8e481c65" 301 3ms
app_1  | [2020-09-15 16:55:43] INFO "GET /ghost/api/v3/content/authors/?limit=100&page=1&fields=cover_image%2Cprofile_image&key=03978e1fdea21f903d8e481c65" 200 18ms
app_1  | [2020-09-15 16:55:43] INFO "GET /ghost/api/v2/admin/pages/?limit=100&page=1&formats=html,mobiledoc" 301 5ms
app_1  | [2020-09-15 16:55:43] ERROR "GET /ghost/api/v2/admin/pages/?limit=100&page=1&formats=html%2Cmobiledoc" 403 5ms
app_1  | 
app_1  | Authorization failed
app_1  | 
app_1  | "Unable to determine the authenticated user or integration. Check that cookies are being passed through if using session authentication."
app_1  | 
app_1  | Error ID:
app_1  |     4b0cab90-f774-11ea-a43c-9570ff40ccb4
app_1  | 
app_1  | ----------------------------------------
app_1  | 
app_1  | NoPermissionError: Authorization failed
app_1  |     at new NoPermissionError (/var/lib/ghost/versions/3.33.0/node_modules/ghost-ignition/lib/errors/index.js:134:23)
app_1  |     at authorizeAdminApi (/var/lib/ghost/versions/3.33.0/core/server/services/auth/authorize.js:28:25)
app_1  |     at Layer.handle [as handle_request] (/var/lib/ghost/versions/3.33.0/node_modules/express/lib/router/layer.js:95:5)
app_1  |     at next (/var/lib/ghost/versions/3.33.0/node_modules/express/lib/router/route.js:137:13)
app_1  |     at authenticate (/var/lib/ghost/versions/3.33.0/core/server/services/auth/session/middleware.js:24:13)
app_1  |     at processTicksAndRejections (internal/process/task_queues.js:97:5)
app_1  | 

Some images from settings are being deleted when they shouldn't

A small issue here:

The following images are being deleted each time I run the script

  • Publication icon (not Publication logo)
  • Twitter card
  • Facebook card

These ones are found in Settings-General

It's not that big an issue, as I'm aware of it I just have to re-upload the images again once I've done image-purge. But anyway, it seems like a small bug to me.

Admin UI integration

Hello there! Love the plugin! It gives me confidence in the platform as a whole to have something like this maintained as it directly addresses the only thing I disllike about ghost... Media management!


Feature: I would like a button in the admin UI to do this as a front end user.

I understand that this is 100% a massive extension of the scope of this project. While I would love for this plugin to evolve into such a thing I do not expect it to be addressed here and am planning my own media library plugin that would leverage this.

Has any one made any efforts to this end?

Is anyone else interested in contributing to this end?

Move instead of delete

It could be great to add a command to move the unused files to another directory instead of remove.

Not quite working, unfortunately -

Hi there, great work on your part, very appreciated for sure.

Upgrading to the latest 3.0 variant, things doesn't quite work. When I try to run it says

ghost-mgr@ghost-caucasusandmercury:~$ ghost-purge-images display --content-key=69891a65807dc1931c98406ded --admin-key=5cefed645c7944048554b2d5:b50084fd0cdda8fa7cb93fb2f17c2537cd294aa1f540b3f307ca6dbedc48da8b
internal/modules/cjs/loader.js:638
    throw err;
    ^

Error: Cannot find module 'jsonwebtoken'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/usr/lib/node_modules/ghost-purge-images/lib/ghost/auth.js:1:13)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
ghost-mgr@ghost-caucasusandmercury:~$ 

before that, when upgrading, I got a message saying:

root@ghost-caucasusandmercury:~# npm install -g ghost-purge-images
/usr/bin/ghost-purge-images -> /usr/lib/node_modules/ghost-purge-images/bin/ghost-purge-images.js
npm WARN notsup Unsupported engine for [email protected]: wanted: {"node":"8.12.0","npm":"6.0.0"} (current: {"node":"10.18.1","npm":"6.13.4"})
npm WARN notsup Not compatible with your version of node/npm: [email protected]
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: [email protected] (node_modules/ghost-purge-images/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
+ [email protected]
added 109 packages from 567 contributors, removed 14 packages and updated 11 packages in 8.765s

And yes, I tried doing the sudo chmod -R a+rw content thing.

...so something about node I guess? Kind regards !

Ghost 3 support

When I run

/var/lib/ghost# ghost-purge-images display --content-key=<myContentKey> --admin-key=<myAdminKey>

in Ghost 3.3.0 I get this error: Error: API Pages - Not valid response

Can someone confirm Ghost 3 is not supported at the moment?

cc @dburgos

Images in use are being deleted

After purging images I had a sense some images had dissapeared/errors. Then I made an experiment, sort of.

I added images to my ghost site by various means. I uploaded images by clicking on the add image variant, and by simply dropping them. I uploaded to galleries, and I dropped many images at once. I also uploaded images the ghost-markdown-card-way. I did it on published posts, on pages and with drafts.

The results are that images are dissapearing that should have been left alone. It seems very random and I can't see a pattern. Some images have been removed in a random way. the only pattern I might possibly see is that on a few occasions where I dropped a lot (10+) pics on the post at once, all had been purged.

So obviously something here is not working. I'm on the digitalocean droplet-variant, latest ghost.

Favicon (Publication Icon)

The active favicon image is always detected not used

๐Ÿ‘‡ Unused images that can be removed:

  • /var/www/site/content/images/2020/12/favicon.png (0.03 MB)
  • /var/www/site/content/images/2020/12/favicon_o.png (0.05 MB)

๐Ÿ“Š Summary:

  • 2 files of 17 uploaded images (11.76%)
  • Total space: 0.08MB

Make it works with Docker based intallation

Hi, whenever I try to make the script works with my docker installation I got an error :

Error reading config.production.json Error: Cannot find module config.production.json
So I tried to BS by creating this file following the official Ghost doc just to indicate the path of my content and then :
Error: TypeError: Cannot read property 'endsWith' of undefined

Have you ever experienced this ?
Thanks

Error 403 for Ghost 5 in Docker

Hi,
I generate the keys and am running this in the container :

CONTENT_KEY=xxxxxxxxxxxxxxxxx
ADMIN_KEY=xxxxxxxxxxxxxxxxxxxx
ghost-purge-images display --content-key=$CONTENT_KEY --admin-key=$ADMIN_KEY

output:
โŒ Error: AxiosError: Request failed with status code 403

ghost log:

Authorization failed

"Unable to determine the authenticated user or integration. Check that cookies are being passed through if using session authentication."

Error ID:
6384b8e0-0502-11ee-8948-e36133aebc76


NoPermissionError: Authorization failed
at authorizeAdminApi (/var/lib/ghost/versions/5.49.3/core/server/services/auth/authorize.js:33:25)
at Layer.handle [as handle_request] (/var/lib/ghost/versions/5.49.3/node_modules/express/lib/router/layer.js:95:5)
at next (/var/lib/ghost/versions/5.49.3/node_modules/express/lib/router/route.js:144:13)
at authenticate (/var/lib/ghost/versions/5.49.3/core/server/services/auth/session/middleware.js:28:13)
at runMicrotasks ()
at processTicksAndRejections (node:internal/process/task_queues:96:5)

[2023-06-07 07:10:31] ERROR "GET /ghost/api/v2/admin/posts/?limit=100&page=1&formats=html%2Cmobiledoc" 403 1ms

Ghost v.5

Thanks for your work
Is Ghost v5 supported?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.