Skip to content

Commit

Permalink
Merge pull request #2 from abhinavr4/master
Browse files Browse the repository at this point in the history
feat(imagekit-adapter): add CI, tests, error handling
  • Loading branch information
nikniv committed May 1, 2023
2 parents a32df68 + 75f17f3 commit 31a7bdb
Show file tree
Hide file tree
Showing 22 changed files with 3,523 additions and 614 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Release

on:
release:
types: [created]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
- name: Test and build
run: npm i -g yarn
yarn install
yarn test
yarn build

publish:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
registry-url: https://registry.npmjs.org/
- name: NPM Publish
run: npm config set //registry.npmjs.org/:_authToken=$NODE_AUTH_TOKEN
npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
17 changes: 17 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Test

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
name: Checkout
- uses: actions/setup-node@v2
with:
node-version: 16
- name: Test and report coverage
run: npm i -g yarn
yarn install
yarn test
6 changes: 6 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"full-trace": true,
"exit": true,
"timeout": 60000,
"require": ["ts-node/register"]
}
9 changes: 9 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.github
.nyc_output
coverage
types
tests
.npmignore
.mocharc.json
.nycrc.json
tsconfig.json
25 changes: 25 additions & 0 deletions .nycrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"reporter": [
"html",
"text",
"lcov",
"cobertura"
],
"extension": [
".ts"
],
"all": false,
"instrument": true,
"sourceMap": true,
"include": [
"src/**/*.ts"
],
"exclude": [
"coverage/**",
"node_modules/**",
"tests/**/*.ts"
],
"require": [
"ts-node/register"
]
}
119 changes: 119 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# ImageKit Ghost Storage

[ImageKit's](https://imagekit.io) [Ghost](https://github.com/TryGhost/Ghost) storage adapter.

### Features

- Up to date with the most recent Ghost release.
- Uses the latest ImageKit [NodeJS SDK](https://github.com/imagekit-developer/imagekit-nodejs/releases).
- Supports image read, existence check, and upload.
- Serve existing images present in Ghost's default storage.
- Ability to upload images into a specific directory.
- Ability to configure uploads in dated subdirectories like the default Ghost local storage adapter in `YYYY/MM` format.
- Use ImageKit's [upload API](https://docs.imagekit.io/api-reference/upload-file-api/server-side-file-upload) to set `tags`, `useUniqueFileName`, and `folder`.

## Installation

### Using yarn

- Go into your Ghost project's root directory.
- Install the adapter:

```bash
yarn add ghost-imagekit-store
cp node_modules/ghost-imagekit-store/dist/src/index.js content/adapters/storage/imagekit/
```

- Configure Ghost's config file.

### Install on Docker

Here's an example of using this adapter with a containerized Ghost:

```Dockerfile
FROM ghost:5-alpine as imagekit
RUN apk add g++ make python3
RUN su-exec node yarn add ghost-imagekit-store

FROM ghost:5-alpine
COPY --chown=node:node --from=imagekit $GHOST_INSTALL/node_modules $GHOST_INSTALL/node_modules
COPY --chown=node:node --from=imagekit $GHOST_INSTALL/node_modules/ghost-imagekit-store/dist/src/index.js $GHOST_INSTALL/content/adapters/storage/imagekit/index.js
# Here, we use the Ghost CLI to set some pre-defined values.
RUN set -ex; \
su-exec node ghost config storage.active imagekit; \
su-exec node ghost config storage.imagekit.uploadOptions.useUniqueFileName true; \
su-exec node ghost config storage.imagekit.uploadOptions.folder /ghost/blog;
```

Make sure the content path is correctly set in the Ghost configuration:

```json
"paths": {
"contentPath": "/var/lib/ghost/content/"
}
```

Ensure that the `GHOST_CONTENT` environment variable is set to the same value as that of `paths.contentPath` in your Ghost app.

## Configuration

Check out [configuration.json.dist](./configuration.json.dist) or below for a complete example.

```json
{
"storage": {
"active": "imagekit",
"imagekit": {
"enableDatedFolders": true,
"auth": {
"privateKey": "your-private-key",
"publicKey": "your-public-key",
"urlEndpoint": "https://ik.imagekit.io/your-imagekit-id"
},
"uploadOptions": {
"useUniqueFileName": false,
"folder": "/sample",
"tags": ["blog"]
}
}
},
"imageOptimization": {
"resize": false
}
}
```

- Make sure [Ghost Image Optimization](https://ghost.org/docs/config/#image-optimisation) is disabled.
- The optional `enableDatedFolders` setting allows uploading images into dated sub-directories (like the default Ghost Local Storage Adapter). It is `true` by default.
- The `auth` property is used to configure your ImageKit account's credentials and URL endpoint. You can find them in the [dashboard](https://imagekit.io/dashboard/developer/api-keys).
- The `uploadOptions` property allows you to configure ImageKit's upload options. You can configure `useUniqueFileName`, `folder`, and `tags`.

### Recommended configuration

- `uploadOptions.useUniqueFileName = true` Ghost's local storage adaptor handles duplicate file names automatically. When uploading a file to a location where a file with the same name already exists, ImageKit creates a new file version. In order to avoid this, `useUniqueFileName` can be set to `true`.
- `uploadOptions.tags = ["travel", "discover"]` if you want to add associations of tags to your uploaded images.
- `uploadOptions.folder = "/ghost/blog"` allows to upload all your images into a specific directory in your ImageKit media library. By default, files are uploaded to the root of media library.

## ImageKit dashboard settings

Ensure that `Restrict unsigned image URLs` settings is turned off in your [ImageKit account](https://imagekit.io/dashboard/settings/images). This is because the storage adapter currently does not support fetching images that require [signed URLs](https://docs.imagekit.io/features/security/signed-urls), including private files.

## Development

To install, run:

```bash
yarn install
```

To run tests and generate coverage, run:

```bash
yarn test
```

To build, run:

```bash
yarn build
```
21 changes: 21 additions & 0 deletions configuration.json.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"storage": {
"active": "imagekit",
"imagekit": {
"enableDatedFolders": true,
"auth": {
"privateKey": "your-private-key",
"publicKey": "your-public-key",
"urlEndpoint": "https://ik.imagekit.io/your-imagekit-id"
},
"uploadOptions": {
"useUniqueFileName": false,
"folder": "/sample",
"tags": ["blog"]
}
}
},
"imageOptimization": {
"resize": false
}
}
Loading

0 comments on commit 31a7bdb

Please sign in to comment.