This repository has been archived by the owner on Nov 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: wip: upload file with graphql and azure
- Loading branch information
1 parent
75dc0d0
commit 8843422
Showing
11 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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" | ||
|
@@ -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" | ||
|
@@ -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== | ||
|
@@ -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" | ||
|
@@ -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" | ||
|