pa_load_images (public)

 pa_load_images [ -remove remove ] [ -client_name client_name ] \
    [ -strip_prefix strip_prefix ] [ -description description ] \
    [ -story story ] [ -caption caption ] \
    [ -feedback_mode feedback_mode ] [ -package_id package_id ] \
    image_files album_id user_id

Defined in packages/photo-album/tcl/photo-album-procs.tcl

load a list of files to the provided album owned by user_id

Switches:
-remove (optional, defaults to "0")
1 to delete the file after moving to the content repository
-client_name (optional)
provide the name of the upload file (for individual uploads)
-strip_prefix (optional)
the prefix to remove from the filename (for expanded archives)
-description (optional)
-story (optional)
-caption (optional)
-feedback_mode (optional, defaults to "0")
to provide much info of the loading process on a bulk upload
-package_id (optional)
Optionally specify the package_id owning the album, if this is not called from a page within the photo-album package itself.
Parameters:
image_files (required)
list of files to process
album_id (required)
user_id (required)

Partial Call Graph (max 5 caller/called nodes):
%3 packages/photo-album/www/photo-add-2.tcl packages/photo-album/ www/photo-add-2.tcl pa_load_images pa_load_images packages/photo-album/www/photo-add-2.tcl->pa_load_images packages/photo-album/www/photos-add-2.tcl packages/photo-album/ www/photos-add-2.tcl packages/photo-album/www/photos-add-2.tcl->pa_load_images ad_conn ad_conn (public) pa_load_images->ad_conn cr_create_content_file cr_create_content_file (public) pa_load_images->cr_create_content_file db_dml db_dml (public) pa_load_images->db_dml db_exec_plsql db_exec_plsql (public) pa_load_images->db_exec_plsql db_nextval db_nextval (public) pa_load_images->db_nextval

Testcases:
No testcase defined.
Source code:
 
    set new_ids [list]
    set peeraddr [ad_conn peeraddr]

    # Create the tmp dir if needed 
    if { $package_id eq "" } {
        set package_id [ad_conn package_id]
    }
    set tmp_path [parameter::get -parameter FullTempPhotoDir -package_id $package_id]
    if { ![file exists $tmp_path] } {
        ns_log Debug "pa_load_images: Making: tmp_photo_album_dir_path $tmp_path"
        file mkdir $tmp_path
    }

    # Fix upload name if missing
    foreach image_file $image_files {


        # Figure out what to call the file...
        if {$client_name eq ""} { 
            set upload_name $image_file
        } else { 
            set upload_name $client_name
        }
        if {$strip_prefix ne ""} { 
            regsub "^$strip_prefix" $upload_name {} upload_name
        }

        if {![regexp {([^/\\]+)$} $upload_name match client_filename]} {
            # couldn't find a match
            set client_filename $upload_name
        }

        if {[catch {set base_info [pa_file_info $image_file]} errMsg]} {
            ns_log Warning "pa_load_images: error parsing file data $image_file Error: $errMsg"
            error "pa_load_images: error parsing file data $image_file Error: $errMsg"
            continue
        }

        lassign $base_info base_bytes base_width base_height base_type base_mime base_colors base_quantum base_sha256
        
        # If we don't have a mime type we like we try to make a jpg or png 
        #
        if {$base_mime eq ""} { 
            set new_image [file join $tmp_path "tmp-[file rootname [file tail $image_file]]"]
            if {$base_colors ne "" && $base_colors < 257} { 
                # convert it to a png
                if {[catch {exec [parameter::get -parameter ImageMagickPath]/convert $image_file PNG:$new_image.png} errMsg]} { 
                    ns_log Warning "pa_load_images: Failed convert to PNG for $image_file (magicktype $base_type)" 
                }
                if { $remove } { 
                    file delete $image_file
                } 
                set image_file $new_image.png
                set remove 1
            } elseif {$base_colors ne "" && $base_colors > 256} { 
                # convert it to a jpg
                if {[catch {exec [parameter::get -parameter ImageMagickPath]/convert $image_file JPG:$new_image.jpg} errMsg]} { 
                    ns_log Warning "pa_load_images: failed convert to JPG for $image_file (magicktype $base_type)" 
                }
                if { $remove } { 
                    file delete $image_file
                } 
                set image_file $new_image.jpg
                set remove 1
            } else { 
                ns_log Warning "pa_load_images: is this file even an image: $image_file $base_type"
            }

            # get info again
            lassign [pa_file_info $image_file] base_bytes base_width base_height base_type base_mime base_colors base_quantum base_sha256
        }
        
        if {$base_mime eq "image/jpeg"} { 
            array set exif [pa_get_exif_data ${image_file}]
        } else { 
            array unset exif
        }

        set BaseExt [string tolower $base_type]
        
        if {$base_mime eq ""} { 
            ns_log Debug "pa_load_images: invalid image type $image_file $type even after convert!"
            continue 
        } 
          
        # Get all the IDs we will need 
        #
        foreach name [list photo_id photo_rev_id base_item_id base_rev_id thumb_item_id  thumb_rev_id viewer_item_id viewer_rev_id] { 
            set $name [db_nextval "acs_object_id_seq"]
        }
        
        # Set the names we use in the content repository.
        #
        set image_name "${photo_rev_id}:$client_filename"
        set base_image_name "base_$client_filename"
        set vw_image_name "vw_$client_filename"
        set th_image_name "th_$client_filename"
        
        # Handle viewer file 
        #
        set viewer_size [parameter::get -parameter ViewerSize -package_id $package_id]
        set viewer_filename [pa_make_file_name -ext $BaseExt $viewer_rev_id]
        set full_viewer_filename [file join ${tmp_path} ${viewer_filename}]
        pa_make_new_image $image_file ${full_viewer_filename} $viewer_size
        foreach {viewer_bytes viewer_width viewer_height viewer_type viewer_mime viewer_colors viewer_quantum viewer_sha256} [pa_file_info $full_viewer_filename] {}

        # Handle thumb file 
        #
        set thumb_size [parameter::get -parameter ThumbnailSize -package_id $package_id]
        set thumb_filename [pa_make_file_name -ext $BaseExt $thumb_rev_id]
        set full_thumb_filename [file join $tmp_path $thumb_filename]
        pa_make_new_image ${full_viewer_filename} ${full_thumb_filename} $thumb_size
        foreach {thumb_bytes thumb_width thumb_height thumb_type thumb_mime thumb_colors thumb_quantum thumb_sha256} [pa_file_info $full_thumb_filename] {}

        # copy the tmp file to the cr's file-system
        set thumb_filename_relative [cr_create_content_file -move $thumb_item_id $thumb_rev_id ${full_thumb_filename}]
        set viewer_filename_relative [cr_create_content_file -move $viewer_item_id $viewer_rev_id ${full_viewer_filename}]
        if { $remove } { 
            set base_filename_relative [cr_create_content_file -move $base_item_id $base_rev_id $image_file]
        } else { 
            set base_filename_relative [cr_create_content_file $base_item_id $base_rev_id $image_file]
        }


        # Insert the mess into the DB
        #
        db_transaction {
            db_exec_plsql new_photo {
                declare 
                dummy  integer;
                begin

                dummy := pa_photo.new (
                                       name            => :image_name,
                                       parent_id       => :album_id,
                                       item_id         => :photo_id,
                                       revision_id     => :photo_rev_id,
                                       creation_date   => sysdate,
                                       creation_user   => :user_id,
                                       creation_ip     => :peeraddr,
                                       context_id      => :album_id,
                                       title           => :client_filename,
                                       description     => :description,
                                       is_live         => 't',
                                       caption         => :caption,
                                       story           => :story,
                                       user_filename   => :upload_name
                                       );
                end;
            }
            
            if {[array size exif] > 1} { 
                foreach {key value} [array get exif] { 
                    set tmp_exif_$key $value
                }

                # Check the datetime looks valid - clock scan works pretty well...
                if {[catch {clock scan $tmp_exif_DateTime}]} { 
                    set tmp_exif_DateTime {}
                }

                db_dml update_photo_data {}
            }

        if {$feedback_mode} {
        ns_write "
                          <ul>
                            <li>Loading image <b>$client_filename</b></li>
                             <ul>
                               <li>General information:</li>
                                 <ul>
                                   <li>Base Name: $base_image_name</li>
                                   <li>Photo_id: $photo_id</li>
                                   <li>Width: $base_width</li>
                                   <li>Height: $base_height</li>
                                   <li>Size: $base_bytes bytes</li>
                                   <li>Base image name: $base_image_name</li>
                                 </ul>
                               <li>Thumbnail information:</li>
                                 <ul>
                                   <li>Thumnail name: $th_image_name</li>
                                   <li>Width: $thumb_width</li>
                                   <li>Height: $thumb_height</li>
                                   <li>Size: $thumb_bytes bytes</li>
                                 </ul>
                               <li>Photo view information:</li>
                                 <ul>
                                   <li>View name: $vw_image_name</li>
                                   <li>Width: $viewer_width</li>
                                   <li>Height: $viewer_height</li>
                                   <li>Size: $viewer_bytes bytes</li>
                                 </ul>
                             </ul>
                          </ul>"
        }

            pa_insert_image $base_image_name $photo_id $base_item_id $base_rev_id $user_id $peeraddr $photo_id $base_image_name "original image" $base_mime "base" "t" $base_filename_relative $base_height $base_width $base_bytes 
            pa_insert_image $th_image_name $photo_id $thumb_item_id $thumb_rev_id $user_id $peeraddr $photo_id $th_image_name "thumbnail" $thumb_mime "thumb" "t" $thumb_filename_relative $thumb_height $thumb_width $thumb_bytes 
            pa_insert_image $vw_image_name $photo_id $viewer_item_id $viewer_rev_id $user_id $peeraddr $photo_id $vw_image_name "web image" $viewer_mime "viewer" "t" $viewer_filename_relative $viewer_height $viewer_width $viewer_bytes 
            
            pa_grant_privilege_to_creator $photo_id $user_id

            lappend new_ids $photo_id
        } 

    }

    return $new_ids
Generic XQL file:
<fullquery name="pa_load_images.update_photo_data">
    <querytext>

        UPDATE pa_photos 
        SET camera_model = :tmp_exif_Cameramodel,
            user_filename = :upload_name,
            date_taken = to_timestamp(:tmp_exif_DateTime,'YYYY-MM-DD HH24:MI:SS'),
            flash = :tmp_exif_Flashused,
            aperture = :tmp_exif_Aperture,
            metering = :tmp_exif_MeteringMode,
            focal_length = :tmp_exif_Focallength,
            exposure_time = :tmp_exif_Exposuretime,
            focus_distance = :tmp_exif_FocusDist,
            sha256 = :base_sha256
        WHERE pa_photo_id = :photo_rev_id

    </querytext>
</fullquery>
packages/photo-album/tcl/photo-album-procs.xql

PostgreSQL XQL file:
<fullquery name="pa_load_images.new_photo">
    <querytext>
        select pa_photo__new (
          :image_name, -- name
          :album_id, -- parent_id
          :photo_id, -- item_id
          :photo_rev_id, -- revision_id
          current_timestamp, -- creation_date
          :user_id, -- creation_user
          :peeraddr, -- creation_ip
          null, -- locale
          :album_id, -- context_id
          :client_filename, -- title
          :description, -- description
          't', -- is_live
          current_timestamp, -- publish_date
          null, -- nls_lang
          :caption, -- caption
          :story -- story
        )
    </querytext>
</fullquery>
packages/photo-album/tcl/photo-album-procs-postgresql.xql

Oracle XQL file:
<fullquery name="pa_load_images.new_photo">
    <querytext>
        declare 
            dummy  integer;
        begin

        dummy := pa_photo.new (
            name            => :image_name,
            parent_id       => :album_id,
            item_id         => :photo_id,
            revision_id     => :photo_rev_id,
            creation_user   => :user_id,
            creation_ip     => :peeraddr,
            context_id      => :album_id,
            title           => :client_filename,
            description     => :description,
            is_live         => 't',
            caption         => :caption,
            story           => :story
         );
         end;

    </querytext>
</fullquery>

<fullquery name="pa_load_images.update_photo_data">
    <querytext>

        UPDATE pa_photos 
        SET camera_model = :tmp_exif_Cameramodel,
            user_filename = :upload_name,
            date_taken = to_date(:tmp_exif_DateTime, 'YYYY-MM-DD HH24:MI:SS'),
            flash = :tmp_exif_Flashused,
            aperture = :tmp_exif_Aperture,
            metering = :tmp_exif_MeteringMode,
            focal_length = :tmp_exif_Focallength,
            exposure_time = :tmp_exif_Exposuretime,
            focus_distance = :tmp_exif_FocusDist,
            sha256 = :base_sha256
        WHERE pa_photo_id = :photo_rev_id

    </querytext>
</fullquery>
packages/photo-album/tcl/photo-album-procs-oracle.xql

[ hide source ] | [ make this the default ]
Show another procedure: