Skip to content
This repository has been archived by the owner on Nov 29, 2021. It is now read-only.

Commit

Permalink
feat: wip: upload file with graphql and azure
Browse files Browse the repository at this point in the history
  • Loading branch information
YanceyOfficial committed Sep 4, 2021
1 parent 75dc0d0 commit 8843422
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 1 deletion.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"dotenv": "^10.0.0",
"express-rate-limit": "^5.2.6",
"graphql": "^15.5.1",
"graphql-upload": "^12.0.0",
"helmet": "^4.4.1",
"joi": "^17.4.0",
"luxon": "^1.26.0",
Expand Down Expand Up @@ -80,6 +81,7 @@
"@types/bcrypt": "^5.0.0",
"@types/csurf": "^1.11.0",
"@types/express-rate-limit": "^5.1.3",
"@types/graphql-upload": "^8.0.7",
"@types/jest": "^26.0.20",
"@types/luxon": "^1.27.1",
"@types/morgan": "^1.9.2",
Expand Down
4 changes: 4 additions & 0 deletions schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ type Mutation {
updateUserName(username: String!): UserModel!
updateEmail(email: String!): UserModel!
deleteAccount: UserModel!
uploadFile(file: Upload!): Boolean!
createAnnouncement(input: CreateAnnouncementInput!): AnnouncementModel!
updateAnnouncementById(input: UpdateAnnouncementInput!): AnnouncementModel!
exchangePositionAnnouncement(input: ExchangePositionInput!): [AnnouncementModel!]!
Expand Down Expand Up @@ -464,6 +465,9 @@ input UpdateUserInput {
avatarUrl: String
}

"""The `Upload` scalar type represents a file upload."""
scalar Upload

input CreateAnnouncementInput {
content: String!
}
Expand Down
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { DataBaseModule } from './database/database.module'
import { GraphqlModule } from './graphql/graphqls.module'
import { AuthModule } from './auth/auth.module'
import { UploadersModule } from './uploaders/uploaders.module'
import { UploadersModule as UploadersGraphQLModule } from './uploaders-graphql/uploaders.module'
import { UsersModule } from './users/users.module'
import { AnnouncementsModule } from './announcements/announcements.module'
import { SMSModule } from './sms/sms.module'
Expand All @@ -32,6 +33,7 @@ import { WinstonLogModule } from './shared/log/log.module'
DataBaseModule,
AuthModule,
UploadersModule,
UploadersGraphQLModule,
UsersModule,
AnnouncementsModule,
SMSModule,
Expand Down
10 changes: 10 additions & 0 deletions src/uploaders-graphql/dtos/create-motto.input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { InputType, Field } from '@nestjs/graphql'
import { IsString, IsNotEmpty, IsNumber } from 'class-validator'

@InputType()
export class CreateUploaderInput {
@Field()
@IsString()
@IsNotEmpty()
public readonly content: string
}
15 changes: 15 additions & 0 deletions src/uploaders-graphql/dtos/update-motto.input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { InputType, Field } from '@nestjs/graphql'
import { IsString, IsNotEmpty } from 'class-validator'

@InputType()
export class UpdateUploaderInput {
@Field()
@IsString()
@IsNotEmpty()
public readonly id: string

@Field()
@IsString()
@IsNotEmpty()
public readonly content: string
}
9 changes: 9 additions & 0 deletions src/uploaders-graphql/interfaces/uploaders.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Document } from 'mongoose'

export interface Uploader extends Document {
readonly _id: string
readonly content: string
readonly weight: number
readonly createdAt: Date
readonly updatedAt: Date
}
19 changes: 19 additions & 0 deletions src/uploaders-graphql/models/uploaders.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Field, ObjectType } from '@nestjs/graphql'

@ObjectType()
export class UploaderModel {
@Field({ nullable: false })
public _id: string

@Field({ nullable: false })
public content: string

@Field({ nullable: false })
public weight: number

@Field({ nullable: false })
public createdAt: Date

@Field({ nullable: false })
public updatedAt: Date
}
8 changes: 8 additions & 0 deletions src/uploaders-graphql/uploaders.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Module } from '@nestjs/common'
import { UploadersResolver } from './uploaders.resolver'
import { UploadersService } from './uploaders.service'

@Module({
providers: [UploadersService, UploadersResolver],
})
export class UploadersModule {}
22 changes: 22 additions & 0 deletions src/uploaders-graphql/uploaders.resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { UseGuards } from '@nestjs/common'
import { Args, Resolver, Mutation } from '@nestjs/graphql'
import { GraphQLUpload, FileUpload } from 'graphql-upload'
import { UploadersService } from './uploaders.service'
import { UploaderModel } from './models/uploaders.model'
import { GqlAuthGuard } from '../shared/guard/gqlAuth.guard'

@Resolver(() => UploaderModel)
export class UploadersResolver {
constructor(private readonly uploadersService: UploadersService) {
this.uploadersService = uploadersService
}

@Mutation(() => Boolean)
// @UseGuards(GqlAuthGuard)
async uploadFile(
@Args({ name: 'file', type: () => GraphQLUpload })
file: FileUpload,
): Promise<boolean> {
return this.uploadersService.upload(file)
}
}
28 changes: 28 additions & 0 deletions src/uploaders-graphql/uploaders.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { createWriteStream } from 'fs'
import { Injectable } from '@nestjs/common'
import { GraphQLUpload, FileUpload } from 'graphql-upload'
import { BlobServiceClient, ContainerClient } from '@azure/storage-blob'
import { ConfigService } from '../config/config.service'
import { AZURE_STORAGE_CONTAINER_NAME } from '../shared/constants'

@Injectable()
export class UploadersService {
private readonly containerClient: ContainerClient

constructor(configService: ConfigService) {
const blobServiceClient = BlobServiceClient.fromConnectionString(
configService.getAzureStorageConnectionString(),
)

this.containerClient = blobServiceClient.getContainerClient(AZURE_STORAGE_CONTAINER_NAME)
}

public async upload(file: FileUpload) {
const { filename, createReadStream } = file
console.log(file)
const blockBlobClient = this.containerClient.getBlockBlobClient(filename)
// const uploadBlobResponse = await blockBlobClient.upload(buffer, size)

return true
}
}
43 changes: 42 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1463,6 +1463,16 @@
dependencies:
"@types/node" "*"

"@types/graphql-upload@^8.0.7":
version "8.0.7"
resolved "https://registry.npmjs.org/@types/graphql-upload/-/graphql-upload-8.0.7.tgz#71dd5d4a8d9ddb598df91298d6e98a943061b255"
integrity sha512-uXhInuUY/W6n9a+PdCt9vcZ7z2m+NzByBJFvvDM+46pljqEwXXnIAjsEI1Dka2FKRTGthetm/imN//RhtEEYSA==
dependencies:
"@types/express" "*"
"@types/fs-capacitor" "*"
"@types/koa" "*"
graphql "^15.3.0"

"@types/http-assert@*":
version "1.5.1"
resolved "https://registry.yarnpkg.com/@types/http-assert/-/http-assert-1.5.1.tgz#d775e93630c2469c2f980fc27e3143240335db3b"
Expand Down Expand Up @@ -4933,6 +4943,11 @@ fs-capacitor@^2.0.4:
resolved "https://registry.yarnpkg.com/fs-capacitor/-/fs-capacitor-2.0.4.tgz#5a22e72d40ae5078b4fe64fe4d08c0d3fc88ad3c"
integrity sha512-8S4f4WsCryNw2mJJchi46YgB6CR5Ze+4L1h8ewl9tEpL4SJ3ZO+c/bS4BWhB8bK+O3TMqhuZarTitd0S0eh2pA==

fs-capacitor@^6.2.0:
version "6.2.0"
resolved "https://registry.npmjs.org/fs-capacitor/-/fs-capacitor-6.2.0.tgz#fa79ac6576629163cb84561995602d8999afb7f5"
integrity sha512-nKcE1UduoSKX27NSZlg879LdQc94OtbOsEmKMN2MBNudXREvijRKx2GEBsTMTfws+BrbkJoEuynbGSVRSpauvw==

[email protected], fs-extra@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-8.1.0.tgz#49d43c45a88cd9677668cb7be1b46efdb8d2e1c0"
Expand Down Expand Up @@ -5252,6 +5267,22 @@ graphql-tools@^4.0.8:
iterall "^1.1.3"
uuid "^3.1.0"

graphql-upload@^12.0.0:
version "12.0.0"
resolved "https://registry.npmjs.org/graphql-upload/-/graphql-upload-12.0.0.tgz#2351d20d294e920fb25d2eba9f7c352e37a1a02b"
integrity sha512-ovZ3Q7sZ17Bmn8tYl22MfrpNR7nYM/DUszXWgkue7SFIlI9jtqszHAli8id8ZcnGBc9GF0gUTNSskYWW+5aNNQ==
dependencies:
busboy "^0.3.1"
fs-capacitor "^6.2.0"
http-errors "^1.8.0"
isobject "^4.0.0"
object-path "^0.11.5"

graphql@^15.3.0:
version "15.5.2"
resolved "https://registry.npmjs.org/graphql/-/graphql-15.5.2.tgz#efa19f8f2bf1a48eb7d5c85bf17e144ba8bb0480"
integrity sha512-dZjLPWNQqYv0dqV2RNbiFed0LtSp6yd4jchsDGnuhDKa9OQHJYCfovaOEvY91w9gqbYO7Se9LKDTl3xxYva/3w==

graphql@^15.5.1:
version "15.5.1"
resolved "https://registry.yarnpkg.com/graphql/-/graphql-15.5.1.tgz#f2f84415d8985e7b84731e7f3536f8bb9d383aad"
Expand Down Expand Up @@ -5405,7 +5436,7 @@ [email protected], http-errors@~1.7.2:
statuses ">= 1.5.0 < 2"
toidentifier "1.0.0"

http-errors@^1.7.3:
http-errors@^1.7.3, http-errors@^1.8.0:
version "1.8.0"
resolved "https://registry.yarnpkg.com/http-errors/-/http-errors-1.8.0.tgz#75d1bbe497e1044f51e4ee9e704a62f28d336507"
integrity sha512-4I8r0C5JDhT5VkvI47QktDW75rNlGVsUf/8hzjCC/wkWI/jdTRmBb9aI7erSG82r1bjKY3F6k28WnsVxB1C73A==
Expand Down Expand Up @@ -6006,6 +6037,11 @@ isobject@^3.0.0, isobject@^3.0.1:
resolved "https://registry.yarnpkg.com/isobject/-/isobject-3.0.1.tgz#4e431e92b11a9731636aa1f9c8d1ccbcfdab78df"
integrity sha1-TkMekrEalzFjaqH5yNHMvP2reN8=

isobject@^4.0.0:
version "4.0.0"
resolved "https://registry.npmjs.org/isobject/-/isobject-4.0.0.tgz#3f1c9155e73b192022a80819bacd0343711697b0"
integrity sha512-S/2fF5wH8SJA/kmwr6HYhK/RI/OkhD84k8ntalo0iJjZikgq1XFvR5M8NPT1x5F7fBwCG3qHfnzeP/Vh/ZxCUA==

isstream@~0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/isstream/-/isstream-0.1.2.tgz#47e63f7af55afa6f92e1500e690eb8b8529c099a"
Expand Down Expand Up @@ -7675,6 +7711,11 @@ object-path@^0.11.4:
resolved "https://registry.yarnpkg.com/object-path/-/object-path-0.11.5.tgz#d4e3cf19601a5140a55a16ad712019a9c50b577a"
integrity sha512-jgSbThcoR/s+XumvGMTMf81QVBmah+/Q7K7YduKeKVWL7N111unR2d6pZZarSk6kY/caeNxUDyxOvMWyzoU2eg==

object-path@^0.11.5:
version "0.11.7"
resolved "https://registry.npmjs.org/object-path/-/object-path-0.11.7.tgz#5f211161f34bb395e4b13a5f565b79d933b6f65d"
integrity sha512-T4evaK9VfGGQskXBDILcn6F90ZD+WO3OwRFFQ2rmZdUH4vQeDBpiolTpVlPY2yj5xSepyILTjDyM6UvbbdHMZw==

object-visit@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/object-visit/-/object-visit-1.0.1.tgz#f79c4493af0c5377b59fe39d395e41042dd045bb"
Expand Down

0 comments on commit 8843422

Please sign in to comment.