Keystone-classic: 4.0.0 b5 - keystone.Storage.Adapters.FS - 'remove file' and 'change file' not deleting from disk

Created on 9 Aug 2017  路  6Comments  路  Source: keystonejs/keystone-classic

Expected behavior

Clicking on 'Remove File' or 'Change File' should remove the file from disk.

Actual/Current behavior

Clicking on 'Remove File' or 'Change File' does not remove the file from disk, it only removes the reference in the database.

Steps to reproduce the actual/current behavior

var storage = new keystone.Storage({
    adapter: keystone.Storage.Adapters.FS,
    fs: {
        path: 'public/uploads/',
        publicPath: 'uploads/'
    },
    schema: {
        size: true,
        mimetype: true,
        path: true,
        originalname: true,
        url: true,
    },
});
Post.add({
    image: { type: Types.File, storage: storage }
});

Environment

| Software | Version
| ---------------- | -------
| Keystone | v4.0.0 b5
| Node | v6.10.0

Most helpful comment

@Noviny #4205 is a fix for cloudinary files, not for local file storage. Will that fix handle both?

All 6 comments

7 days and no comment? Anyone? This seems to be a pretty basic requirement

@areknow This project seems to have been abandoned.

Hey, this is a duplicate of #3476 and is fixed by #4205. Once that PR is merged, and a new beta is out, this will be fixed.

@Noviny #4205 is a fix for cloudinary files, not for local file storage. Will that fix handle both?

If someone else has this problem, this is how I fixed it. I am listening to every post request in my Articles model. I compare all local files to ones in database and delete if they don't exist.

const path = require('path');
const fs = require('fs');

Articles.schema.post('save', () => {
    const opts = keystone.options();
    const videosPath = path.join(opts['module root'], opts.static + '/videos');

    keystone
        .list('Articles')
        .model.find()
        .then(articles => {
            const videosInDb = [];
            articles.forEach(item => {
                if (item.video.filename) {
                    videosInDb.push(item.video.filename);
                }
            });

            // Compare local files to files in DB and delete those who don't match
            fs.readdir(videosPath, (err, files) => {
                if (err) throw err;
                files.forEach(function(localFile) {
                    if (videosInDb.indexOf(localFile) == -1) {
                        fs.unlink(`${videosPath}/${localFile}`, err => {
                            if (err) throw err;
                            console.log(`${localFile} was deleted`);
                        });
                    }
                });
            });
        });
});

Expected behavior

Clicking on 'Remove File' or 'Change File' should remove the file from disk.

Actual/Current behavior

Clicking on 'Remove File' or 'Change File' does not remove the file from disk, it only removes the reference in the database.

Steps to reproduce the actual/current behavior

var storage = new keystone.Storage({
  adapter: keystone.Storage.Adapters.FS,
  fs: {
      path: 'public/uploads/',
      publicPath: 'uploads/'
  },
  schema: {
      size: true,
      mimetype: true,
      path: true,
      originalname: true,
      url: true,
  },
});
Post.add({
  image: { type: Types.File, storage: storage }
});

Environment

Software Version
Keystone v4.0.0 b5
Node v6.10.0

have you solved? if so please type here

Was this page helpful?
0 / 5 - 0 ratings

Related issues

josephg picture josephg  路  4Comments

sarmadsangi picture sarmadsangi  路  5Comments

calebmcelroy picture calebmcelroy  路  3Comments

javierpelozo picture javierpelozo  路  5Comments

useralive003 picture useralive003  路  5Comments