Index: openacs-4/packages/file-storage/www/file-add-2.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/file-storage/www/Attic/file-add-2.tcl,v diff -u -r1.2 -r1.3 --- openacs-4/packages/file-storage/www/file-add-2.tcl 4 Sep 2001 01:29:38 -0000 1.2 +++ openacs-4/packages/file-storage/www/file-add-2.tcl 22 Sep 2001 05:42:16 -0000 1.3 @@ -26,77 +26,121 @@ } } -set user_id [ad_conn user_id] - -# check for write permission on this folder - +# Check for write permission on this folder ad_require_permission $folder_id write -# get the filename part of the upload file +# Get the filename part of the upload file if ![regexp {[^//\\]+$} $upload_file filename] { # no match set filename $upload_file } -# get the ip +# Get the user +set user_id [ad_conn user_id] + +# Get the ip set creation_ip [ad_conn peeraddr] +# The content repository is kinda stupid about mime types, +# so we have to check if we know about this one and possibly +# add it. +set mime_type [fs_maybe_create_new_mime_type $upload_file] + +# Get the storage type +set indb_p [ad_parameter "StoreFilesInDatabaseP" -package_id [ad_conn package_id]] + db_transaction { -# create the new item + # create the new item + if {$indb_p} { -set item_id [db_exec_plsql item_add " -begin - :1 := content_item.new ( - name => :filename, - parent_id => :folder_id, - context_id => :folder_id, - creation_user => :user_id, - creation_ip => :creation_ip, - item_subtupe => 'file_storage_item' -- needed by site-wide search - ); -end;"] + set file_id [db_exec_plsql new_lob_file " + begin + :1 := file_storage__create_file ( + name => :filename, + parent_id => :folder_id, + context_id => :folder_id, + creation_user => :user_id, + creation_ip => :creation_ip, + item_subtype => 'file_storage_item' -- needed by site-wide search + ); + end;"] -# create a revision + set version_id [db_exec_plsql new_version " + begin + :1 := file_storage__create_file ( + name => :filename, + parent_id => :folder_id, + context_id => :folder_id, + creation_user => :user_id, + creation_ip => :creation_ip, + item_subtype => 'file_storage_item' -- needed by site-wide search + ); + end;"] -# The content repository is kinda stupid about mime types, -# so we have to check if we know about this one and possibly -# add it. + db_dml lob_content " + update cr_revisions + set content = empty_lob() + where revision_id = :version_id + returning content into :1" -blob_files [list ${upload_file.tmpfile}] -set mime_type [fs_maybe_create_new_mime_type $upload_file] -set revision_id [db_exec_plsql revision_add " -begin - :1 := content_revision.new ( - title => :title, - description => :description, - mime_type => :mime_type, - item_id => :item_id, - creation_user => :user_id, - creation_ip => :creation_ip - ); -end;"] + # Unfortunately, we can only calculate the file size after the lob is uploaded + db_dml lob_size " + update cr_revisions + set content_length = lob_length(lob) + where revision_id = :version_id" -db_dml content_add " -update cr_revisions -set content = empty_blob() -where revision_id = :revision_id -returning content into :1" -blob_files [list ${upload_file.tmpfile}] + } else { -db_exec_plsql make_live " -begin - content_item.set_live_revision(:revision_id); -end;" + set file_id [db_exec_plsql new_fs_file " + begin + :1 := content_item.new ( + name => :filename, + parent_id => :folder_id, + context_id => :folder_id, + creation_user => :user_id, + creation_ip => :creation_ip, + item_subtype => 'file_storage_item', + storage_type => 'file' + ); + end;"] + + set version_id [db_exec_plsql new_version " + begin + :1 := file_storage__create_file ( + name => :filename, + parent_id => :folder_id, + context_id => :folder_id, + creation_user => :user_id, + creation_ip => :creation_ip, + item_subtype => 'file_storage_item' -- needed by site-wide search + ); + end;"] + + set tmp_filename [cr_create_content_file $file_id $version_id ${upload_file.tmpfile}] + set tmp_size [cr_file_size $tmp_filename] + + db_dml fs_content_size " + update cr_revisions + set content = '$tmp_filename', + content_length = $tmp_size + where revision_id = :version_id" + + } + + } on_error { + # most likely a duplicate name or a double click if [db_string duplicate_check " select count(*) from cr_items where name = :filename and parent_id = :folder_id"] { - ad_return_complaint 1 "Either there is already a file with the name \"$filename\" or you clicked on the button more than once. You can use the Back button to return and choose a new name, or return to the directory listing to see if your file is there." + ad_return_complaint 1 "Either there is already a file with the name \"$tmp_filename\" or you clicked on the button more than once. You can use the Back button to return and choose a new name, or return to the directory listing to see if your file is there." } else { ad_return_complaint 1 "We got an error that we couldn't readily identify. Please let the system owner know about this. @@ -107,4 +151,4 @@ } -ad_returnredirect "?folder_id=$folder_id" \ No newline at end of file +ad_returnredirect "?folder_id=$folder_id"