Attachment upload server and path now configurable

This commit is contained in:
Zach Copley 2009-06-22 22:48:31 +00:00
parent f95dbee7d6
commit d9bebfd651
2 changed files with 77 additions and 15 deletions

View File

@ -257,23 +257,43 @@ class NewnoticeAction extends Action
} }
function storeFile($notice, $mimetype) { function storeFile($notice, $mimetype) {
$filename = basename($_FILES['attach']['name']);
$destination = "file/{$notice->id}-$filename"; common_debug("NewnoticeAction::storeFile()");
if (move_uploaded_file($_FILES['attach']['tmp_name'], INSTALLDIR . "/$destination")) {
$basename = basename($_FILES['attach']['name']);
common_debug("Basename: $basename");
$filename = File::filename($notice->id, $basename);
common_debug("filename: $filename");
$filepath = File::path($filename);
common_debug("filepath: $filepath");
if (move_uploaded_file($_FILES['attach']['tmp_name'], $filepath)) {
$file = new File; $file = new File;
$file->filename = $filename;
$file->url = common_local_url('file', array('notice' => $notice->id)); $file->url = common_local_url('file', array('notice' => $notice->id));
$file->size = filesize(INSTALLDIR . "/$destination");
common_debug("file->url =". $file->url);
$file->size = filesize($filepath);
$file->date = time(); $file->date = time();
$file->mimetype = $mimetype; $file->mimetype = $mimetype;
if ($file_id = $file->insert()) { if ($file_id = $file->insert()) {
$file_redir = new File_redirection; $file_redir = new File_redirection;
$file_redir->url = common_path($destination); $file_redir->url = File::url($filename);
$file_redir->file_id = $file_id; $file_redir->file_id = $file_id;
$file_redir->insert(); $file_redir->insert();
$f2p = new File_to_post; $f2p = new File_to_post;
$f2p->file_id = $file_id; $f2p->file_id = $file_id;
$f2p->post_id = $notice->id; $f2p->post_id = $notice->id;
$f2p->insert(); $f2p->insert();
} else { } else {
$this->clientError(_('There was a database error while saving your file. Please try again.')); $this->clientError(_('There was a database error while saving your file. Please try again.'));

View File

@ -30,7 +30,7 @@ require_once INSTALLDIR.'/classes/File_to_post.php';
* Table Definition for file * Table Definition for file
*/ */
class File extends Memcached_DataObject class File extends Memcached_DataObject
{ {
###START_AUTOCODE ###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */ /* the code below is auto generated do not remove the above tag */
@ -38,12 +38,12 @@ class File extends Memcached_DataObject
public $__table = 'file'; // table name public $__table = 'file'; // table name
public $id; // int(4) primary_key not_null public $id; // int(4) primary_key not_null
public $url; // varchar(255) unique_key public $url; // varchar(255) unique_key
public $mimetype; // varchar(50) public $mimetype; // varchar(50)
public $size; // int(4) public $size; // int(4)
public $title; // varchar(255) public $title; // varchar(255)
public $date; // int(4) public $date; // int(4)
public $protected; // int(4) public $protected; // int(4)
public $filename; // varchar(255) public $filename; // varchar(255)
public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
/* Static get */ /* Static get */
@ -116,7 +116,7 @@ class File extends Memcached_DataObject
$x = File::staticGet($file_id); $x = File::staticGet($file_id);
if (empty($x)) die('Impossible!'); if (empty($x)) die('Impossible!');
} }
File_to_post::processNew($file_id, $notice_id); File_to_post::processNew($file_id, $notice_id);
return $x; return $x;
} }
@ -145,5 +145,47 @@ class File extends Memcached_DataObject
} }
return true; return true;
} }
// where should the file go?
static function filename($notice_id, $basename)
{
return $notice_id . '-' . $basename;
}
static function path($filename)
{
$dir = common_config('attachments', 'dir');
if ($dir[strlen($dir)-1] != '/') {
$dir .= '/';
}
return $dir . $filename;
}
static function url($filename)
{
$path = common_config('attachments', 'path');
if ($path[strlen($path)-1] != '/') {
$path .= '/';
}
if ($path[0] != '/') {
$path = '/'.$path;
}
$server = common_config('attachments', 'server');
if (empty($server)) {
$server = common_config('site', 'server');
}
// XXX: protocol
return 'http://'.$server.$path.$filename;
}
} }