Address files by name rather than ID in the downloads section (issue 686)

This commit is contained in:
Thomas Keller 2011-05-28 00:48:24 +02:00
parent 82bb18fe10
commit ee33cc1832
5 changed files with 41 additions and 19 deletions

View File

@ -3,6 +3,7 @@
## New Features ## New Features
- Mercurial source views now show parent revisions (if any) and detailed change information - Mercurial source views now show parent revisions (if any) and detailed change information
- File download URLs now contain the file name rather than the upload id; old links still work though (issue 686)
## Bugfixes ## Bugfixes

View File

@ -59,7 +59,7 @@ class IDF_Views_Download
$sql .= ' AND id NOT IN ('.implode(',', $ids).')'; $sql .= ' AND id NOT IN ('.implode(',', $ids).')';
} }
$pag->forced_where = new Pluf_SQL($sql, array($prj->id)); $pag->forced_where = new Pluf_SQL($sql, array($prj->id));
$list_display = array( $list_display = array(
'file' => __('File'), 'file' => __('File'),
array('summary', 'IDF_Views_Download_SummaryAndLabels', __('Summary')), array('summary', 'IDF_Views_Download_SummaryAndLabels', __('Summary')),
@ -79,7 +79,7 @@ class IDF_Views_Download
'dlabel' => $dtag, 'dlabel' => $dtag,
), ),
$request); $request);
} }
/** /**
@ -99,17 +99,17 @@ class IDF_Views_Download
$deprecated = Pluf_Model_InArray($dtag, $tags); $deprecated = Pluf_Model_InArray($dtag, $tags);
if ($request->method == 'POST' and if ($request->method == 'POST' and
true === IDF_Precondition::projectMemberOrOwner($request)) { true === IDF_Precondition::projectMemberOrOwner($request)) {
$form = new IDF_Form_UpdateUpload($request->POST, $form = new IDF_Form_UpdateUpload($request->POST,
array('project' => $prj, array('project' => $prj,
'upload' => $upload, 'upload' => $upload,
'user' => $request->user)); 'user' => $request->user));
if ($form->isValid()) { if ($form->isValid()) {
$upload = $form->save(); $upload = $form->save();
$urlfile = Pluf_HTTP_URL_urlForView('IDF_Views_Download::view', $urlfile = Pluf_HTTP_URL_urlForView('IDF_Views_Download::view',
array($prj->shortname, $upload->id)); array($prj->shortname, $upload->id));
$request->user->setMessage(sprintf(__('The file <a href="%1$s">%2$s</a> has been updated.'), $urlfile, Pluf_esc($upload->file))); $request->user->setMessage(sprintf(__('The file <a href="%1$s">%2$s</a> has been updated.'), $urlfile, Pluf_esc($upload->file)));
$url = Pluf_HTTP_URL_urlForView('IDF_Views_Download::index', $url = Pluf_HTTP_URL_urlForView('IDF_Views_Download::index',
array($prj->shortname)); array($prj->shortname));
return new Pluf_HTTP_Response_Redirect($url); return new Pluf_HTTP_Response_Redirect($url);
} }
@ -162,7 +162,7 @@ class IDF_Views_Download
* [description] * [description]
* *
* This signal allows an application to perform a set of tasks * This signal allows an application to perform a set of tasks
* just before the deletion of the corresponding object in the * just before the deletion of the corresponding object in the
* database but just after the deletion from the storage. * database but just after the deletion from the storage.
* *
* [parameters] * [parameters]
@ -171,11 +171,11 @@ class IDF_Views_Download
* *
*/ */
$params = array('upload' => $upload); $params = array('upload' => $upload);
Pluf_Signal::send('IDF_Upload::delete', Pluf_Signal::send('IDF_Upload::delete',
'IDF_Views_Download', $params); 'IDF_Views_Download', $params);
$upload->delete(); $upload->delete();
$request->user->setMessage(__('The file has been deleted.')); $request->user->setMessage(__('The file has been deleted.'));
$url = Pluf_HTTP_URL_urlForView('IDF_Views_Download::index', $url = Pluf_HTTP_URL_urlForView('IDF_Views_Download::index',
array($prj->shortname)); array($prj->shortname));
return new Pluf_HTTP_Response_Redirect($url); return new Pluf_HTTP_Response_Redirect($url);
} }
@ -190,19 +190,35 @@ class IDF_Views_Download
} }
/** /**
* Download a file. * Download the file with the given name.
*/ */
public $download_precond = array('IDF_Precondition::accessDownloads'); public $download_precond = array('IDF_Precondition::accessDownloads');
public function download($request, $match) public function download($request, $match)
{ {
$prj = $request->project; $prj = $request->project;
$upload = Pluf_Shortcuts_GetObjectOr404('IDF_Upload', $match[2]); $sql = new Pluf_SQL('file=%s', array($match[2]));
$upload = Pluf::factory('IDF_Upload')->getOne(array('filter' => $sql->gen()));
if (!$upload) throw new Pluf_HTTP_Error404();
$prj->inOr404($upload); $prj->inOr404($upload);
$upload->downloads += 1; $upload->downloads += 1;
$upload->update(); $upload->update();
return new Pluf_HTTP_Response_Redirect($upload->getAbsoluteUrl($prj)); return new Pluf_HTTP_Response_Redirect($upload->getAbsoluteUrl($prj));
} }
/**
* Download the file with the given ID (for legacy links).
*/
public $downloadById_precond = array('IDF_Precondition::accessDownloads');
public function downloadById($request, $match)
{
$upload = Pluf_Shortcuts_GetObjectOr404('IDF_Upload', $match[2]);
return new Pluf_HTTP_Response_Redirect(
Pluf_HTTP_URL_urlForView('IDF_Views_Download::download', array(
$match[1], $upload->file
)), 301
);
}
/** /**
* Submit a new file for download. * Submit a new file for download.
*/ */
@ -218,10 +234,10 @@ class IDF_Views_Download
'user' => $request->user)); 'user' => $request->user));
if ($form->isValid()) { if ($form->isValid()) {
$upload = $form->save(); $upload = $form->save();
$urlfile = Pluf_HTTP_URL_urlForView('IDF_Views_Download::view', $urlfile = Pluf_HTTP_URL_urlForView('IDF_Views_Download::view',
array($prj->shortname, $upload->id)); array($prj->shortname, $upload->id));
$request->user->setMessage(sprintf(__('The <a href="%s">file</a> has been uploaded.'), $urlfile)); $request->user->setMessage(sprintf(__('The <a href="%s">file</a> has been uploaded.'), $urlfile));
$url = Pluf_HTTP_URL_urlForView('IDF_Views_Download::index', $url = Pluf_HTTP_URL_urlForView('IDF_Views_Download::index',
array($prj->shortname)); array($prj->shortname));
return new Pluf_HTTP_Response_Redirect($url); return new Pluf_HTTP_Response_Redirect($url);
} }
@ -246,7 +262,7 @@ class IDF_Views_Download
{ {
$conf = new IDF_Conf(); $conf = new IDF_Conf();
$conf->setProject($project); $conf->setProject($project);
$st = preg_split("/\015\012|\015|\012/", $st = preg_split("/\015\012|\015|\012/",
$conf->getVal('labels_download_predefined', IDF_Form_UploadConf::init_predefined), -1, PREG_SPLIT_NO_EMPTY); $conf->getVal('labels_download_predefined', IDF_Form_UploadConf::init_predefined), -1, PREG_SPLIT_NO_EMPTY);
$auto = ''; $auto = '';
foreach ($st as $s) { foreach ($st as $s) {
@ -354,7 +370,7 @@ function IDF_Views_Download_SummaryAndLabels($field, $down, $extra='')
{ {
$tags = array(); $tags = array();
foreach ($down->get_tags_list() as $tag) { foreach ($down->get_tags_list() as $tag) {
$url = Pluf_HTTP_URL_urlForView('IDF_Views_Download::listLabel', $url = Pluf_HTTP_URL_urlForView('IDF_Views_Download::listLabel',
array($down->shortname, $tag->id)); array($down->shortname, $tag->id));
$tags[] = sprintf('<a href="%s" class="label">%s</a>', $url, Pluf_esc((string) $tag)); $tags[] = sprintf('<a href="%s" class="label">%s</a>', $url, Pluf_esc((string) $tag));
} }

View File

@ -73,7 +73,7 @@ $ctl[] = array('regex' => '#^/p/([\-\w]+)/$#',
'base' => $base, 'base' => $base,
'model' => 'IDF_Views_Project', 'model' => 'IDF_Views_Project',
'method' => 'home'); 'method' => 'home');
$ctl[] = array('regex' => '#^/p/([\-\w]+)/logo/$#', $ctl[] = array('regex' => '#^/p/([\-\w]+)/logo/$#',
'base' => $base, 'base' => $base,
'model' => 'IDF_Views_Project', 'model' => 'IDF_Views_Project',
@ -294,11 +294,16 @@ $ctl[] = array('regex' => '#^/p/([\-\w]+)/downloads/(\d+)/$#',
'model' => 'IDF_Views_Download', 'model' => 'IDF_Views_Download',
'method' => 'view'); 'method' => 'view');
$ctl[] = array('regex' => '#^/p/([\-\w]+)/downloads/(\d+)/get/$#', $ctl[] = array('regex' => '#^/p/([\-\w]+)/downloads/get/(.+)$#',
'base' => $base, 'base' => $base,
'model' => 'IDF_Views_Download', 'model' => 'IDF_Views_Download',
'method' => 'download'); 'method' => 'download');
$ctl[] = array('regex' => '#^/p/([\-\w]+)/downloads/(\d+)/get/$#',
'base' => $base,
'model' => 'IDF_Views_Download',
'method' => 'downloadById');
$ctl[] = array('regex' => '#^/p/([\-\w]+)/downloads/create/$#', $ctl[] = array('regex' => '#^/p/([\-\w]+)/downloads/create/$#',
'base' => $base, 'base' => $base,
'model' => 'IDF_Views_Download', 'model' => 'IDF_Views_Download',

View File

@ -3,7 +3,7 @@
{block body} {block body}
<div class="download-file"> <div class="download-file">
<a href="{url 'IDF_Views_Download::download', array($project.shortname, $file.id)}">{$file}</a> - {$file.filesize|size} <a href="{url 'IDF_Views_Download::download', array($project.shortname, $file.file)}">{$file}</a> - {$file.filesize|size}
</div> </div>
<p>{blocktrans}<strong>Attention!</strong> If you want to delete a specific version of your software, maybe, someone is depending on this specific version to run his systems. Are you sure, you will not affect anybody when removing this file?{/blocktrans}</p> <p>{blocktrans}<strong>Attention!</strong> If you want to delete a specific version of your software, maybe, someone is depending on this specific version to run his systems. Are you sure, you will not affect anybody when removing this file?{/blocktrans}</p>
@ -15,7 +15,7 @@
<table class="form" summary=""> <table class="form" summary="">
<tr> <tr>
<td>&nbsp;</td> <td>&nbsp;</td>
<td><input type="submit" value="{trans 'Delete File'}" name="submit" /> | <a href="{url 'IDF_Views_Download::index', array($project.shortname)}">{trans 'Cancel'}</a> <td><input type="submit" value="{trans 'Delete File'}" name="submit" /> | <a href="{url 'IDF_Views_Download::index', array($project.shortname)}">{trans 'Cancel'}</a>
</td> </td>
</tr> </tr>
</table> </table>

View File

@ -4,7 +4,7 @@
<div class="download-file"> <div class="download-file">
{if $deprecated}<p class="smaller">{blocktrans}<strong>Attention!</strong> This file is marked as deprecated, download it only if you are sure you need this specific version.{/blocktrans}</p>{/if} {if $deprecated}<p class="smaller">{blocktrans}<strong>Attention!</strong> This file is marked as deprecated, download it only if you are sure you need this specific version.{/blocktrans}</p>{/if}
<a href="{url 'IDF_Views_Download::download', array($project.shortname, $file.id)}">{$file}</a> - {$file.filesize|size} <a href="{url 'IDF_Views_Download::download', array($project.shortname, $file.file)}">{$file}</a> - {$file.filesize|size}
</div> </div>
{if $file.changelog} {if $file.changelog}
<h2 class="changes">{trans 'Changes'}</h2> <h2 class="changes">{trans 'Changes'}</h2>