Permalink
Browse files

File Uploads

  • Loading branch information...
1 parent d147765 commit 54577722728366cc1b611b7e39c3605f56601ffb @robrighter committed Jan 21, 2011
Showing with 133 additions and 46 deletions.
  1. +25 −4 datastore.js
  2. +90 −35 index.js
  3. +1 −1 lib/formidable
  4. +5 −2 static/admin/css/style.css
  5. +4 −0 static/admin/js/script.js
  6. +8 −4 static/admin/views/edit.ejs
View
29 datastore.js
@@ -23,7 +23,6 @@
methods : {
getChildren: function(callback){
NodeCMSContentGraph.find({ parentid: that.convertIdToString(this._id) }).all(function(result){
- console.log(sys.inspect(result));
callback(result);
});
}
@@ -205,14 +204,36 @@
else{
//still going keep iterating
var tofind = { parentid: that.convertIdToString(cursor._id), slug: path[i++] };
- console.log('Still not there, now we are looking for: ' + sys.inspect(tofind));
NodeCMSContentGraph.find(tofind).first(iter);
}
};
//alright lets kick it off by grabing the root item (the one with a parentid of 0)
NodeCMSContentGraph.find({ parentid: 0 }).first(iter);
}
+ this.getPathForItem = function(item, callback){
+ path = [];
+ //make an iter function that will be run recursively
+ var iter = function(result){
+ console.log('ITERATING with: ' + sys.inspect(result));
+ if(result.parentid == 0){
+ if(path.length > 0){
+ //this is the first one, so the path is actually /
+ callback('/');
+ }
+ else{
+ callback(path.join('/'));
+ }
+ }
+ else{
+ //still not there yet so lets add the slug and move on
+ path.push(result.slug);
+ NodeCMSContentGraph.find({ __id: result.parentid }).first(iter);
+ }
+ }
+ iter(item);
+ }
+
function getUserByUsername(username, callback){
User.find({ username: username }).first(function(result){
callback(result);
@@ -287,8 +308,8 @@
function Image(value){
this.adminLabel = adminlabel;
this.adminOrder = adminorder;
- this.url = value;
- this.alt = '';
+ this.filename = value.filename;
+ this.alt = value.alt;
this.type = this.constructor.name;
}
return function(v){ return new Image(v); };
View
125 index.js
@@ -21,9 +21,6 @@
/////////////////////////////////////////////////////
this.setupExpressJSRoutes = function(server){
- //add in the form handler
- server.use(form({ keepExtensions: true }));
-
//main page loader route
server.get(new RegExp("^([a-zA-Z0-9\-\/]*)$"), function(req,res,next){
findContentOrPassToNext(req.params[0], next, function(result){
@@ -38,39 +35,83 @@
});
//REST Resources
- server.put("/_content", function(req,res,next){
- var doc = processIncomingBody(req.body);
- ds.updateContent(doc.__id, doc, function(result){
- if(result){
- res.send({status: true});
- }
- else{
- res.send({status: false})
- }
- });
+ server.post("/_content/UPDATE", function(req,res,next){
+ var isform = processForm(req, function(err, fields, files){
+ console.log('The arguements: ' + sys.inspect(arguments));
+ if(err){
+ req.flash('warning', 'Oops. The page encountered an error and was not saved.');
+ res.redirect('back');
+ }
+ var doc = processIncomingFormFields(fields, files);
+ ds.updateContent(doc.__id, doc, function(result){
+ if(result){
+ req.flash('confirm', 'Success! The page has been saved.');
+ res.redirect('back');
+ //res.send({status: true});
+ }
+ else{
+ req.flash('warning', 'Oops. The page encountered an error and was not saved.');
+ res.redirect('back');
+ }
+ });
+ })
+
+ if(!isform){
+ //no form was submitted so lets pass it on for a 404
+ next();
+ }
});
- server.post("/_content", function(req,res,next){
- var doc = processIncomingBody(req.body);
- //make our content item and populate it with the form field values
- var content = ds.makeContent(doc.__contenttype);
- content.getProperties().forEach(function(item){
- if(doc.hasOwnProperty(item.name)){
- content[item.name] = doc[item.name];
- }
- });
- ds.addContentToSitemap(content, doc.__parentid, doc.__template, function(result){
- if(result){
- res.send({status: true, newslug: result.slug});
- }
- else{
- res.send({status: false})
- }
- });
+ server.post("/_content/ADD", function(req,res,next){
+ var isform = processForm(req, function(err, fields, files){
+ if(err){
+ req.flash('warning', 'Oops. The page encountered an error and was not saved.');
+ res.redirect('back');
+ }
+ var doc = processIncomingFormFields(fields, files);
+ //make our content item and populate it with the form field values
+ var content = ds.makeContent(doc.__contenttype);
+ content.getProperties().forEach(function(item){
+ if(doc.hasOwnProperty(item.name)){
+ content[item.name] = doc[item.name];
+ }
+ });
+ ds.addContentToSitemap(content, doc.__parentid, doc.__template, function(result){
+ if(result){
+ ds.getPathForItem(result, function(path){
+ console.log('REDIRECTING TO: ' + '_admin/'+path);
+ req.flash('confirm', 'Success! The page has been saved.');
+ res.redirect('_admin/' + path);
+ });
+ //res.send({status: true, newslug: result.slug});
+ }
+ else{
+ req.flash('warning', 'Oops. The page encountered an error and was not saved.');
+ res.redirect('back');
+ }
+ });
+ })
+
+ if(!isform){
+ //no form was submitted so lets pass it on for a 404
+ next();
+ }
+
+
});
- function processIncomingBody(body){
- var doc = body;
+ function processForm(req, callback){
+ if(req.form){
+ req.form.complete(callback);
+ return true;
+ }
+ else{
+ return false;
+ }
+ }
+
+ function processIncomingFormFields(fields, files){
+ var doc = fields;
if(!doc.hasOwnProperty('__hidden_from_navigation')){
doc['__hidden_from_navigation'] = 'false';
}
@@ -80,20 +121,26 @@
else{
doc['__hidden_from_navigation'] = false;
}
+
+ for(key in files){
+ doc[key] = { filename: stripFileNameFromUploadPath(files[key].path), alt: '' };
+ }
+
+ console.log("FIELDS:");
console.log(sys.inspect(doc));
return doc;
}
//main admin page loader route
server.get(new RegExp("^\/_admin\/([a-zA-Z0-9\-\/]*)$"), function(req,res,next){
- console.log('The Query String is: ' + sys.inspect(req.query));
findContentOrPassToNext(req.params[0], next, function(result){
prepareModelResultForAdmin(result, function(locals){
locals.adminassets = settings.AdminAssetsWebPath;
locals.contenttypes = ds.contentTypes;
locals.sys = sys;
- locals.confirm = (req.query['confirm'] || '');
- locals.warning = (req.query['warning'] || '');
+ locals.uploadedfiles = settings.UploadedFilesWebPath;
+ locals.confirm = (req.flash('confirm') || '');
+ locals.warning = (req.flash('warning') || '');
res.render(settings.AdminAssetsFilePath + '/views/edit.ejs', {
layout: false,
locals : locals,
@@ -115,6 +162,7 @@
locals.adminassets = settings.AdminAssetsWebPath;
locals.sys = sys;
locals.templates = frontEndTemplates;
+ locals.uploadedfiles = settings.UploadedFilesWebPath;
locals.confirm = (req.params['confirm'] || '');
locals.warning = (req.params['warning'] || '');
locals.location = {}
@@ -174,6 +222,13 @@
}
});
}
+
+ function stripFileNameFromUploadPath(filepath){
+ return filepath.replace(settings.UploadedFiles, '').replace(/\//g, '');
+ }
+
+
+
}
2 lib/formidable
@@ -1 +1 @@
-Subproject commit f22993a40658b77ec74ece9e44679297469b144e
+Subproject commit 63347d825bbeb30f1effadf153e537a641c3d0d4
View
7 static/admin/css/style.css
@@ -300,8 +300,11 @@ textarea.prop {
}
.inputgroup .preview{
- width: 50px;
- float: right;
+ height: 70px;
+ position: absolute;
+ margin-left: 230px;
+ border-bottom: 1px solid #FFF;
+ border-radius: 10px;
}
.cta {
View
4 static/admin/js/script.js
@@ -7,8 +7,11 @@ $(document).ready(function() {
$("select, input:checkbox, input:radio, input:file").uniform();
$('#editor').submit( function(){
+ alert('HERE!!!');
$('.message').slideUp('fast');
var formvals = $('#editor').serialize();
+ console.log('SERIALIZED FORM VALS :');
+ console.log(formvals);
$.ajax({
type: "PUT",
url: "/_content",
@@ -26,6 +29,7 @@ $(document).ready(function() {
});
$('#makenew').submit( function(){
+ alert('HERE!!!');
$('.message').slideUp('fast');
var formvals = $('#makenew').serialize();
$.ajax({
View
12 static/admin/views/edit.ejs
@@ -39,8 +39,8 @@
%>
-
- <form id='<%= (content.isNew ? 'makenew' : 'editor' )%>'>
+ <!-- id='<%= (content.isNew ? 'makenew' : 'editor' )%>' -->
+ <form enctype="multipart/form-data" action="/_content/<%= (content.isNew ? 'ADD' : 'UPDATE' )%>" method='POST'>
<%
if(content.isNew){
%>
@@ -95,9 +95,13 @@
%>
<div class='inputgroup'>
<div class='label'><%=item.value.adminLabel%></div>
- <img class='preview' src="<%=item.value.url%>" />
+ <%
+ if(item.value.url != ''){
+ %><img class='preview' src="<%= (uploadedfiles +'/'+ item.value.filename) %>" /><%
+ }
+ %>
<div class='singlefile'>
- <input class="prop" type='file' name='<%=item.name%>' />
+ <input class="prop" type='file' id='uploadertest' name='<%=item.name%>' />
</div>
<div class='break'></div>

0 comments on commit 5457772

Please sign in to comment.