-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MONIT-60: ABM de restricción por paciente
- Loading branch information
1 parent
e13a6fc
commit fee1856
Showing
7 changed files
with
155 additions
and
5 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
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
64 changes: 64 additions & 0 deletions
64
modules/restriccion-huds/archivos/controller/restriccionHudsStore.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,64 @@ | ||
import { make_Fs } from '../schemas/restriccionHudsStore'; | ||
import { Types } from 'mongoose'; | ||
import * as base64_stream from 'base64-stream'; | ||
import * as stream from 'stream'; | ||
|
||
const base64RegExp = /data:([a-zA-Z0-9]+\/[a-zA-Z0-9-.+]+).*,(.*)/; | ||
|
||
export async function storeFile(base64, metadata) { | ||
try { | ||
const match = base64.match(base64RegExp); | ||
const mime = match[1]; | ||
const data = match[2]; | ||
const writePromise = new Promise((resolve) => { | ||
const uniqueId = new Types.ObjectId(); | ||
const input = new stream.PassThrough(); | ||
const decoder64 = base64_stream.decode(); | ||
const ImageFiles = make_Fs(); | ||
ImageFiles.writeFile( | ||
{ | ||
_id: uniqueId, | ||
filename: uniqueId + '.' + mime.split('/')[1], | ||
contentType: mime, | ||
metadata | ||
}, input.pipe(decoder64), | ||
(error, createdFile) => { | ||
resolve(createdFile); | ||
} | ||
); | ||
input.end(data); | ||
}); | ||
const result = await writePromise; | ||
return result; | ||
} catch (e) { | ||
return e; | ||
} | ||
} | ||
|
||
export async function readFile(id) { | ||
try { | ||
const ImageFiles = make_Fs(); | ||
const idFile = new Types.ObjectId(id); | ||
const contexto = await ImageFiles.findOne({ _id: idFile }); | ||
const imageStream = await ImageFiles.readFile({ _id: idFile }); | ||
return { | ||
file: contexto, | ||
stream: imageStream | ||
}; | ||
} catch (e) { | ||
return e; | ||
} | ||
} | ||
|
||
export async function deleteFile(id) { | ||
try { | ||
const ImageFiles = make_Fs(); | ||
const idFile = new Types.ObjectId(id); | ||
await ImageFiles.unlink(idFile, (error) => { }); | ||
return { | ||
file: idFile | ||
}; | ||
} catch (e) { | ||
return e; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
modules/restriccion-huds/archivos/schemas/restriccionHudsStore.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,12 @@ | ||
import * as mongoose from 'mongoose'; | ||
const { createBucket } = require('mongoose-gridfs'); | ||
|
||
export function make_Fs() { | ||
const CDAFilesSchema = createBucket({ | ||
collectionName: 'RestriccionHUDSStore', | ||
bucketName: 'RestriccionHUDSStore', | ||
mongooseConnection: mongoose.connection | ||
}); | ||
|
||
return CDAFilesSchema; | ||
} |
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,2 @@ | ||
|
||
export { restriccionHudsRouter } from './../restriccion-huds/restriccion-huds.routes'; |
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,46 @@ | ||
import { MongoQuery, ResourceBase } from '@andes/core'; | ||
import { restriccionHuds } from './restriccion-huds.schema'; | ||
import { readFile, storeFile, deleteFile } from './archivos/controller/restriccionHudsStore'; | ||
|
||
class restriccionHudsResource extends ResourceBase { | ||
Model = restriccionHuds; | ||
resourceName = 'restriccionHuds'; | ||
routesEnable = ['get', 'post', 'delete']; | ||
searchFileds = { | ||
id: { | ||
field: '_id', | ||
fn: MongoQuery.equalMatch | ||
}, | ||
}; | ||
} | ||
|
||
export const restriccionHudsCtr = new restriccionHudsResource({}); | ||
export const restriccionHudsRouter = restriccionHudsCtr.makeRoutes(); | ||
|
||
restriccionHudsRouter.get('/store/:id', (req, res, next) => { | ||
readFile(req.params.id).then((data: any) => { | ||
res.contentType(data.file.contentType); | ||
data.stream.on('data', (data2) => { | ||
res.write(data2); | ||
}); | ||
data.stream.on('end', () => { | ||
res.end(); | ||
}); | ||
}).catch(next); | ||
}); | ||
|
||
restriccionHudsRouter.post('/store', (req, res, next) => { | ||
const file = req.body.file; | ||
const metadata = req.body.metadata; | ||
storeFile(file, metadata).then((data) => { | ||
res.json(data); | ||
}).catch(next); | ||
}); | ||
|
||
restriccionHudsRouter.delete('/store/:id', async (req, res, next) => { | ||
const id = req.params.id; | ||
deleteFile(id).then((data) => { | ||
res.json(data); | ||
}).catch(next); | ||
}); | ||
|
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,16 @@ | ||
import * as mongoose from 'mongoose'; | ||
import { ModuloSchema } from '../../core/tm/schemas/modulos.schema'; | ||
|
||
export const restriccionHudsSchema = new mongoose.Schema({ | ||
id: mongoose.Schema.Types.ObjectId, | ||
fecha: { type: Date, default: Date.now }, | ||
titulo: String, | ||
descripcion: String, | ||
modulo: { | ||
type: ModuloSchema | ||
}, | ||
imagenes: mongoose.Schema.Types.Mixed, | ||
activa: Boolean | ||
}); | ||
|
||
export const restriccionHuds = mongoose.model('restriccionHuds', restriccionHudsSchema, 'restriccionHuds'); |