Skip to content

Commit

Permalink
Merge branch 'master' into change-image-ref
Browse files Browse the repository at this point in the history
  • Loading branch information
gwyneplaine authored May 3, 2021
2 parents cbac7b5 + b7aeb23 commit 51ed701
Show file tree
Hide file tree
Showing 42 changed files with 1,592 additions and 713 deletions.
5 changes: 5 additions & 0 deletions .changeset/afraid-maps-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/admin-ui': minor
---

Added symlink logic for file storage.
5 changes: 5 additions & 0 deletions .changeset/dirty-bananas-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/fields': minor
---

Added file field type.
5 changes: 5 additions & 0 deletions .changeset/silent-stingrays-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/utils-legacy': minor
---

Added `getFileRef` and `parseFileRef` to exported utilities.
5 changes: 5 additions & 0 deletions .changeset/six-dodos-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/api-tests-legacy': minor
---

Added beforeEach and afterEach hooks to test suite, added file config defaults.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ dist/

# Used by the image field type tests as a storage target
tmp_test_images/
tmp_test_files/

# Created by https://www.gitignore.io/api/vim
# Edit at https://www.gitignore.io/?templates=vim
Expand Down
28 changes: 28 additions & 0 deletions docs/pages/apis/config.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,34 @@ export default config({

See the [schema extension guide](../guides/schema-extension) for more details on how to use `graphQLSchemaExtension()` to extend your GraphQL API.

## files

Keystone supports file handling via the [`file`](./fields#file) field type.
In order to use this field type you need to configure Keystone with information about where your files will be stored and served from.
At the moment Keystone supports storing files on the local filesystem, and is agnostic about how files are served.

```typescript
import { config } from '@keystone-next/keystone/schema';

export default config({
files: {
upload: 'local',
local: {
storagePath: 'public/files',
baseUrl: '/files',
},
}
/* ... */
});
```

Options:

- `upload`: The storage target when uploading files. Currently only `local` is supported.
- `local`: Configuration options when using the `local` storage target.
- `storagePath`: The path local files are uploaded to.
- `baseUrl`: The base of the URL local files will be served from, outside of keystone.

## images

Keystone supports image handling via the [`image`](./fields#image) field type.
Expand Down
28 changes: 27 additions & 1 deletion docs/pages/apis/fields.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,33 @@ File types allow you to upload different types of files to your Keystone system.

### file

(coming soon)
A `file` field represents a file of any type.

See [`config.files`](./config#files) for details on how to configure your Keystone system with support for the `file` field type.

Options:

- `isRequired` (default: `false`): If `true` then this field can never be set to `null`.

```typescript
import { config, createSchema, list } from '@keystone-next/keystone/schema';
import { file } from '@keystone-next/fields';

export default config({
lists: createSchema({
ListName: list({
fields: {
repo: file({
isRequired: true,
}),
/* ... */
},
}),
/* ... */
}),
/* ... */
});
```

### image

Expand Down
1 change: 1 addition & 0 deletions examples/basic/keystone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export default auth.withAuth(
// isAccessAllowed,
},
images: { upload: 'local' },
files: { upload: 'local' },
lists,
extendGraphqlSchema,
session: withItemData(
Expand Down
3 changes: 3 additions & 0 deletions examples/basic/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
select,
virtual,
image,
file,
} from '@keystone-next/fields';
import { document } from '@keystone-next/fields-document';
// import { cloudinaryImage } from '@keystone-next/cloudinary';
Expand Down Expand Up @@ -43,7 +44,9 @@ export const lists = createSchema({
name: text({ isRequired: true }),
/** Email is used to log into the system. */
email: text({ isRequired: true, isUnique: true }),
/** Avatar upload for the users profile, stored locally */
avatar: image(),
attachment: file(),
/** Used to log in. */
password: password(),
/** Administrators have more access to various lists and fields. */
Expand Down
13 changes: 11 additions & 2 deletions packages-next/admin-ui/src/system/generateAdminUI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,24 @@ export const generateAdminUI = async (
) => {
// Nuke any existing files in our target directory
await fs.remove(projectAdminPath);
const publicDirectory = Path.join(projectAdminPath, 'public');

if (config.images) {
const publicDirectory = Path.join(projectAdminPath, 'public');
if (config.images || config.files) {
await fs.mkdir(publicDirectory, { recursive: true });
}

if (config.images) {
const storagePath = Path.resolve(config.images.local?.storagePath ?? './public/images');
await fs.mkdir(storagePath, { recursive: true });
await fs.symlink(storagePath, Path.join(publicDirectory, 'images'), 'junction');
}

if (config.files) {
const storagePath = Path.resolve(config.files.local?.storagePath ?? './public/files');
await fs.mkdir(storagePath, { recursive: true });
await fs.symlink(storagePath, Path.join(publicDirectory, 'files'), 'junction');
}

// Write out the files configured by the user
const userPages = config.ui?.getAdditionalFiles?.map(x => x(config)) ?? [];
const userFilesToWrite = (await Promise.all(userPages)).flat();
Expand Down
15 changes: 8 additions & 7 deletions packages-next/fields/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
export { autoIncrement } from './types/autoIncrement';
export { checkbox } from './types/checkbox';
export { decimal } from './types/decimal';
export { file } from './types/file';
export { float } from './types/float';
export { integer } from './types/integer';
export { image } from './types/image';
export { password } from './types/password';
export { relationship } from './types/relationship';
export { select } from './types/select';
export { text } from './types/text';
export { password } from './types/password';
export { timestamp } from './types/timestamp';
export { integer } from './types/integer';
export { float } from './types/float';
export { decimal } from './types/decimal';
export { autoIncrement } from './types/autoIncrement';
export { select } from './types/select';
export { virtual } from './types/virtual';
export { Implementation } from './Implementation';
export type { FieldConfigArgs, FieldExtraArgs } from './Implementation';
export { image } from './types/image';
Loading

0 comments on commit 51ed701

Please sign in to comment.