-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Peter Baus
committed
Feb 10, 2021
1 parent
1bc6832
commit a047617
Showing
30 changed files
with
945 additions
and
183 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 @@ | ||
export const host = process.env.API_HOST || "master-api"; | ||
export const port = process.env.PORT || 8080; | ||
export const isSsl = process.env.USE_SSL === "ssl" ? true : false; | ||
export const hostPort = `${isSsl ? "https" : "http"}://${host}:${port}`; | ||
|
||
export const minioEndPoint = process.env.MINIO_ENDPOINT; // nginx in development | ||
export const minioPort = process.env.MINIO_PORT && parseInt(process.env.MINIO_PORT as string, 10) || 9000; | ||
export const minioUseSSL = process.env.MINIO_USE_SSL === "true" ? true : false; | ||
export const minioAccessKey = process.env.MINIO_ACCESS_KEY || "minio"; | ||
export const minioSecretKey = process.env.MINIO_SECRET_KEY || "minio123"; |
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,17 @@ | ||
export const schema = { | ||
description: "Not found", | ||
type: "object", | ||
properties: { | ||
apiVersion: { type: "string", example: "1.0" }, | ||
error: { | ||
type: "object", | ||
properties: { | ||
code: { type: "string", example: "404" }, | ||
message: { | ||
type: "string", | ||
example: "The route you are looking for was not found.", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; |
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,136 @@ | ||
import * as Minio from "minio"; | ||
import { v4 as uuid } from "uuid"; | ||
import { minioEndPoint, minioPort, minioUseSSL, minioAccessKey, minioSecretKey } from "../config"; | ||
|
||
const Readable = require("stream").Readable; | ||
|
||
interface Metadata { | ||
"Content-Type"?: string, | ||
fileName: string, | ||
} | ||
|
||
interface MetadataWithName extends Metadata { | ||
name: string | ||
} | ||
|
||
const minioClient: any = new Minio.Client({ | ||
endPoint: minioEndPoint || "nginx", | ||
port: minioPort, | ||
useSSL: minioUseSSL, | ||
accessKey: minioAccessKey, | ||
secretKey: minioSecretKey, | ||
}); | ||
|
||
const bucketName: string = "trubudget"; | ||
|
||
|
||
const makeBucket = (bucket: string, cb: Function) => { | ||
minioClient.bucketExists(bucket, (err, exists) => { | ||
if (err) { | ||
return console.error("Error during searching for bucket", err); | ||
} | ||
|
||
if (!exists) { | ||
minioClient.makeBucket(bucket, "us-east-1", (err) => { | ||
if (err) { | ||
console.error("Error creating bucket.", err); | ||
return cb(err); | ||
} | ||
console.log(`Minio: Bucket ${bucket} created.`); | ||
return cb(null, true); | ||
}); | ||
} | ||
}); | ||
}; | ||
|
||
export const makeBucketAsPromised = (bucket: string) => { | ||
return new Promise((resolve, reject) => { | ||
makeBucket(bucket, (err) => { | ||
if (err) return reject(err); | ||
|
||
resolve(true); | ||
}); | ||
}); | ||
}; | ||
|
||
|
||
const upload = (file: string, content: string, metaData: Metadata, cb: Function) => { | ||
const s = new Readable(); | ||
s._read = () => {}; | ||
s.push(content); | ||
s.push(null); | ||
|
||
const metaDataWithName: MetadataWithName = { ...metaData, name: file }; | ||
// Using putObject API upload your file to the bucket . | ||
minioClient.putObject(bucketName, file, s, metaDataWithName, (err, etag) => { | ||
if (err) { | ||
console.error("minioClient.putObject", err); | ||
return cb(err); | ||
} | ||
|
||
return cb(null, etag); | ||
}); | ||
}; | ||
|
||
export const uploadAsPromised = (file: string, content: string, metaData: Metadata = {fileName: "default"}) => { | ||
return new Promise((resolve, reject) => { | ||
upload(file, content, metaData, (err, etag) => { | ||
if (err) return reject(err); | ||
|
||
resolve(etag); | ||
}); | ||
}); | ||
}; | ||
|
||
const download = (file: string, cb: Function) => { | ||
let fileContent: string = ""; | ||
minioClient.getObject(bucketName, file, (err, dataStream) => { | ||
if (err) { | ||
console.error("Error during getting file object", err); | ||
cb(err); | ||
} | ||
dataStream.on("data", (chunk: string) => { | ||
fileContent += chunk; | ||
}); | ||
dataStream.on("end", () => { | ||
cb(null, fileContent); | ||
}); | ||
dataStream.on("error", function (err) { | ||
console.error("Error during getting file object datastream", err); | ||
}); | ||
}); | ||
}; | ||
|
||
export const downloadAsPromised = (file: string) => { | ||
return new Promise((resolve, reject) => { | ||
download(file, (err, fileContent: string) => { | ||
if (err) return reject(err); | ||
|
||
resolve(fileContent); | ||
}); | ||
}); | ||
}; | ||
|
||
const getMetadata = (fileHash: string, cb: Function) => { | ||
minioClient.statObject(bucketName, fileHash, (err, stat: MetadataWithName) => { | ||
if (err) { | ||
console.error(err); | ||
return cb(err); | ||
} | ||
cb(null, stat); | ||
}); | ||
}; | ||
|
||
export const getMetadataAsPromised = (fileHash: string) => { | ||
return new Promise((resolve, reject) => { | ||
getMetadata(fileHash, (err, metaData: MetadataWithName) => { | ||
if (err) return reject(err); | ||
|
||
resolve(metaData); | ||
}); | ||
}); | ||
}; | ||
|
||
makeBucketAsPromised(bucketName); | ||
|
||
export default minioClient; |
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
48 changes: 48 additions & 0 deletions
48
api/src/service/domain/workflow/workflowitem_document_download_minio.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,48 @@ | ||
import { Ctx } from "../../../lib/ctx"; | ||
import * as Result from "../../../result"; | ||
import { NotAuthorized } from "../errors/not_authorized"; | ||
import { NotFound } from "../errors/not_found"; | ||
import { ServiceUser } from "../organization/service_user"; | ||
import * as WorkflowitemDocument from "./document"; | ||
import * as Workflowitem from "./workflowitem"; | ||
import * as WorkflowitemDocumentUploaded from "./workflowitem_document_uploaded"; | ||
import VError = require("verror"); | ||
|
||
interface Repository { | ||
getWorkflowitem(workflowitemId): Promise<Result.Type<Workflowitem.Workflowitem>>; | ||
getDocumentEvents(documentId): Promise<Result.Type<WorkflowitemDocumentUploaded.Event[]>>; | ||
} | ||
|
||
export async function getDocumentMinio( | ||
ctx: Ctx, | ||
workflowitemId: string, | ||
documentId: string, | ||
repository: Repository, | ||
): Promise<Result.Type<WorkflowitemDocument.UploadedDocument>> { | ||
// check for permissions etc | ||
const workflowitem = await repository.getWorkflowitem(workflowitemId); | ||
if (Result.isErr(workflowitem)) { | ||
return workflowitem; | ||
} | ||
|
||
// Get all events from one document | ||
const documentEvents = await repository.getDocumentEvents(documentId); | ||
if (Result.isErr(documentEvents)) { | ||
return new VError( | ||
new NotFound(ctx, "document", documentId), | ||
`couldn't get document events from ${workflowitem}`, | ||
); | ||
} | ||
|
||
// Only return if document has relation to the workflowitem | ||
if (!workflowitem.documents.some((d) => d.documentId === documentId)) { | ||
return new VError( | ||
new NotFound(ctx, "document", documentId), | ||
`workfowitem ${workflowitem} has no link to document`, | ||
); | ||
} | ||
|
||
return documentEvents | ||
.filter((d) => d.workflowitemId === workflowitem.id) | ||
.map((d) => d.document)[0]; | ||
} |
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.