You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
934 B
30 lines
934 B
4 years ago
|
import { createWriteStream } from "fs-extra"
|
||
|
import { GraphQLUpload } from "graphql-upload"
|
||
|
import { pipeline } from "stream"
|
||
|
import { Arg, Mutation } from "type-graphql"
|
||
|
import { Upload } from "./FigureResolver/Upload"
|
||
|
import path = require("path")
|
||
|
|
||
|
export class FigureResolver {
|
||
|
@Mutation(() => Boolean)
|
||
|
async uploadFigure(
|
||
|
@Arg("file", () => GraphQLUpload)
|
||
|
{ createReadStream, filename }: Upload
|
||
|
) {
|
||
|
const allowedExt = [".jpg", ".png"]
|
||
|
const ext = path.extname(filename)
|
||
|
|
||
|
if (!allowedExt.includes(ext)) {
|
||
|
throw new Error("wrong extension")
|
||
|
}
|
||
|
|
||
|
return new Promise((resolve, reject) =>
|
||
|
pipeline(
|
||
|
createReadStream() as any,
|
||
|
createWriteStream(__dirname + "/../../uploads/" + filename),
|
||
|
error => (error ? reject(error) : resolve(true))
|
||
|
)
|
||
|
)
|
||
|
}
|
||
|
}
|