00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "cgiTemplates.h"
00010 #include "connectstring"
00011
00012 #include "siteLimit.h"
00013 #include "read_write_base.hpp"
00014
00015
00016 #include "openLogger.h"
00017 #include <Magick++.h>
00018 #include "ocFileSys.h"
00019 class rev_date_ocDirEntry
00020 {
00021 public:
00022 bool operator ( ) ( const ocDirEntry & d1, const ocDirEntry & d2 )
00023 {
00024 return d1.last_mod > d2.last_mod;
00025 }
00026 };
00027 rev_date_ocDirEntry rev_dates;
00028
00029 using namespace Magick;
00030 class imagePicker : public read_write_base
00031 {
00032 private:
00033 string m_imageBase;
00034 string m_thumbBase;
00035 string m_imageDir;
00036 string m_thumbDir;
00037 string m_domain;
00038 string m_urlDir;
00039 string m_urlThumbDir;
00040 string m_fileUploaded;
00041
00042
00043 int boundX;
00044 int boundY;
00045 int FSboundX;
00046 int FSboundY;
00047
00048
00049 FilterTypes filter;
00050 unsigned int quality;
00051
00052 public:
00053
00054 string m_relPath;
00055 bool m_isBasePathed;
00056
00057
00058 imagePicker(openLogin & login)
00059 :read_write_base(),m_imageBase("images"),m_thumbBase("small_images")
00060 ,boundX(120),boundY(120),FSboundX(0),FSboundY(0),m_isBasePathed(true)
00061 ,filter(LanczosFilter),quality(75)
00062 {
00063 string siteLimitation = siteFocus(login);
00064 string sql = "select path, domain, url from metasite.sites";
00065 if( siteLimitation.length() )
00066 {
00067 sql += " where id ";
00068 sql += siteLimitation;
00069 }
00070 writelog2( "SQL to get site path", sql );
00071
00072 if( rs.open(sql ) )
00073 {
00074 m_imageDir = rs.getField(0).format();
00075 m_thumbDir = rs.getField(0).format();
00076 m_domain = rs.getField(1).format();
00077 m_urlDir = rs.getField(2).format();
00078 m_urlThumbDir = rs.getField(2).format();
00079 m_imageDir += "/";
00080 m_thumbDir += "/";
00081
00082 m_imageDir += m_imageBase;
00083 m_thumbDir += m_thumbBase;
00084
00085 m_urlThumbDir += m_thumbBase;
00086 rs.close();
00087 }
00088 }
00089 virtual ~imagePicker(){;}
00090
00091 void checkForImagesToDelete(cgiScript & script)
00092 {
00093 cgiInput & args = script.ClientArguments();
00094 if( args.count("delete") > 0 && args["delete"].length() > 0)
00095 {
00096 string delPath = m_imageDir;
00097 delPath +="/";
00098 delPath += args["delete"].c_str();
00099 string delThumbPath = m_thumbDir;
00100 delThumbPath +="/";
00101 delThumbPath += args["delete"].c_str();
00102 ocFileSys fs;
00103 writelog2( "Deleting ", delPath );
00104
00105 if( !fs.remove(delPath) )
00106 {
00107 script << fs.check();
00108 script << " image path: " << delPath << endl;
00109 }
00110
00111 if( !fs.remove(delThumbPath) )
00112 {
00113 script << fs.check();
00114 script << " thumb path: " << delThumbPath << endl;
00115 }
00116 }
00117 }
00118
00119 bool isFile( ocString candidate )
00120 {
00121 bool bret;
00122 bret = candidate.regExMatch(".[a-z,A-Z]");
00123 return bret;
00124 }
00125
00126 void imageList(cgiScript & script)
00127 {
00128 cgiInput & args = script.ClientArguments();
00129
00130 cgiTemplates htmlDoc;
00131 writelog("Loading Templates/imagelist.htmp");
00132 htmlDoc.load("Templates/imagelist.htmp");
00133 char * Rows[2] = { "oddcell", "evencell" };
00134 writelog("Writing top of page");
00135 script << ocString(htmlDoc.getParagraph("top"))
00136 .replace( "__REPORT_TITLE_GOES_HERE", "Image Picker" )
00137 .replaceAll( "$relPath$", m_relPath.c_str() )
00138 .replaceAll( "$noBasePath$", m_isBasePathed ? "":"true" )
00139 << endl;
00140 script << "Path: " << m_thumbDir << "<BR>" << endl;
00141 ocString headitem = htmlDoc.getParagraph("headitem");
00142 string rowsep = htmlDoc.getParagraph("rowsep");
00143
00144 writelog("Writing headers");
00145
00146 script << headitem.replace("__hcell_label","Image");
00147 script << headitem.replace("__hcell_label","Name");
00148 script << headitem.replace("__hcell_label","Action");
00149 script << headitem.replace("__hcell_label","Delete");
00150
00151 ocString iLnk = "<img src='_img_'>";
00152
00153 ocString fLnk = "<a href='javascript:pickImage(\"_img_\")'>Pick</a></div>";
00154
00155 ocString delLink = "<a href='javascript:deleteImage(\"_img_\")'>"
00156 "[delete]"
00157 "</a>";
00158 ocFileSys fs;
00159 writelog2("Scanning image Directory",m_thumbDir);
00160 if( fs.openDir(m_thumbDir) )
00161 {
00162 ocDirectory & entries = fs.getDirectoryEntries();
00163
00164 sort( entries.begin(),
00165 entries.end(),
00166 rev_dates );
00167
00168
00169 string filter;
00170 if( args.count("filter") ) filter = args["filter"].c_str();
00171 int iEntries = entries.size();
00172 int iMax = iEntries?10:0;
00173 bool filesFound = false;
00174 bool showAll = args.count("showall")?true:false;
00175 if( filter.length() ) showAll = true;
00176
00177 for( int iRow = 0;
00178 ( showAll && iRow < iEntries) ||
00179 ( filesFound && iRow < iMax && iRow < iEntries ) ||
00180 ( !filesFound && iRow < iEntries );
00181 ++iRow )
00182 {
00183 if( isFile(entries[iRow].name) )
00184 {
00185 if( filter.length() )
00186 {
00187 ocString testString = entries[iRow].name;
00188 if( testString.regExMatch(filter.c_str()) == false)
00189 {
00190 continue;
00191 }
00192 }
00193 filesFound = true;
00194 string imgPath = m_imageDir;
00195 imgPath +="/";
00196 imgPath += entries[iRow].name;
00197
00198
00199 string imgLink;
00200 if(m_isBasePathed)
00201 {
00202 imgLink = m_urlDir;
00203 imgLink += m_relPath;
00204 }
00205 imgLink += entries[iRow].name;
00206
00207
00208 string absThumb = "http://";
00209 absThumb += m_domain;
00210 absThumb += m_urlThumbDir;
00211 absThumb += "/";
00212 absThumb += entries[iRow].name;
00213
00214 ocString row = htmlDoc.getParagraph(Rows[ iRow % 2 ] );
00215 script << rowsep;
00216
00217 script << row.replace( "__cell_data",
00218 iLnk.replace( "_img_",
00219 absThumb.c_str()
00220 ).c_str() );
00221 script << row.replace( "__cell_data", entries[iRow].name.c_str() );
00222 script << row.replace( "__cell_data",
00223 fLnk.replace( "_img_",
00224 imgLink.c_str()
00225 ).c_str() );
00226 script << row.replace( "__cell_data",
00227 delLink.replaceAll( "_img_",
00228 entries[iRow].name.c_str()
00229 ).c_str() );
00230 }
00231 }
00232 }
00233 script << htmlDoc.getParagraph("bottom") << endl;
00234 }
00235 string imageDir( void )
00236 {
00237 return m_imageDir;
00238 }
00239 string imageBase( void )
00240 {
00241 return m_imageBase;
00242 }
00243
00244 void makeThumb( cgiScript & script )
00245 {
00246 if( script.RequestMethod().upper() == "POST" && script.ClientArguments().count("File") )
00247 {
00248
00249
00250 cgiInput & args = script.ClientArguments();
00251 if( args.count( "boundX" ) )
00252 {
00253 boundX = atoi(script.ClientArguments()["boundX"].c_str());
00254 }
00255 if( args.count( "boundY" ) )
00256 {
00257 boundY = atoi(script.ClientArguments()["boundY"].c_str());
00258 }
00259
00260 ocString files = script.ClientArguments()["File"].c_str();;
00261 m_fileUploaded = files.parse("|");
00262 while (m_fileUploaded.length())
00263 {
00264 string imgPath = m_imageDir;
00265 imgPath +="/";
00266 imgPath += m_fileUploaded;
00267 string smlPath = m_thumbDir;
00268 smlPath +="/";
00269 smlPath += m_fileUploaded;
00270 try
00271 {
00272 Image image;
00273
00274 image.read(imgPath);
00275
00276 Geometry geom( boundX, boundY );
00277
00278 geom.greater(true);
00279
00280 geom.aspect(false);
00281
00282
00283 image.filterType(filter);
00284 image.quality(quality);
00285
00286
00287 image.scale(geom);
00288
00289 image.write(smlPath);
00290 }
00291 catch( Exception &error_ )
00292 {
00293 script << "Error creating thumbnail - Exception caught: " << error_.what() << endl;
00294 }
00295
00296
00297 m_fileUploaded = files.parse("|");
00298 }
00299 }
00300 }
00301 void setArgs( cgiScript & script )
00302 {
00303 if( script.ClientArguments().count("FSboundX") &&
00304 script.ClientArguments().count("FSboundY") )
00305 {
00306 FSboundX = atoi(script.ClientArguments()["FSboundX"].c_str());
00307 FSboundY = atoi(script.ClientArguments()["FSboundY"].c_str());
00308 if( script.ClientArguments().count("Quality") )
00309 { quality = atoi(script.ClientArguments()["Quality"].c_str());;
00310 }
00311 if( script.ClientArguments().count("FilterType") )
00312 { filter = (MagickLib::FilterTypes) atoi(script.ClientArguments()["FilterType"].c_str());;
00313 }
00314 }
00315 if( script.ClientArguments().count("boundX") &&
00316 script.ClientArguments().count("boundY") )
00317 {
00318 boundX = atoi(script.ClientArguments()["boundX"].c_str());
00319 boundY = atoi(script.ClientArguments()["boundX"].c_str());
00320 }
00321 }
00322 void scaleSource( cgiScript & script )
00323 {
00324 if( script.RequestMethod().upper() == "POST" &&
00325 script.ClientArguments().count("File") )
00326 {
00327
00328
00329 ocString files = script.ClientArguments()["File"].c_str();
00330 FSboundX = atoi(script.ClientArguments()["FSboundX"].c_str());
00331 FSboundY = atoi(script.ClientArguments()["FSboundY"].c_str());
00332
00333 m_fileUploaded = files.parse("|");
00334 while (m_fileUploaded.length())
00335 {
00336 string imgPath = m_imageDir;
00337 imgPath +="/";
00338 imgPath += m_fileUploaded;
00339
00340 try
00341 {
00342 Image image;
00343
00344 image.read(imgPath);
00345
00346 Geometry geom( FSboundX, FSboundY );
00347
00348 geom.greater(true);
00349
00350 geom.aspect(false);
00351
00352
00353 image.filterType(filter);
00354 image.quality(quality);
00355
00356
00357 image.scale(geom);
00358
00359 image.write(imgPath);
00360 }
00361 catch( Exception &error_ )
00362 {
00363 script << "Error sizing Full Scale Image - Exception caught: " << error_.what() << endl;
00364 }
00365
00366 m_fileUploaded = files.parse("|");
00367 }
00368 }
00369 }
00370
00371 };
00372
00373 int main( int argcount, char ** args )
00374 {
00375
00376
00377
00378 openLogin login;
00379 imagePicker picker(login);
00380 cgiScript script("text/html", false, picker.imageDir().c_str());
00381 writelog("OK - script made");
00382
00383 if ( login.testLoginStatus() )
00384 {
00385 script.closeHeader();
00386 cgiInput & args = script.ClientArguments();
00387
00388 if( args.count( "relPath" ) )
00389 {
00390 picker.m_relPath = args["relPath"].c_str();
00391 }
00392
00393
00394 if( args.count( "noBasePath" ) )
00395 {
00396 picker.m_isBasePathed = false;
00397 }
00398
00399
00400 picker.setArgs( script );
00401 picker.makeThumb( script );
00402 picker.scaleSource( script );
00403
00404 writelog("Checking for images to delete");
00405 picker.checkForImagesToDelete(script);
00406
00407 writelog("Generating image list");
00408 picker.imageList(script);
00409
00410 writelog("Done");
00411 }
00412 else
00413 {
00414 script.Redirect("signIn.html");
00415 script.closeHeader();
00416 writelog("INVALID ACCESS ATTEMPT");
00417 }
00418 writelog("OK - All Done");
00419 return(0);
00420 }
00421
00422 #include "read_write_base.cpp"
00423