-
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.
Merge pull request #757 from openkfw/650-dms-no-auth
650 DMS with no authorization
- Loading branch information
Showing
31 changed files
with
974 additions
and
183 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
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,164 @@ | ||
import * as Minio from "minio"; | ||
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) { | ||
console.error("Error during searching for bucket", err); | ||
return cb(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); | ||
}); | ||
}); | ||
}; | ||
|
||
const sleep = (ms) => { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, ms); | ||
}); | ||
}; | ||
|
||
const establishConnection = async () => { | ||
const retries = 20; | ||
for (let i = 0; i <= retries; i++) { | ||
try { | ||
await sleep(20000); | ||
|
||
await makeBucketAsPromised(bucketName); | ||
|
||
console.log("Connection with min.io established."); | ||
break; | ||
} catch (e) { | ||
console.error("Problem with establishing connection to min.io and creating bucket."); | ||
|
||
if (i === retries) { | ||
console.error("Unable to connect with min.io. EXITING!"); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
} | ||
}; | ||
|
||
establishConnection(); | ||
|
||
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
Oops, something went wrong.