Skip to content

Instantly share code, notes, and snippets.

@colinmeinke
Created September 30, 2016 11:34
Show Gist options
  • Save colinmeinke/978812f69bc9ea83390b231f8889318e to your computer and use it in GitHub Desktop.
Save colinmeinke/978812f69bc9ea83390b231f8889318e to your computer and use it in GitHub Desktop.
Hard-coded writing to local file in Ghost

Storage adapters

Problem

Currently Ghost writes to the local file system for:

  • Creation of database backups
  • Uploading themes (currently hard-coded to use the local file storage adapter)
  • Unzipping directories

This isn't ideal as the user may not have the permissions to do this (in the case of immutable hosts such as Now), or these changes may be lost on server restart (in the case of ephemeral hosts such as Heroku).

Locations

These are taken from the master branch.

fs.writeFile

Is used in the writeExportFile function of core/server/data/migration/backup.js.

fs.createWriteStream

Is used in the zipFolder function of core/server/utils/zip-folder.js.

fs.delete

Is used in the cleanUp function of core/server/data/importer/index.js.

extract-zip-fork

Is used in the extractZip function of core/server/data/importer/index.js.

Solution

The ideal solution is to allow the user to define storage adapters for all tasks that manipulate the local file system.

This is partially implemented, but currently only for images, and database.

The storage adapter system still needs to be applied to:

  • Database backups
  • Themes (currently hard-coded to use the local file storage adapter)
  • Logging (@kirrg001 is currently working on this)
  • Apps (currently only setup to read from app path?)
  • Temporary storage?

It might be nice to abstract storage adapters. For example, a file storage adapter shouldn't care about what type of file it is writing/reading. It should only know how to write/read/delete a file.

One idea would be something like @vdemedes' suggestion. This would allow us to define the same storage adapter for multiple tasks.

storage: {
  active: {
    apps: 's3',
    backups: 's3',
    images: 's3',
    logs: 'loggly',
    themes: 's3',
  },
  loggly: {},
  s3: {
    rootDir: {
      apps: 'apps',
      backups: 'backups',
      images: 'images',
      themes: 'themes',
    },
  },
}

I also agree that we should remove the responsibility of serving assets from storage adapters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment