Index: openacs-4/packages/acs-content-repository/tcl/revision-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/acs-content-repository/tcl/revision-procs.tcl,v diff -u -r1.5 -r1.6 --- openacs-4/packages/acs-content-repository/tcl/revision-procs.tcl 31 Oct 2001 20:42:07 -0000 1.5 +++ openacs-4/packages/acs-content-repository/tcl/revision-procs.tcl 15 Nov 2001 01:47:13 -0000 1.6 @@ -46,32 +46,32 @@ } { if { [info exists revision_id] && [info exists item_id] } { - return -code error "Both revision_id and item_id were specfied" + ad_return -code error "Both revision_id and item_id were specfied" } if { [info exists item_id] } { if { ![db_0or1row get_item_info ""] } { - return -code error "There is no content that matches item_id '$item_id'" + ad_return -code error "There is no content that matches item_id '$item_id'" } } elseif { [info exists revision_id] } { if { ![db_0or1row get_revision_info ""] } { - return -code error "There is no content that matches revision_id '$revision_id'" + ad_return -code error "There is no content that matches revision_id '$revision_id'" } } else { - return -code error "Either revision_id or item_id must be specified" + ad_return -code error "Either revision_id or item_id must be specified" } if { ![string equal $storage_type "file"] && \ ![string equal $storage_type "text"] && \ ![string equal $storage_type "lob"] } { - return -code error "Storage type '$storage_type' is invalid." + ad_return -code error "Storage type '$storage_type' is invalid." } ReturnHeaders $mime_type switch $storage_type { text { - ns_write [db_string write_text_content] + ns_write [db_string write_text_content ""] } file { set path [cr_fs_path $storage_area_key] @@ -84,3 +84,197 @@ return } + +ad_proc -public cr_import_content { + {-storage_type "file"} + -creation_user + -creation_ip + -image_only:boolean + {-image_type "image"} + {-other_type "content_revision"} + {-title ""} + {-description ""} + -item_id + parent_id + tmp_filename + tmp_size + mime_type + object_name +} { + + Import an uploaded file into the content repository. + + @param storage_type Where to store the content (lob or file), defaults to "file" (later + a system-wide parameter) + @param creation_user The creating user (defaults to current user) + @param creation_ip The creating ip (defaults to peeraddr) + @param image_only Only allow images + @param image_type The type of content item to create if the file contains an image + @param other_type The type of content item to create for a non-image file + @param title The title given the new revision + @param description The description of the new revision + @param item_id If present, make a new revision of this item, otherwise, make a new + item + @param parent_id The parent of the content item we create + @param tmp_filename The name of the temporary file holding the uploaded content + @param tmp_size The size of tmp_file + @param mime_type The uploaded file's mime type + @param object_name The name to give the result content item and revision + + This procedure handles all mime_type details, creating a new item of the appropriate + type and stuffing the content into either the file system or the database depending + on "storage_type". The new revision is set live, and its item_id is returned to the + caller. + + image_type and other_type should be supplied when the client package + has extended the image and content_revision types to hold package-specific + information. Checking is done to ensure that image_type has been inherited from + image, and that other_type has been inherited from content_revision. + + It up to the caller to do any checking on size limitations, etc. + +} { + + if { ![info exists creation_user] } { + set creation_user [ad_verify_and_get_user_id] + } + + if { ![info exists creation_ip] } { + set creation_ip [ad_conn peeraddr] + } + + # DRB: Eventually we should allow for text storage ... (CLOB for Oracle) + + if { ![string equal $storage_type "file"] && ![string equal $storage_type "lob"] } { + return -code error "Imported content must be stored in the file system or as a large object" + } + + if {[string equal $mime_type "*/*"]} { + set mime_type "application/octet-stream" + } + + set old_item_p [info exists item_id] + if { !$old_item_p } { + set item_id [db_nextval acs_object_id_seq] + } + + set revision_id [db_nextval acs_object_id_seq] + + db_transaction { + + if { [empty_string_p [cr_registered_type_for_mime_type $mime_type]] } { + db_dml mime_type_insert "" + db_exec_plsql mime_type_register "" + } + + switch [cr_registered_type_for_mime_type $mime_type] { + image { + + if { [db_string image_subclass ""] == "f" } { + ad_return -code error "Image file must be stored in an image object" + } + + set what_aolserver_told_us "" + if { [string equal $mime_type "image/jpeg"] } { + catch { set what_aolserver_told_us [ns_jpegsize $tmp_filename] } + } elseif { [string equal $mime_type "gif"] } { + catch { set what_aolserver_told_us [ns_gifsize $tmp_filename] } + } else { + ad_return -code error "Unknown image type" + } + + # the AOLserver jpegsize command has some bugs where the height comes + # through as 1 or 2 + if { ![empty_string_p $what_aolserver_told_us] && \ + [lindex $what_aolserver_told_us 0] > 10 && \ + [lindex $what_aolserver_told_us 1] > 10 } { + set original_width [lindex $what_aolserver_told_us 0] + set original_height [lindex $what_aolserver_told_us 1] + } else { + set original_width "" + set original_height "" + } + + if { !$old_item_p } { + db_exec_plsql image_new "" + } else { + db_exec_plsql image_revision_new "" + } + + } + + default { + + if { $image_only_p } { + ad_return -code error "The file you uploaded was not an image (.gif, .jpg or .jpeg) file" + } + + if { [db_string content_revision_subclass ""] == "f" } { + ad_return -code error "Content must be stored in a content revision object" + } + + if { !$old_item_p } { + db_exec_plsql content_item_new "" + } + db_exec_plsql content_revision_new "" + + } + } + + # insert the attatchment into the database + + switch $storage_type { + file { + set filename [cr_create_content_file $item_id $revision_id $tmp_filename] + db_dml set_file_content "" + } + lob { + db_dml set_lob_content "" -blob_files [list $tmp_filename] + db_dml set_lob_size "" + } + } + + } + + return $revision_id + +} + +ad_proc cr_set_imported_content_live { + -image_sql + -other_sql + mime_type + revision_id +} { + @param image_sql Optional SQL to extend the base image type + @param other_sql Optional SQL to extend the base content revision type + @mime_type Mime type of the new revision + @revision_id The revision we're setting live + + If provided execute the appropriate SQL in the caller's context, then + set the given revision live. + + The idea is to give the caller a clean way of setting the additional information + needed for its private type. This is a hack. Executing this SQL can't be done + within cr_import_content because the caller can't see the new revision's key... +} { + if { [cr_registered_type_for_mime_type $mime_type] == "image" } { + if { [info exists image_sql] } { + uplevel 1 [list db_dml dynamic_query $image_sql] + } + } elseif { [info exists other_sql] } { + uplevel 1 [list db_dml dynamic_query $other_sql] + } + db_exec_plsql set_live "" +} + + +ad_proc cr_registered_type_for_mime_type { + mime_type +} { + Return the type registered for this mime type. + + @mime_type param The mime type +} { + return [db_string registered_type_for_mime_type "" -default ""] +}