ad_page_contract {

@author Nick Kokkos
Oh well, talk about some code reuse. Most of this here is copied from photo-album's procs
I also took a peek at photo-album-lite's code to see how I can upload a file under www/images

Basically, I upload the files and save them under www/images. I create the corresponding thumbs and save
them under www/images/thumbs and save their paths into the database. It's a quick, dumb and 
crude way. I just wanted to get this working as fast as possible.

I don't use the content repository's functionality, although that would be great. We could, 
for example have different instances running with different pictures. Next version should
do that.


} { upload_file:optional 
    upload_file.tmpfile:optional,tmpfile
}

   set package_id [ad_conn package_id]

   permission::require_permission \
          -object_id $package_id \
          -privilege admin

  if [empty_string_p $upload_file] {
        set upload_file $upload_file.tmpfile
    }

    # strip off the C:\directories... crud and just get the file name
    if ![regexp {([^/\\]+)$} $upload_file match client_filename] {
        # couldn't find a match
        set client_filename $upload_file
    }

   regexp {(.*)/[^/]+} [ns_info pageroot] match server_base
	
   set tmp_filename [ns_queryget upload_file.tmpfile]

   # perhaps this is a dumb way of uploading files. Perhaps someone can point out a better way?
   
   set this_package_key [ad_conn package_key]

   set path_to_file_thumb "$server_base/packages/$this_package_key/www/images/thumbs/$client_filename"
   set viewfilepath_thumb "images/thumbs/$client_filename" 
   #ns_cp $tmp_filename $path_to_file

   # Handle thumbnail file. Thumbnail size is fixed. It's used by Jon's 
   # gallery to build the carousel 
    set geometry "100x75" 
    exec [parameter::get -parameter ImageMagickPath]/convert -geometry $geometry -interlace None -sharpen 1x2 $tmp_filename $path_to_file_thumb

   # Handle viewer file 
   set path_to_file "$server_base/packages/$this_package_key/www/images/$client_filename"
   set viewfilepath "images/$client_filename" 
   set geometry [parameter::get -parameter Width_Height]
   
    # If we get an old style single number
   if {[regexp {^[0-9]+$} $geometry]} {
   	set geometry ${geometry}x${geometry}
      }

  
   exec [parameter::get -parameter ImageMagickPath]/convert -geometry $geometry -interlace None -sharpen 1x2 $tmp_filename $path_to_file

   db_dml do_insert "insert into slideshowgallery (filepath_thumb,viewfilepath_thumb,filepath,viewfilepath) values (:path_to_file_thumb,:viewfilepath_thumb,:path_to_file,:viewfilepath)"

   ad_returnredirect photos-list



