? ftp-upload.patch
Index: attachment.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/attachment/attachment.module,v
retrieving revision 1.20
diff -u -i -r1.20 attachment.module
--- attachment.module 2 Aug 2007 17:38:57 -0000 1.20
+++ attachment.module 27 Aug 2007 15:33:14 -0000
@@ -60,9 +60,19 @@
$form['attachments'][$key]['description'] = array('#type'=>'textfield', '#size'=>100, '#maxlength'=>255, '#default_value'=>$attachment['description']);
$form['attachments'][$key]['display'] = array('#type'=>'markup', '#value'=> 'Filename:'. $attachment['filename'] .'
URL:'. module_invoke('filemanager', 'url', $attachment['fid'], FALSE, TRUE) .'
Size:' . format_size($attachment['size']) . '');
}
-
- $form['attachments']['commands']['attachment_file'] = array('#type'=>'file', '#title'=>t('File'), '#size'=>40, '#tree'=>false);
- $form['attachments']['commands']['fileop_add'] = array('#type'=>'button', '#name'=>'fileop', '#value'=>t('Add'), '#tree'=>false);
+
+ if(variable_get('attachment_ftp_allow', 0)) {
+ $form['attachments']['commands']['upload'] = array('#type'=>'fieldset', '#title'=>'Upload a File');
+ $form['attachments']['commands']['upload']['attachment_file'] = array('#type'=>'file', '#title'=>t('File'), '#size'=>40, '#tree'=>false);
+ $form['attachments']['commands']['upload']['fileop_add'] = array('#type'=>'button', '#name'=>'fileop', '#value'=>t('Add'), '#tree'=>false);
+ $form['attachments']['commands']['ftp'] = array('#type'=>'fieldset', '#title'=>t('Choose FTP Uploaded File'));
+ $form['attachments']['commands']['ftp']['ftp_file'] = array('#type'=>'textfield', '#title'=>t('FTP Uploaded File Path'), '#size'=>40, '#tree'=>false);
+ $form['attachments']['commands']['ftp']['fileop_ftp'] = array('#type'=>'button', '#name'=>'fileop_ftp', '#value'=>t('Add from FTP drop box'), '#tree'=>false);
+ }
+ else {
+ $form['attachments']['commands']['attachment_file'] = array('#type'=>'file', '#title'=>t('File'), '#size'=>40, '#tree'=>false);
+ $form['attachments']['commands']['fileop_add'] = array('#type'=>'button', '#name'=>'fileop', '#value'=>t('Add'), '#tree'=>false);
+ }
}
break;
@@ -152,7 +162,7 @@
return;
case 'prepare':
- if (($_POST['fileop'] == t('Add')) || ($_POST['op'] == t('Preview')) || ($_POST['op'] == t('Submit'))) {
+ if (($_POST['fileop_ftp'] == t('Add from FTP drop box')) || ($_POST['fileop'] == t('Add')) || ($_POST['op'] == t('Preview')) || ($_POST['op'] == t('Submit'))) {
$node->attachments = $_POST['attachments'];
}
@@ -160,6 +170,10 @@
attachment_add($node);
}
+ if ($_POST['fileop_ftp'] == t('Add from FTP drop box')) {
+ attachment_add_ftp($node, $_POST['ftp_file']);
+ }
+
return;
}
@@ -186,6 +200,25 @@
'#return_value' => 1,
'#description' => t('If checked attachment file lists will be added to the bottom of teasers.')
);
+
+ $form['ftp'] = array(
+ '#type' => 'fieldset',
+ '#title' => t('FTP Uploads'),
+ /*'#description' => t('Local files are files which have already been uploaded to the webserver, using FTP for example. This may be useful to work around maximum uploaded file constraints on shared hosting platforms.')*/
+ );
+ $form['ftp']['attachment_ftp_allow'] = array(
+ '#type' => 'checkbox',
+ '#title' => t('Allow FTP uploads'),
+ '#default_value' => variable_get('attachment_ftp_allow', 0),
+ '#return_value' => 1,
+ '#description' => t('If checked users will be able to add files from the FTP drop box.')
+ );
+ $form['ftp']['attachment_ftp_dropbox'] = array(
+ '#type' => 'textfield',
+ '#title' => t('FTP drop box path'),
+ '#default_value' => variable_get('attachment_ftp_dropbox', ''),
+ '#description' => t('A file system path where FTP uploaded files will be dropped. This directory has to exist and be writable by Drupal.')
+ );
$form['security'] = array(
'#type' => 'fieldset',
@@ -377,6 +410,55 @@
$node->attachments[] = $new_attachment;
}
+/*
+ * Callback function to add an attachment from the local filesystem to the node being edited
+ */
+function attachment_add_ftp(&$node, $local_path) {
+
+ if (!file_check_location($local_path, variable_get('attachment_ftp_dropbox', null))) {
+ form_set_error('local_file', t('File was not in the FTP drop box.'));
+ return $node;
+ }
+ elseif (!file_exists($local_path)) {
+ form_set_error('local_file', t('File not found.'));
+ return $node;
+ }
+
+ $new_attachment['fid'] = FALSE;
+ $new_attachment['filename'] = basename($local_path);
+ $new_attachment['deleted'] = FALSE;
+ $new_attachment['working'] = TRUE;
+ $new_attachment['size'] = filesize($local_path);
+
+ // If we are uploading a file with the same name as an existing
+ // attachment then we should use the same fid and remove the
+ // existing attachment from the array.
+ foreach ((array)$node->attachments as $key => $attachment) {
+ if ($attachment['filename'] == $new_attachment['filename']) {
+ $new_attachment['fid'] = $attachment['fid'];
+ $remove_key = $key;
+ }
+ }
+ if (isset($remove_key)) {
+ array_splice($node->attachments, $key, 1);
+ }
+ else {
+ $new_attachment['title'] = basename($local_path);
+ $new_attachment['description'] = '';
+ $node->attachmentcount++;
+ }
+
+ // Move the upload file into working filestore
+ $file = module_invoke('filemanager', 'add_file', 'attachments', $local_path, basename($local_path), 'application/unknown', true, variable_get('attachment_private_files', 0), $new_attachment["fid"]);
+ if (!$file) {
+ form_set_error('local_file', t('Error saving upload to filestore.'));
+ return $node;
+ }
+ $new_attachment['fid'] = $file->fid;
+
+ $node->attachments[] = $new_attachment;
+}
+
/**
* Load the attachments for the given node
*/