-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #255 from rr-wfm/feature/az-blob-storage
Add Azure Blob Storage as a storage engine for attachments
- Loading branch information
Showing
12 changed files
with
439 additions
and
114 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -118,3 +118,4 @@ dist | |
!**/.yarn/versions | ||
|
||
dist-types/ | ||
app-config.local.yaml |
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
23 changes: 23 additions & 0 deletions
23
plugins/qeta-backend/src/service/upload/attachmentStorageEngine.ts
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,23 @@ | ||
import { Attachment } from '@drodil/backstage-plugin-qeta-common'; | ||
import { File } from '../types'; | ||
import { Config } from '@backstage/config/index'; | ||
import { QetaStore } from '../../database/QetaStore'; | ||
|
||
export type AttachmentStorageEngineOptions = { | ||
config: Config; | ||
database: QetaStore; | ||
}; | ||
|
||
export interface AttachmentStorageEngine { | ||
handleFile: ( | ||
file: File, | ||
options?: { | ||
creator: string; | ||
postId?: number; | ||
answerId?: number; | ||
collectionId?: number; | ||
}, | ||
) => Promise<Attachment>; | ||
getAttachmentBuffer: (attachment: Attachment) => Promise<Buffer | undefined>; | ||
deleteAttachment(attachment: Attachment): Promise<void>; | ||
} |
94 changes: 94 additions & 0 deletions
94
plugins/qeta-backend/src/service/upload/azureBlobStorage.ts
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,94 @@ | ||
import * as fs from 'fs'; | ||
import { v4 as uuidv4 } from 'uuid'; | ||
import { Config } from '@backstage/config'; | ||
import { QetaStore } from '../../database/QetaStore'; | ||
import { Attachment } from '@drodil/backstage-plugin-qeta-common'; | ||
import { File } from '../types'; | ||
import { getAzureBlobServiceClient } from '../util'; | ||
import { | ||
AttachmentStorageEngine, | ||
AttachmentStorageEngineOptions, | ||
} from './attachmentStorageEngine'; | ||
|
||
class AzureBlobStorageEngine implements AttachmentStorageEngine { | ||
config: Config; | ||
database: QetaStore; | ||
backendBaseUrl: string; | ||
qetaUrl: string; | ||
container: string; | ||
|
||
constructor(opts: AttachmentStorageEngineOptions) { | ||
this.config = opts.config; | ||
this.database = opts.database; | ||
this.backendBaseUrl = this.config.getString('backend.baseUrl'); | ||
this.qetaUrl = `${this.backendBaseUrl}/api/qeta/attachments`; | ||
this.container = | ||
this.config.getOptionalString('qeta.storage.blobStorageContainer') || | ||
'backstage-qeta-images'; | ||
} | ||
|
||
handleFile = async ( | ||
file: File, | ||
options?: { postId?: number; answerId?: number; collectionId?: number }, | ||
): Promise<Attachment> => { | ||
const imageUuid = uuidv4(); | ||
const filename = `image-${imageUuid}-${Date.now()}.${file.ext}`; | ||
|
||
const imageURI = `${this.qetaUrl}/${imageUuid}`; | ||
const client = getAzureBlobServiceClient(this.config); | ||
const container = client.getContainerClient(this.container); | ||
if (!(await container.exists())) { | ||
await container.create(); | ||
} | ||
|
||
await container.uploadBlockBlob( | ||
filename, | ||
fs.createReadStream(file.path), | ||
file.size, | ||
); | ||
|
||
return await this.database.postAttachment({ | ||
uuid: imageUuid, | ||
locationType: 'azure', | ||
locationUri: imageURI, | ||
extension: file.ext, | ||
mimeType: file.mimeType, | ||
path: filename, | ||
binaryImage: undefined, | ||
...options, | ||
}); | ||
}; | ||
|
||
getAttachmentBuffer = async (attachment: Attachment) => { | ||
const client = getAzureBlobServiceClient(this.config); | ||
const container = client.getContainerClient(this.container); | ||
|
||
if (!(await container.exists())) { | ||
return undefined; | ||
} | ||
|
||
const blob = container.getBlockBlobClient(attachment.path); | ||
if (await blob.exists()) { | ||
return blob.downloadToBuffer(); | ||
} | ||
|
||
return undefined; | ||
}; | ||
|
||
deleteAttachment = async (attachment: Attachment) => { | ||
const client = getAzureBlobServiceClient(this.config); | ||
const container = client.getContainerClient(this.container); | ||
if (!(await container.exists())) { | ||
return; | ||
} | ||
|
||
const blob = container.getBlockBlobClient(attachment.path); | ||
if (await blob.exists()) { | ||
await blob.delete(); | ||
} | ||
}; | ||
} | ||
|
||
export default (opts: AttachmentStorageEngineOptions) => { | ||
return new AzureBlobStorageEngine(opts); | ||
}; |
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
Oops, something went wrong.