Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
VILLASweb-backend-node
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Container registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
ACS
Public
VILLASframework
VILLASweb-backend-node
Commits
8d1fb37a
Commit
8d1fb37a
authored
8 years ago
by
Ricardo Hernandez
Browse files
Options
Downloads
Patches
Plain Diff
ensure public folder exists in docker image. Files directories per user, authenticated upload
parent
d91bde59
No related branches found
No related tags found
1 merge request
!3
Image widget fix
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
Dockerfile
+11
-3
11 additions, 3 deletions
Dockerfile
config.js
+1
-0
1 addition, 0 deletions
config.js
routes/upload.js
+52
-34
52 additions, 34 deletions
routes/upload.js
with
64 additions
and
37 deletions
Dockerfile
+
11
−
3
View file @
8d1fb37a
FROM
node:latest
# Bundle app source
COPY
. .
# Create app directory
RUN
mkdir
-p
/usr/src/app
WORKDIR
/usr/src/app
# Install dependencies
# Install app dependencies
COPY
package.json /usr/src/app/
RUN
npm
install
# Create public directory
RUN
mkdir
-p
/usr/src/app/public
# Bundle app source
COPY
. /usr/src/app
# Run the app
EXPOSE
4000
CMD
[ "npm", "start" ]
This diff is collapsed.
Click to expand it.
config.js
+
1
−
0
View file @
8d1fb37a
...
...
@@ -20,6 +20,7 @@
******************************************************************************/
module
.
exports
=
{
publicDir
:
'
../public
'
,
development
:
{
databaseName
:
'
VILLAS
'
,
databaseURL
:
'
mongodb://localhost:27017/
'
,
...
...
This diff is collapsed.
Click to expand it.
routes/upload.js
+
52
−
34
View file @
8d1fb37a
...
...
@@ -25,64 +25,82 @@ var path = require('path');
var
formidable
=
require
(
'
formidable
'
);
var
fs
=
require
(
'
fs
'
);
//var auth = require('../auth');
// load configuration
var
config
=
require
(
'
../config
'
);
var
auth
=
require
(
'
../auth
'
);
//
var User = require('../models/user');
var
User
=
require
(
'
../models/user
'
);
var
File
=
require
(
'
../models/file
'
);
// create router
var
router
=
express
.
Router
();
// all user routes need authentication
router
.
use
(
'
/upload
'
,
auth
.
validateToken
);
// serve public files
router
.
use
(
express
.
static
(
path
.
join
(
__dirname
,
'
../public
'
)));
let
publicDir
=
path
.
join
(
__dirname
,
config
.
publicDir
);
router
.
use
(
express
.
static
(
publicDir
));
// routes
router
.
post
(
'
/upload
'
,
/*auth.validateToken,*/
function
(
req
,
res
)
{
// create form object
var
form
=
new
formidable
.
IncomingForm
();
form
.
uploadDir
=
path
.
join
(
__dirname
,
'
../public
'
);
router
.
post
(
'
/upload
'
,
auth
.
validateRole
(
'
visualization
'
,
'
update
'
),
function
(
req
,
res
)
{
// register events
form
.
on
(
'
file
'
,
function
(
field
,
file
)
{
console
.
log
(
file
);
// find upload author
User
.
findOne
({
_id
:
req
.
decoded
.
_doc
.
_id
},
function
(
err
,
user
)
{
if
(
err
)
{
console
.
error
(
'
Could find author while uploading
'
,
err
);
res
.
status
(
500
).
send
({
success
:
false
,
message
:
'
File could not be uploaded.
'
});
}
//fs.rename(file.path, path.join(form.uploadDir, /*req.decoded._doc._id + '_' +*/ file.name));
// create form object with the upload dir corresponding to user's
var
form
=
new
formidable
.
IncomingForm
();
let
userFolder
=
path
.
join
(
publicDir
,
user
.
_id
+
''
);
// ensure is a string
form
.
uploadDir
=
userFolder
;
// find user
/*User.findOne({ _id: req.decoded._doc._id }, function(err, user) {
if (err) {
console.log(err);
}*/
form
.
on
(
'
error
'
,
function
(
error
)
{
console
.
error
(
'
Error while processing incoming form
'
,
error
);
res
.
status
(
400
).
send
({
success
:
false
,
message
:
'
File could not be uploaded.
'
});
});
// create file object
var
fileObj
=
new
File
({
name
:
file
.
name
,
path
:
'
public/
'
+
/*user._id + '_' +*/
file
.
name
/*, user: user._id*/
});
form
.
on
(
'
file
'
,
function
(
field
,
file
)
{
// create file object, assigning path to userID + formidable's created name
let
filePath
=
path
.
join
(
user
.
_id
+
''
,
path
.
basename
(
file
.
path
));
var
fileObj
=
new
File
({
name
:
file
.
name
,
path
:
filePath
});
fileObj
.
save
(
function
(
err
)
{
if
(
err
)
{
console
.
log
(
err
);
console
.
error
(
'
Error while storing reference to uploaded file
'
,
err
);
res
.
status
(
500
).
send
({
success
:
false
,
message
:
'
File could not be uploaded.
'
});
}
/*
user.files.push(fileObj._id);
user
.
files
.
push
(
fileObj
.
_id
);
user
.
save
(
function
(
err
)
{
if
(
err
)
{
console.log(err);
console
.
error
(
'
Error while updating user
\'
s files references
'
,
err
);
res
.
status
(
500
).
send
({
success
:
false
,
message
:
'
File could not be uploaded.
'
});
}
});
});*/
});
});
form
.
on
(
'
error
'
,
function
(
error
)
{
console
.
log
(
'
Error uploading file:
'
+
error
);
res
.
status
(
403
).
send
({
success
:
false
,
message
:
'
Error uploading file:
'
+
error
});
});
form
.
on
(
'
end
'
,
function
()
{
res
.
send
({
success
:
true
,
message
:
'
File uploaded
'
});
});
// Make sure user's folder exists
fs
.
mkdir
(
userFolder
,
function
(
err
)
{
// error occurred (ignore already existing)
if
(
err
&&
err
.
code
!=
'
EEXIST
'
)
{
res
.
status
(
500
).
send
({
success
:
false
,
message
:
'
File could not be uploaded.
'
});
return
;
}
// handle the request
form
.
parse
(
req
);
});
});
});
module
.
exports
=
router
;
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment