|
@@ -6,7 +6,6 @@ const express = require("express");
|
|
|
const axios = require("axios");
|
|
|
// const SERVER_PORT = process.env.port || process.env.PORT || 53000;
|
|
|
const SERVER_PORT = process.env.port || process.env.PORT || 53000;
|
|
|
-// const authProvider = require('./auth/AuthProvider');
|
|
|
const {getFetch, updateFetch} = require('./fetch');
|
|
|
const bodyParser = require('body-parser');
|
|
|
const https = require('https');
|
|
@@ -18,7 +17,7 @@ const multer = require('multer');
|
|
|
// const pptxgen = require('pptxgenjs');
|
|
|
const msal = require('@azure/msal-node');
|
|
|
// const FormData = require("form-data");
|
|
|
-// const { v4: uuidv4 } = require('uuid');
|
|
|
+const { v4: uuidv4 } = require('uuid');
|
|
|
const mime = require('mime-types');
|
|
|
const {init} = require('./ext.js');
|
|
|
const BadRequest = require('./error/badRequest.js');
|
|
@@ -473,6 +472,8 @@ async function uploadDrive(options, folder, siteId, path) {
|
|
|
serverApp.post('/api/upload', upload.array('file'),
|
|
|
errorHandler(async (req, res, next)=>{
|
|
|
const startTime = new Date();
|
|
|
+ console.log('*** 파일 Upload Start: ' + startTime.toLocaleString());
|
|
|
+
|
|
|
const files = req.files;
|
|
|
let {siteId, path, folder, file_path} = req.body;
|
|
|
|
|
@@ -486,10 +487,11 @@ serverApp.post('/api/upload', upload.array('file'),
|
|
|
}
|
|
|
|
|
|
let fileWriteTime = new Date();
|
|
|
+ const nameArray = [];
|
|
|
if (files && files.length > 0) {
|
|
|
const promiseArray = [];
|
|
|
const labelPromiseArray = [];
|
|
|
- // const fileWriteArray = [];
|
|
|
+ const fileWriteArray = [];
|
|
|
const possibleExt = POSSIBLE_EXT;
|
|
|
const {supported_file_ext, protected_file_ext} = possibleExt;
|
|
|
let supportedArr = [];
|
|
@@ -501,158 +503,195 @@ serverApp.post('/api/upload', upload.array('file'),
|
|
|
}
|
|
|
let beforeUri = '';
|
|
|
let beforeItemId = '';
|
|
|
- for (let idx in files) {
|
|
|
- const file = files[idx];
|
|
|
+ const now = new Date();
|
|
|
+ console.log('File Write Start : ', now.toLocaleString());
|
|
|
+ const promiseArrNew = files.map((file, idx)=>{
|
|
|
file.originalname = Buffer.from(file.originalname, 'ascii').toString('utf8');
|
|
|
let originName = file.originalname;
|
|
|
if (originName) {
|
|
|
- let ext = globalPath.extname(originName);
|
|
|
- if (supportedArr.length > 0 && !supportedArr.includes(ext)) {
|
|
|
- return res.json({success:'F', message: '파일명 : ' + originName
|
|
|
- +'<br>업로드 불가 파일이 포함되어 있습니다.<br>가능 확장자 :'
|
|
|
- + JSON.stringify(supportedArr)});
|
|
|
- }
|
|
|
-
|
|
|
- // const uuid = uuidv4();
|
|
|
- // const realFileName = uuid + ext;
|
|
|
- // fileWriteArray.push(new Promise((resolve, reject) => {
|
|
|
- // const data = fs.writeFileSync('Z:/Source/' + realFileName, file.buffer)
|
|
|
- // resolve({
|
|
|
- // data
|
|
|
- // })
|
|
|
- // }));
|
|
|
-
|
|
|
- const base64Data = Buffer.from(file.buffer).toString('base64');
|
|
|
- const param = {
|
|
|
- apiKey: gateWayKey,
|
|
|
- email: req.session.account.idTokenClaims.email,
|
|
|
- dispFileName: originName,
|
|
|
- aipGuid: "878173ae-cc36-4881-af57-604af868314c",
|
|
|
- // aipGuid: "0166b75f-6a93-47f3-8fd3-e1e7c59141ab",
|
|
|
- // comment: "set-protection",
|
|
|
- comment: "set-label",
|
|
|
- fileData: base64Data,
|
|
|
- }
|
|
|
+ let ext = globalPath.extname(originName);
|
|
|
+ if (supportedArr.length > 0 && !supportedArr.includes(ext)) {
|
|
|
+ throw '파일명 : ' + originName +'<br>업로드 불가 파일이 포함되어 있습니다.<br>가능 확장자 :' + JSON.stringify(supportedArr);
|
|
|
+ }
|
|
|
|
|
|
- // labelPromiseArray.push(new Promise(async (resolve)=>{resolve(await axios.post(apiUrl + '/api/v1/stream/set-label', param))}));
|
|
|
- labelPromiseArray.push(axios.post(apiUrl + '/api/v1/stream/set-label', param));
|
|
|
- // labelPromiseArray.push(axios.post(apiUrl + '/api/v1/stream/set-protection', param));
|
|
|
-
|
|
|
- let formatPath = '';
|
|
|
+ // const base64Data = Buffer.from(file.buffer).toString('base64');
|
|
|
+ const uuid = uuidv4();
|
|
|
+ const realFileName = uuid + ext;
|
|
|
+ fileWriteArray.push(new Promise((resolve, reject) => {
|
|
|
+ // const data = fs.writeFileSync('Z:/Source/' + realFileName, file.buffer)
|
|
|
+ const data = fs.writeFileSync('C:/Users/anrps/OneDrive/바탕 화면/saveDirectory/' + realFileName, file.buffer)
|
|
|
+ resolve({
|
|
|
+ data
|
|
|
+ })
|
|
|
+ }));
|
|
|
+ nameArray.push(realFileName);
|
|
|
+ // const param = {
|
|
|
+ // apiKey: gateWayKey,
|
|
|
+ // email: req.session.account.idTokenClaims.email,
|
|
|
+ // dispFileName: originName,
|
|
|
+ // aipGuid: "878173ae-cc36-4881-af57-604af868314c",
|
|
|
+ // // aipGuid: "0166b75f-6a93-47f3-8fd3-e1e7c59141ab",
|
|
|
+ // // comment: "set-protection",
|
|
|
+ // comment: "set-label",
|
|
|
+ // fileData: base64Data,
|
|
|
+ // }
|
|
|
+
|
|
|
+ // return axios.post(apiUrl + '/api/v1/stream/set-label', param).then(async (result)=>{
|
|
|
+ // let formatPath = '';
|
|
|
+
|
|
|
+ // if (file_path[idx]) {
|
|
|
+ // // console.log(file_path[idx]);
|
|
|
+ // if (!path.includes(":")) {
|
|
|
+ // formatPath = ":" + file_path[idx];
|
|
|
+ // }
|
|
|
+ // else {
|
|
|
+ // formatPath = file_path[idx];
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+
|
|
|
+ // let itemId = '';
|
|
|
+ // const uri = endPoint + "/sites/"+ siteId + path + formatPath;
|
|
|
+ // // siteInfoArr.push(axios.get(uri, options));
|
|
|
+ // if (beforeUri === uri) {
|
|
|
+ // itemId = beforeItemId;
|
|
|
+ // }
|
|
|
+ // else {
|
|
|
+ // const sitesInfo = await axios.get(uri, options);
|
|
|
+ // itemId = sitesInfo.data.id;
|
|
|
+ // beforeUri = uri;
|
|
|
+ // beforeItemId = itemId;
|
|
|
+ // }
|
|
|
+
|
|
|
+ // const labelData = result.data;
|
|
|
+ // if (labelData && labelData.success && !labelData.errorCode && labelData.result.fileData){
|
|
|
+ // const fileData = labelData.result.fileData;
|
|
|
+ // const data = Buffer.from(fileData, 'base64');
|
|
|
+ // return axios.put(endPoint + "/sites/"+ siteId +"/drive/items/"+itemId+':/' + labelData.result.dispFileName + ':/content', data, options);
|
|
|
+ // }
|
|
|
+ // else {
|
|
|
+ // let name = '';
|
|
|
+ // if (labelData.result && labelData.result.dispFileName) {
|
|
|
+ // name = ' (파일명 : ' + labelData.result.dispFileName + ')';
|
|
|
+ // }
|
|
|
+ // throw new LabelBadResponse(labelData.errorMessage + name);
|
|
|
+ // }
|
|
|
+ // });
|
|
|
+ // labelPromiseArray.push(axios.post(apiUrl + '/api/v1/stream/set-protection', param));
|
|
|
+
|
|
|
+
|
|
|
+ // promiseArray.push(endPoint + "/sites/"+ siteId +"/drive/items/"+itemId+':/');
|
|
|
+ }
|
|
|
+ });
|
|
|
+ await Promise.all(fileWriteArray);
|
|
|
+ console.log(nameArray);
|
|
|
+ console.log('File Write 소요시간', timeCheck(now, new Date()));
|
|
|
+ // for (let idx in files) {
|
|
|
+ // const file = files[idx];
|
|
|
+ // file.originalname = Buffer.from(file.originalname, 'ascii').toString('utf8');
|
|
|
+ // let originName = file.originalname;
|
|
|
+ // if (originName) {
|
|
|
+ // let ext = globalPath.extname(originName);
|
|
|
+ // if (supportedArr.length > 0 && !supportedArr.includes(ext)) {
|
|
|
+ // return res.json({success:'F', message: '파일명 : ' + originName
|
|
|
+ // +'<br>업로드 불가 파일이 포함되어 있습니다.<br>가능 확장자 :'
|
|
|
+ // + JSON.stringify(supportedArr)});
|
|
|
+ // }
|
|
|
+
|
|
|
+ // const base64Data = Buffer.from(file.buffer).toString('base64');
|
|
|
+ // const param = {
|
|
|
+ // apiKey: gateWayKey,
|
|
|
+ // email: req.session.account.idTokenClaims.email,
|
|
|
+ // dispFileName: originName,
|
|
|
+ // aipGuid: "878173ae-cc36-4881-af57-604af868314c",
|
|
|
+ // // aipGuid: "0166b75f-6a93-47f3-8fd3-e1e7c59141ab",
|
|
|
+ // // comment: "set-protection",
|
|
|
+ // comment: "set-label",
|
|
|
+ // fileData: base64Data,
|
|
|
+ // }
|
|
|
+
|
|
|
+ // let formatPath = '';
|
|
|
|
|
|
- if (file_path[idx]) {
|
|
|
- // console.log(file_path[idx]);
|
|
|
- if (!path.includes(":")) {
|
|
|
- formatPath = ":" + file_path[idx];
|
|
|
- }
|
|
|
- else {
|
|
|
- formatPath = file_path[idx];
|
|
|
- }
|
|
|
- }
|
|
|
+ // if (file_path[idx]) {
|
|
|
+ // // console.log(file_path[idx]);
|
|
|
+ // if (!path.includes(":")) {
|
|
|
+ // formatPath = ":" + file_path[idx];
|
|
|
+ // }
|
|
|
+ // else {
|
|
|
+ // formatPath = file_path[idx];
|
|
|
+ // }
|
|
|
+ // }
|
|
|
|
|
|
- let itemId = '';
|
|
|
- const uri = endPoint + "/sites/"+ siteId + path + formatPath;
|
|
|
- // siteInfoArr.push(axios.get(uri, options));
|
|
|
- if (beforeUri === uri) {
|
|
|
- itemId = beforeItemId;
|
|
|
- }
|
|
|
- else {
|
|
|
- const sitesInfo = await axios.get(uri, options);
|
|
|
- itemId = sitesInfo.data.id;
|
|
|
- beforeUri = uri;
|
|
|
- beforeItemId = itemId;
|
|
|
- }
|
|
|
+ // let itemId = '';
|
|
|
+ // const uri = endPoint + "/sites/"+ siteId + path + formatPath;
|
|
|
+ // // siteInfoArr.push(axios.get(uri, options));
|
|
|
+ // if (beforeUri === uri) {
|
|
|
+ // itemId = beforeItemId;
|
|
|
+ // }
|
|
|
+ // else {
|
|
|
+ // const sitesInfo = await axios.get(uri, options);
|
|
|
+ // itemId = sitesInfo.data.id;
|
|
|
+ // beforeUri = uri;
|
|
|
+ // beforeItemId = itemId;
|
|
|
+ // }
|
|
|
+
|
|
|
+ // labelPromiseArray.push(axios.post(apiUrl + '/api/v1/stream/set-label', param).then((result)=>{
|
|
|
+ // const labelData = result.data;
|
|
|
+ // if (labelData && labelData.success && !labelData.errorCode && labelData.result.fileData){
|
|
|
+ // const fileData = labelData.result.fileData;
|
|
|
+ // const data = Buffer.from(fileData, 'base64');
|
|
|
+ // return axios.put(endPoint + "/sites/"+ siteId +"/drive/items/"+itemId+':/' + labelData.result.dispFileName + ':/content', data, options);
|
|
|
+ // }
|
|
|
+ // else {
|
|
|
+ // let name = '';
|
|
|
+ // if (obj.result && obj.result.dispFileName) {
|
|
|
+ // name = ' (파일명 : ' + obj.result.dispFileName + ')';
|
|
|
+ // }
|
|
|
+ // throw new LabelBadResponse(obj.data.errorMessage + name);
|
|
|
+ // }
|
|
|
+ // }));
|
|
|
+ // // labelPromiseArray.push(axios.post(apiUrl + '/api/v1/stream/set-protection', param));
|
|
|
|
|
|
- // if (protectedMap.size > 0 && protectedMap.get(ext)) {
|
|
|
- // let name = originName.replace(ext, '');
|
|
|
- // ext = protectedMap.get(ext);
|
|
|
- // originName = name + ext;
|
|
|
- // }
|
|
|
- promiseArray.push(endPoint + "/sites/"+ siteId +"/drive/items/"+itemId+':/');
|
|
|
- }
|
|
|
- }
|
|
|
+
|
|
|
+ // // promiseArray.push(endPoint + "/sites/"+ siteId +"/drive/items/"+itemId+':/');
|
|
|
+ // }
|
|
|
+ // }
|
|
|
|
|
|
- // const siteInfos = await Promise.all(siteInfoArr);
|
|
|
- // siteInfos.forEach((obj)=>{
|
|
|
- // promiseArray.push(endPoint + "/sites/"+ siteId +"/drive/items/"+obj.data.id+':/');
|
|
|
- // })
|
|
|
- //file write 시 사용
|
|
|
- //const writeArr = await Promise.all(fileWriteArray);
|
|
|
|
|
|
- let writeTime = timeCheck(fileWriteTime, new Date());
|
|
|
- console.log('파일 Write Time : '+ writeTime);
|
|
|
-
|
|
|
+ // let writeTime = timeCheck(fileWriteTime, new Date());
|
|
|
+ // console.log('User File Upload Time: '+ writeTime);
|
|
|
|
|
|
- const labelTime = new Date();
|
|
|
- const labelResult = await Promise.all(labelPromiseArray);
|
|
|
- // console.log(labelResult[0].data.result.fileData);
|
|
|
- console.log('label time : ', timeCheck(labelTime, new Date()));
|
|
|
- for (let idx in labelResult) {
|
|
|
- const obj = labelResult[idx].data;
|
|
|
- if (obj && obj.success === true && !obj.errorCode && obj.result.fileData) {
|
|
|
- const data = Buffer.from(obj.result.fileData, 'base64');
|
|
|
- // promiseArray[idx] = new Promise(async (resolve)=> resolve(await axios.put(promiseArray[idx] + obj.data.result.dispFileName + ':/content', data, options)));
|
|
|
- promiseArray[idx] = axios.put(promiseArray[idx] + obj.result.dispFileName + ':/content', data, options);
|
|
|
- // promiseArray.push(axios.put(endPoint + "/sites/"+ siteId +"/drive/items/"+ siteInfos[idx].data.id + ':/' + obj.data.result.dispFileName + ':/content', data, options));
|
|
|
- }
|
|
|
- else {
|
|
|
- let name = '';
|
|
|
- if (obj.result && obj.result.dispFileName) {
|
|
|
- name = ' (파일명 : ' + obj.result.dispFileName + ')';
|
|
|
- }
|
|
|
- throw new LabelBadResponse(obj.data.errorMessage + name);
|
|
|
- }
|
|
|
- }
|
|
|
- // labelResult.forEach((obj, idx)=>{
|
|
|
- // if (obj.data && !obj.data.result.errorCode && obj.data.result.fileData) {
|
|
|
- // const data = Buffer.from(obj.data.result.fileData, 'base64');
|
|
|
- // promiseArray[idx] = axios.put(promiseArray[idx] + obj.data.result.dispFileName + ':/content', data, options);
|
|
|
- // // promiseArray.push(axios.put(endPoint + "/sites/"+ siteId +"/drive/items/"+ siteInfos[idx].data.id + ':/' + obj.data.result.dispFileName + ':/content', data, options));
|
|
|
+ // const labelTime = new Date();
|
|
|
+ // const labelResult = await Promise.all(labelPromiseArray);
|
|
|
+ // console.log('label time : ', timeCheck(labelTime, new Date()));
|
|
|
+ // for (let idx in labelResult) {
|
|
|
+ // const obj = labelResult[idx].data;
|
|
|
+ // if (obj && obj.success === true && !obj.errorCode && obj.result.fileData) {
|
|
|
+ // const data = Buffer.from(obj.result.fileData, 'base64');
|
|
|
+ // promiseArray[idx] = axios.put(promiseArray[idx] + obj.result.dispFileName + ':/content', data, options);
|
|
|
// }
|
|
|
// else {
|
|
|
- // throw '파일명 : '+obj.data.result.dispFileName + '<br>에러 : ' + obj.data.result.errorMessage;
|
|
|
+ // let name = '';
|
|
|
+ // if (obj.result && obj.result.dispFileName) {
|
|
|
+ // name = ' (파일명 : ' + obj.result.dispFileName + ')';
|
|
|
+ // }
|
|
|
+ // throw new LabelBadResponse(obj.data.errorMessage + name);
|
|
|
// }
|
|
|
- // })
|
|
|
- // const readArray = [];
|
|
|
- // labelResult.forEach((obj)=>{
|
|
|
- // readArray.push(new Promise((resolve, reject)=>{
|
|
|
- // if (obj.data && !obj.data.result.errorCode) {
|
|
|
- // fs.readFile('Z:/Target/' + obj.data.result.outputFileName, (err, data)=>{
|
|
|
- // if (err) {
|
|
|
- // reject({err});
|
|
|
- // }
|
|
|
- // resolve({data});
|
|
|
- // //promiseArray[idx] = axios.put(promiseArray[idx], data, options);
|
|
|
- // });
|
|
|
- // }
|
|
|
- // else {
|
|
|
- // reject(obj.data.result.errorMessage);
|
|
|
- // }
|
|
|
- // }))
|
|
|
- // });
|
|
|
- // console.log('labelResult : ', labelResult);
|
|
|
- // let readTime = new Date();
|
|
|
- // const readResult = await Promise.all(readArray);
|
|
|
-
|
|
|
- // console.log(readResult);
|
|
|
- // console.log('File Read time : ', timeCheck(readTime, new Date()));
|
|
|
+ // }
|
|
|
|
|
|
+ let writeTime = timeCheck(fileWriteTime, new Date());
|
|
|
+ console.log('User File Upload Time: '+ writeTime);
|
|
|
|
|
|
- let uploadTime = new Date();
|
|
|
- // promiseArray.forEach((obj, idx)=>{
|
|
|
- // // console.log(readResult[idx].data);
|
|
|
- // // console.log(obj[idx]);
|
|
|
- // promiseArray[idx] = axios.put(obj, readResult[idx].data, options);
|
|
|
- // });
|
|
|
+ let uploadTime = new Date();
|
|
|
|
|
|
- const uploadResult = await Promise.all(promiseArray);
|
|
|
+ // const uploadResult = await Promise.all(labelPromiseArray);
|
|
|
+ // const uploadResult = await Promise.all(promiseArrNew);
|
|
|
|
|
|
- console.log('File uploadTime : ', timeCheck(uploadTime, new Date()));
|
|
|
- }
|
|
|
- const endTime = new Date();
|
|
|
- let betweenTime = timeCheck(startTime, endTime);
|
|
|
- console.log('시작 시간 :', startTime.toLocaleString(), ', 종료 시간 :', endTime.toLocaleString(), ', 소요 시간 :', betweenTime);
|
|
|
- res.json({success:'S', message: '요청하신 파일 업로드가 정상적으로 처리 되었습니다.'});
|
|
|
+ console.log('File uploadTime : ', timeCheck(uploadTime, new Date()));
|
|
|
+ }
|
|
|
+ const endTime = new Date();
|
|
|
+ let betweenTime = timeCheck(startTime, endTime);
|
|
|
+ console.log('*** 파일 Upload ...End: 시작 시간 :', startTime.toLocaleString(), ', 종료 시간 :', endTime.toLocaleString(), ', 소요 시간 :', betweenTime);
|
|
|
+ res.json({success:'S', message: '요청하신 파일 업로드가 정상적으로 처리 되었습니다.'});
|
|
|
}
|
|
|
else {
|
|
|
throw new NotFound(`업로드 요청 파라미터 정보를 확인해 주세요.<br>siteId : ${siteId}, path : ${path}`);
|
|
@@ -708,10 +747,6 @@ serverApp.post('/api/download',
|
|
|
if (data && data.success === true && !data.errorCode && data.result.fileData) {
|
|
|
const fileData = data.result.fileData;
|
|
|
return res.json({success: 'S', data: fileData, name: data.result.dispFileName})
|
|
|
- // return res.json({success: 'S', data: fileData, name: data.result.dispFileName})
|
|
|
- // res.setHeader('Content-disposition', 'attachment; filename=' + data.result.dispFileName);
|
|
|
- // res.setHeader('Content-type', mime.lookup(data.result.dispFileName));
|
|
|
- // res.pipe(fileData);
|
|
|
}
|
|
|
else {
|
|
|
let name = '';
|
|
@@ -751,12 +786,9 @@ serverApp.post('/api/download',
|
|
|
}
|
|
|
}
|
|
|
else {
|
|
|
- return res.json({message:'다운로드 파일 정보를 확인 할 수 없습니다.', success:'F'})
|
|
|
+ throw new NotFound('다운로드 파일 정보를 확인 할 수 없습니다.');
|
|
|
+ // return res.json({message:'다운로드 파일 정보를 확인 할 수 없습니다.', success:'F'})
|
|
|
}
|
|
|
- // }
|
|
|
- // catch(error) {
|
|
|
- // return res.json({success: 'F', message:'파일 다운로드 중 오류가 발생하였습니다.<br>'+ getErrorMessage(error)});
|
|
|
- // }
|
|
|
}
|
|
|
));
|
|
|
|