Toggle navigation
Toggle navigation
This project
Loading...
Sign in
2020-1-CloudComputing
/
D_Team_Khuloud
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
박기범
2020-06-09 00:34:20 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
25994b84e7505a89ed2126b281aef7a034acb73c
25994b84
1 parent
aebdde15
modify file api
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
97 additions
and
9 deletions
backend/routes/file/downloadFile.js
backend/routes/file/files.js
backend/routes/file/modifyFile.js
backend/routes/file/router.js
backend/routes/file/uploadFile.js
backend/routes/file/downloadFile.js
View file @
25994b8
...
...
@@ -2,6 +2,7 @@ var express = require('express');
var
router
=
express
.
Router
();
var
fs
=
require
(
'fs'
);
var
moment
=
require
(
'moment'
);
var
AWS
=
require
(
'aws-sdk'
);
AWS
.
config
.
loadFromPath
(
__dirname
+
"/../modules/awsconfig.json"
);
...
...
@@ -19,7 +20,14 @@ router.get('/:name', function (req, res) {
S3
.
downloadFile3
(
S3
.
BUCKET_NAME
,
user_id
,
targetFile
,
function
(
result
,
downloadDir
)
{
if
(
result
)
{
res
.
send
({
src
:
downloadDir
})
var
sql
=
'UPDATE files SET recent_access=(?) WHERE user_id=(?) AND location=(?) AND file_name=(?)'
;
connection
.
query
(
sql
,
[
moment
().
format
(),
user_id
,
curPath
,
file_name
],
function
(
err
){
if
(
err
){
res
.
send
({
err
:
'update error'
});
}
else
{
res
.
send
({
src
:
downloadDir
})
}
})
}
else
{
res
.
send
({
err
:
'download error'
})
}
...
...
backend/routes/file/files.js
View file @
25994b8
...
...
@@ -35,13 +35,20 @@ router.get('/:name', function (req, res) {
var
extension
=
targetFile
.
split
(
'.'
)[
1
].
toLowerCase
();
S3
.
downloadFile
3
(
S3
.
BUCKET_NAME
,
user_id
,
targetFile
,
function
(
result
,
downloadDir
){
S3
.
downloadFile
2
(
S3
.
BUCKET_NAME
,
user_id
,
targetFile
,
function
(
result
,
downloadDir
){
if
(
result
){
if
(
extension
==
'jpg'
||
extension
==
'jpeg'
||
extension
==
'png'
)
{
res
.
status
(
200
).
send
({
type
:
'image'
,
src
:
downloadDir
})
}
else
{
res
.
status
(
200
).
send
({
type
:
'text'
,
src
:
downloadDir
})
}
var
content
;
content
=
fs
.
readFileSync
(
downloadDir
,
'utf8'
);
res
.
send
({
file_name
:
file_name
,
content
:
content
});
//if (extension == 'jpg' || extension == 'jpeg' || extension == 'png') {
// res.send({ type: 'image', src: downloadDir})
//}else{
// var content;
// content = fs.readFileSync(downloadDir, 'utf8');
// res.send({file_name: file_name, content: content});
// //res.status(200).send({type: 'text', src: downloadDir})
//}
}
})
});
...
...
backend/routes/file/modifyFile.js
0 → 100644
View file @
25994b8
var
express
=
require
(
'express'
);
var
router
=
express
.
Router
();
var
AWS
=
require
(
'aws-sdk'
);
AWS
.
config
.
loadFromPath
(
__dirname
+
"/../modules/awsconfig.json"
);
var
S3
=
require
(
'../modules/s3/s3'
);
var
fs
=
require
(
'fs'
);
var
moment
=
require
(
'moment'
);
// /file/modify/:name
router
.
post
(
'/:name'
,
function
(
req
,
res
){
var
user_id
=
req
.
body
.
user_id
;
var
curPath
=
req
.
body
.
cur
;
// /folder1/folder2/
var
file_name
=
req
.
params
.
name
;
var
modified_name
=
req
.
body
.
name
;
var
modified_content
=
req
.
body
.
content
;
var
targetPath
;
if
(
curPath
==
'/'
)
{
targetPath
=
''
;
}
else
{
targetPath
=
curPath
.
substring
(
1
,
curPaht
.
length
-
1
);
// folder1/folder2
}
var
originalDir
=
__dirname
+
'/../modules/s3/download/'
+
user_id
+
curPath
+
file_name
;
var
tempDownloadDir
;
// 파일 이름 변경
S3
.
renameFile
(
S3
.
BUCKET_NAME
,
user_id
,
file_name
,
modified_name
,
targetPath
,
function
(
result
,
r_modified_name
){
if
(
result
){
modified_name
=
r_modified_name
;
tempDownloadDir
=
__dirname
+
'/../modules/s3/download/'
+
user_id
+
curPath
+
modified_name
;
fs
.
unlink
(
originalDir
,
function
(
err
){
if
(
err
){
console
.
log
(
err
);
res
.
send
({
error
:
'original file not exists in server'
});
}
else
{
// 파일 내용 변경
fs
.
writeFileSync
(
tempDownloadDir
,
modified_content
);
S3
.
coverFile
(
S3
.
BUCKET_NAME
,
user_id
,
modified_name
,
targetPath
,
tempDownloadDir
,
function
(
result
)
{
if
(
result
)
{
var
sql
=
'UPDATE files SET file_name=(?), updated=(?), recent_access=(?) WHERE user_id=(?) AND location=(?) AND file_name=(?)'
;
connection
.
query
(
sql
,
[
modified_name
,
moment
().
format
(),
moment
().
format
(),
user_id
,
curPath
,
file_name
],
function
(
err
)
{
if
(
err
)
{
console
.
log
(
err
);
res
.
send
({
error
:
'update error'
});
}
else
{
fs
.
unlinkSync
(
tempDownloadDir
);
res
.
send
(
'modify file success'
);
}
})
}
else
{
res
.
send
({
error
:
'modify file failed'
});
}
})
}
});
}
else
{
res
.
send
({
error
:
'rename error'
});
}
})
});
module
.
exports
=
router
;
\ No newline at end of file
backend/routes/file/router.js
View file @
25994b8
...
...
@@ -6,12 +6,14 @@ var files = require('./files');
var
uploadFile
=
require
(
'./uploadFile'
);
var
downloadFile
=
require
(
'./downloadFile'
);
var
deleteFile
=
require
(
'./deleteFile'
);
var
modifyFile
=
require
(
'./modifyFile'
);
router
.
use
(
'/'
,
files
);
router
.
use
(
'/upload'
,
uploadFile
);
router
.
use
(
'/download'
,
downloadFile
);
router
.
use
(
'/delete'
,
deleteFile
);
router
.
use
(
'/modify'
,
modifyFile
);
module
.
exports
=
router
;
\ No newline at end of file
...
...
backend/routes/file/uploadFile.js
View file @
25994b8
...
...
@@ -2,6 +2,7 @@ var express = require('express');
var
router
=
express
.
Router
();
var
formidable
=
require
(
'formidable'
);
var
moment
=
require
(
'moment'
);
var
AWS
=
require
(
'aws-sdk'
);
AWS
.
config
.
loadFromPath
(
__dirname
+
"/../modules/awsconfig.json"
);
...
...
@@ -43,6 +44,7 @@ router.post('/', function (req, res) {
}
else
{
sourceFiles
.
push
(
files
.
file
.
name
);
bodies
.
push
(
files
.
file
.
path
);
console
.
log
(
'bodies'
,
bodies
);
}
}
else
{
for
(
var
file
of
files
.
file
)
{
// 파일 여러개일 때
...
...
@@ -65,8 +67,8 @@ router.post('/', function (req, res) {
}
}
for
(
var
sourceFile
of
noErrSourceFiles
)
{
var
sql
=
'INSERT INTO files (file_name, user_id, location
) VALUES (
?, ?, ?)'
;
connection
.
query
(
sql
,
[
sourceFile
,
user_id
,
curPath
],
function
(
err
,
result
)
{
var
sql
=
'INSERT INTO files (file_name, user_id, location
, recent_access) VALUES (?,
?, ?, ?)'
;
connection
.
query
(
sql
,
[
sourceFile
,
user_id
,
curPath
,
moment
().
format
()
],
function
(
err
,
result
)
{
if
(
err
)
{
console
.
log
(
'insert error'
);
res
.
send
({
error
:
'insert error'
});
...
...
Please
register
or
login
to post a comment