Skip to content

Commit b1e9594

Browse files
committed
fix: CreateFile GQL field
1 parent 74c038a commit b1e9594

File tree

16 files changed

+113
-22139
lines changed

16 files changed

+113
-22139
lines changed

packages/webiny-api-files/package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
"license": "MIT",
1616
"dependencies": {
1717
"@babel/runtime": "^7.0.0",
18-
"@commodo/fields": "^0.1.1",
19-
"@commodo/hooks": "^0.0.8",
20-
"@commodo/name": "^0.0.11",
2118
"@svgr/webpack": "^4.1.0",
2219
"bcryptjs": "^2.4.3",
2320
"graphql-shield": "^5.1.0",
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// @flow
2+
import { Entity } from "webiny-entity";
3+
4+
export interface IFile extends Entity {
5+
createdBy: ?Entity;
6+
src: string;
7+
description: string;
8+
name: string;
9+
tags: Array<string>;
10+
}
11+
12+
export function fileFactory(context: Object): Class<IFile> {
13+
return class File extends Entity {
14+
static classId = "File";
15+
16+
createdBy: ?Entity;
17+
src: string;
18+
description: string;
19+
name: string;
20+
type: string;
21+
tags: Array<string>;
22+
constructor() {
23+
super();
24+
25+
const { user = {}, security, files } = context;
26+
const { User } = security.entities;
27+
28+
this.attr("createdBy")
29+
.entity(User)
30+
.setSkipOnPopulate();
31+
32+
this.attr("size")
33+
.integer()
34+
.setValidators("required");
35+
this.attr("type")
36+
.char()
37+
.setValidators("required,maxLength:50");
38+
this.attr("src")
39+
.char()
40+
.setValidators("required,maxLength:200");
41+
this.attr("name")
42+
.char()
43+
.setValidators("required,maxLength:100");
44+
this.attr("tags")
45+
.array()
46+
.onSet(value => {
47+
if (Array.isArray(value)) {
48+
return value.map(item => item.toLowerCase());
49+
}
50+
51+
return value;
52+
})
53+
.setValidators(tags => {
54+
if (Array.isArray(tags)) {
55+
if (tags.length > 15) {
56+
throw Error("You cannot set more than 15 tags.");
57+
}
58+
59+
for (let i = 0; i < tags.length; i++) {
60+
let tag = tags[i];
61+
if (typeof tag !== "string") {
62+
throw Error("Tag must be typeof string.");
63+
}
64+
65+
if (tag.length > 50) {
66+
throw Error(`Tag ${tag} is more than 50 characters long.`);
67+
}
68+
}
69+
}
70+
});
71+
72+
this.on("beforeCreate", async () => {
73+
if (!this.src.startsWith("/") || this.src.startsWith("http")) {
74+
throw Error(
75+
`File "src" must be a relative path, starting with forward slash ("/").`
76+
);
77+
}
78+
79+
if (await files.entities.File.findOne({ query: { src: this.src } })) {
80+
throw Error(`File "src" must be unique. `);
81+
}
82+
83+
this.createdBy = user.id;
84+
});
85+
}
86+
};
87+
}

packages/webiny-api-files/src/entities/File.model.js

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
// @flow
2-
export { File } from "webiny-api-files/entities/File.model";
2+
export { fileFactory } from "webiny-api-files/entities/File.entity";
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// @flow
2+
import { type EntityPluginType } from "webiny-api/types";
3+
import * as entities from "webiny-api-files/entities";
4+
5+
const file: EntityPluginType = {
6+
name: "entity-files-file",
7+
type: "entity",
8+
namespace: "files",
9+
entity: {
10+
name: "File",
11+
factory: entities.fileFactory
12+
}
13+
};
14+
15+
export default [file];

packages/webiny-api-files/src/plugins/graphql/file.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import listFiles from "./resolvers/listFiles";
55
import listTags from "./resolvers/listTags";
66
import updateFileBySrc from "./resolvers/updateFileBySrc";
77

8-
const fileFetcher = ctx => ctx.models.File;
8+
const fileFetcher = ctx => ctx.files.entities.File;
99

1010
import {
1111
FileType,

packages/webiny-api-files/src/plugins/graphql/resolvers/listFiles.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// @flow
22
import { ListResponse } from "webiny-api/graphql/responses";
33

4-
export default modelFetcher => async (root: any, args: Object, context: Object) => {
5-
const model = modelFetcher(context);
4+
export default (entityFetcher: Function) => async (root: any, args: Object, context: Object) => {
5+
const entityClass = entityFetcher(context);
66
const { page = 1, perPage = 10, sort = null, search = "", types = [], tags = [] } = args;
77
const findArgs = { page, perPage, sort };
88

@@ -30,6 +30,6 @@ export default modelFetcher => async (root: any, args: Object, context: Object)
3030
findArgs.query = { $and };
3131
}
3232

33-
const data = await model.find(findArgs);
33+
const data = await entityClass.find(findArgs);
3434
return new ListResponse(data, data.getMeta());
3535
};

packages/webiny-api-files/src/plugins/graphql/resolvers/listTags.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default (modelFetcher: ModelFetcher) => async (root: any, args: Object, c
77
const model = modelFetcher(context);
88

99
const results = await model
10-
.getStorageDriver()
10+
.getDriver()
1111
.getDatabase()
1212
.collection("File")
1313
.aggregate([
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @flow
2-
import models from "./models";
2+
import entities from "./entities";
33
import graphql from "./graphql";
44

5-
export default [models, graphql];
5+
export default [entities, graphql];

packages/webiny-api-files/src/plugins/models.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)