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,17 +257,37 @@ class NewnoticeAction extends Action
}
function storeFile($notice, $mimetype) {
$filename = basename($_FILES['attach']['name']);
$destination = "file/{$notice->id}-$filename";
if (move_uploaded_file($_FILES['attach']['tmp_name'], INSTALLDIR . "/$destination")) {
common_debug("NewnoticeAction::storeFile()");
$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->filename = $filename;
$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->mimetype = $mimetype;
if ($file_id = $file->insert()) {
$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->insert();

View File

@ -145,5 +145,47 @@ class File extends Memcached_DataObject
}
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;
}
}