박권수

feat. Google Cloud Storage Refactoring

......@@ -3,12 +3,11 @@
const User = require('../../models/user');
const Profile = require('../../models/profile');
const DoctorInfo = require('../../models/doctorInfo');
const { uploadDoctorLicense } = require('../../util/GoogleCloudStorage');
const Joi = require('joi');
const jwt = require('jsonwebtoken');
const axios = require('axios');
const { Storage } = require('@google-cloud/storage');
const GoogleStorageUrl = 'https://storage.googleapis.com/';
exports.register = async(ctx) => {
const {
......@@ -107,14 +106,6 @@ exports.doctorRegister = async ctx => {
const { doctorInfoFile } = ctx.request.files;
const info = {
contact,
hospitalAddr,
hospitalNm,
doctorType,
doctorNm,
doctorLicense : '',
};
const schema = Joi.object().keys({
userId : Joi.string().email().max(50).required(),
......@@ -161,6 +152,24 @@ exports.doctorRegister = async ctx => {
return;
}
const [fileName, filePath] = [doctorInfoFile.name, doctorInfoFile.path];
const doctorLicense = await uploadDoctorLicense({
userId,
fileName,
filePath,
});
const info = {
contact,
hospitalAddr,
hospitalNm,
doctorType,
doctorNm,
doctorLicense,
};
const doctor = new User({
userId,
userTypeCd : 'DOCTOR',
......@@ -168,19 +177,6 @@ exports.doctorRegister = async ctx => {
});
await doctor.setPassword(password);
doctor.save();
const destination = userId + '_' + doctorInfoFile.name;
const storage = new Storage();
storage.bucket('doctor-info').upload(doctorInfoFile.path, {
destination,
}, (err, file, res) => {
if(err) console.log('Fail to upload Doctor License');
else {
info.doctorLicense = GoogleStorageUrl + `${res.bucket}/${res.name}`;
console.log('Success to Upload Doctor License!');
}
const doctorInfo = new DoctorInfo({
doctorId : userId,
......@@ -188,8 +184,8 @@ exports.doctorRegister = async ctx => {
useYn : 'W',
});
doctor.save();
doctorInfo.save();
});
ctx.status = 201;
......
const User = require('../../models/user');
const DoctorInfo = require('../../models/doctorInfo');
const jwt = require('jsonwebtoken');
const { Storage } = require('@google-cloud/storage');
const { viewDoctorLicense } = require('../../util/GoogleCloudStorage');
/**
......@@ -110,15 +109,10 @@ exports.getDoctorRegReqDetail = async ctx => {
return;
}
const fileName = doctorInfo.info.doctorLicense.split('/').pop();
const file = new Storage().bucket('doctor-info').file(fileName);
const option = {
version : 'v4',
expires : Date.now() + 1000 * 60 * 15,
action : 'read',
};
const [signedUrl] = file ? await file.getSignedUrl(option) : [''];
const doctorLicense = await viewDoctorLicense({
doctorInfo
});
ctx.status = 200;
ctx.body = {
......@@ -126,7 +120,7 @@ exports.getDoctorRegReqDetail = async ctx => {
...doctorInfo._doc,
info : {
...doctorInfo.info,
doctorLicense : signedUrl,
doctorLicense,
},
},
};
......
const Bottle = require('../models/bottle');
const BottleMedicine = require('../models/bottleMedicine');
const TakeMedicineHist = require('../models/takeMedicineHistory');
......
const { Storage } = require('@google-cloud/storage');
const storage = new Storage();
const GoogleStorageUrl = 'https://storage.googleapis.com/';
//의사 아이디, 업로드할 파일명, 업로드할 파일 경로를 인자로 받아, File을 GCS에 업로드 후 GCS 주소를 반환
exports.uploadDoctorLicense = async ({ userId, fileName, filePath }) => {
const destination = userId + '_' + fileName;
const result = await storage.bucket('doctor-info').upload(filePath, {
destination,
});
const doctorLicenseUrl = GoogleStorageUrl + `${result[0].bucket.id}/${result[0].name}`;
return doctorLicenseUrl;
};
//의사 정보를 인자로 받아 해당 Doctor License의 Signed URL을 반환
exports.viewDoctorLicense = async ({ doctorInfo }) => {
const fileName = doctorInfo.info.doctorLicense.split('/').pop();
const file = new Storage().bucket('doctor-info').file(fileName);
const option = {
version : 'v4',
expires : Date.now() + 1000 * 60 * 15,
action : 'read',
};
const [signedUrl] = file ? await file.getSignedUrl(option) : [null];
return signedUrl;
};
//의사 ID, 약 ID, 복용량을 인자로 받아, QR Code를 생성
exports.prescribeMedicine = async ({ doctorId, medicineId, dosage }) => {
//toDo
};
\ No newline at end of file