sha1 hashes for files, to prevent overwriting srv with same name (e.g. 'screenshot.jpg')

This commit is contained in:
mappu 2015-04-05 16:49:17 +12:00
parent 17b0bea213
commit 671573bc2e
2 changed files with 25 additions and 6 deletions

View File

@ -3,8 +3,6 @@
- Merge "written in" and "tags" - Merge "written in" and "tags"
- Per-project directories, to prevent overwriting files (e.g. 'screenshot.jpg')
- RSS for recent changes - RSS for recent changes
- RSS for all projects - RSS for all projects

View File

@ -155,6 +155,7 @@ class CProject {
private $longdesc = ''; private $longdesc = '';
private $images = array(); private $images = array();
private $downloads = array(); private $downloads = array();
private $downloads_hashes = array();
public $tags = array(); public $tags = array();
public $homeimage = null; public $homeimage = null;
@ -203,6 +204,12 @@ class CProject {
natcasesort($this->downloads); natcasesort($this->downloads);
$this->downloads = array_reverse($this->downloads); $this->downloads = array_reverse($this->downloads);
for($i = 0, $e = count($this->downloads); $i !== $e; ++$i) {
$this->downloads_hashes[] = (
sha1_file($this->dir.$this->downloads[$i])
);
}
} }
public function genHomeImage() { public function genHomeImage() {
@ -231,7 +238,21 @@ class CProject {
// Copy downloads to wwwroot // Copy downloads to wwwroot
foreach($this->downloads as $idx => $filename) { foreach($this->downloads as $idx => $filename) {
copy($this->dir.$filename, BASEDIR.'wwwroot/srv/'.$filename); $cmkdir = @mkdir( BASEDIR.'wwwroot/srv/'.$this->downloads_hashes[$idx] );
if (! $cmkdir) {
fputs(
STDOUT,
"WARNING: Couldn't create directory ".$this->downloads_hashes[$idx].
" for file '${filename}'".
" in project '".$this->projname."'!\n"
);
}
copy(
$this->dir.$filename,
BASEDIR.'wwwroot/srv/'.$this->downloads_hashes[$idx].'/'.$filename
);
} }
// Generate index page // Generate index page
@ -269,11 +290,11 @@ class CProject {
<strong>DOWNLOAD</strong> <strong>DOWNLOAD</strong>
<ul> <ul>
<?php foreach($this->downloads as $filename) { ?> <?php foreach($this->downloads as $idx => $filename) { ?>
<li> <li>
<a href="srv/<?=hesc(rawurlencode($filename))?>"><?=hesc($filename)?></a> <a href="srv/<?=hesc($this->downloads_hashes[$idx])?>/<?=hesc(rawurlencode($filename))?>"><?=hesc($filename)?></a>
<small> <small>
<?=hesc(fbytes(filesize(BASEDIR.'wwwroot/srv/'.$filename)))?> <?=hesc(fbytes(filesize(BASEDIR.'wwwroot/srv/'.$this->downloads_hashes[$idx].'/'.$filename)))?>
</small> </small>
</li> </li>
<?php } ?> <?php } ?>