venice-main-classic/scripts/profile_photo.js

139 lines
5.1 KiB
JavaScript

// The contents of this file are subject to the Mozilla Public License Version 1.1
// (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
//
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
// WARRANTY OF ANY KIND, either express or implied. See the License for the specific
// language governing rights and limitations under the License.
//
// The Original Code is the Venice Web Communities System.
//
// The Initial Developer of the Original Code is Eric J. Bowersox <erbo@users.sf.net>,
// for Silverwrist Design Studios. Portions created by Eric J. Bowersox are
// Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved.
//
// Contributor(s):
importClass(java.awt.Dimension);
importPackage(Packages.com.silverwrist.util);
importPackage(Packages.com.silverwrist.util.image);
importPackage(Packages.com.silverwrist.venice.core);
importPackage(Packages.com.silverwrist.venice.except);
importPackage(Packages.com.silverwrist.venice.ui);
importPackage(Packages.com.silverwrist.venice.ui.helpers);
importPackage(Packages.com.silverwrist.venice.ui.view);
// Get the request object and the target URI.
rinput = bsf.lookupBean("request");
target = rinput.getParameter("tgt");
if (target==null)
target = "top.js.vs";
// If the user is not logged in, this is an error.
user = rinput.user;
if (!(user.isLoggedIn()))
{ // user not logged in - just bail on this
vlib.output(new ErrorBox(null,"You must log in before you can modify the profile on your account."));
vlib.done();
} // end if
rc = null;
if ("GET"==rinput.verb)
{ // create a PhotoUploader to do the photo uploading
try
{ // create and initialize the PhotoUploader
rc = new PhotoUploader("Upload User Photo","profile_photo.js.vs",LinkTypes.SERVLET);
rc.addHiddenParameter("tgt",target);
html = vlib.queryHTMLRendering(rinput);
rc.photoTag = html.getUserPhotoTag(user.getContactInfo().photoURL);
rc.label = "New user photo";
rinput.displayLogin = false;
} // end try
catch (e)
{ // an exception in the output...
etype = vlib.exceptionType(e) + "";
if (etype.match("DataException"))
rc = new ErrorBox("Database Error","Database error generating display: " + e.message,target);
else
rc = e;
} // end catch
vlib.output(rc);
vlib.done();
} // end if
// If the cancel button was clicked, bounce out of here.
if (rinput.isImageButtonClicked("cancel"))
{ // user elected to cancel upload - bounce us out of here
vlib.output(new Redirect("profile.js.vs?tgt=" + vlib.encodeURL(target),LinkTypes.SERVLET));
vlib.done();
} // end if
if (rinput.isImageButtonClicked("upload"))
{ // do some shorts-checking in the parameters
if (!(rinput.isFileParam("thepic")))
{ // bogus file parameter
logger.error("Internal Error: 'thepic' should be a file param");
vlib.output(new ErrorBox(null,"Internal Error: 'thepic' should be a file param",
"profile.js.vs?tgt=" + vlib.encodeURL(target)));
vlib.done();
} // end if
mimetype = rinput.getParameterType("thepic") + "";
if (!(mimetype.match("image/")))
{ // not an image file type that got uploaded
logger.error("Error: 'thepic' not an image type");
vlib.output(new ErrorBox(null,"You did not upload an image file. Try again.",
"profile_photo.js.vs?tgt=" + vlib.encodeURL(target)));
vlib.done();
} // end if
try
{ // normalize the photo to 100x100 pixels
real_pic = ImageNormalizer.normalizeImage(rinput.getParameterDataStream("thepic"),
rinput.engine.getUserPhotoSize(),"jpeg");
// set the user photo data!
ci = user.getContactInfo();
ci.setPhotoData(rinput.contextPath + "/imagedata/","image/jpeg",real_pic.length,real_pic.data);
user.putContactInfo(ci);
// Jump back to the profile form.
rc = new Redirect("profile.js.vs?tgt=" + vlib.encodeURL(target),LinkTypes.SERVLET);
} // end try
catch (e)
{ // exception while setting user photo
etype = vlib.exceptionType(e) + "";
if (etype.match("ServletMultipartException"))
rc = new ErrorBox(null,"Internal Error: " + e.message,"profile.js.vs?tgt=" + vlib.encodeURL(target));
else if (etype.match("ImageNormalizerException"))
rc = new ErrorBox(null,e.message,"profile_photo.js.vs?tgt=" + vlib.encodeURL(target));
else if (etype.match("DataException"))
rc = new ErrorBox("Database Error","Database error storing user photo: " + e.message,
"profile.js.vs?tgt=" + vlib.encodeURL(target));
else if (etype.match("EmailException"))
rc = new ErrorBox(null,"Internal Error: " + e.message,"profile.js.vs?tgt=" + vlib.encodeURL(target));
else
rc = e;
} // end catch
} // end if
else
{ // don't know the command button!
logger.error("no known button click on POST to profile_photo.js");
rc = new ErrorBox("Internal Error","Unknown command button pressed",
"profile.js.vs?tgt=" + vlib.encodeURL(target));
} // end else
vlib.output(rc); // all done!