Issue 81: Allow users to create downloads that point offsite

This commit is contained in:
Nathan Adams
2015-10-16 22:09:57 -05:00
parent 4421b6d4a1
commit 75986bb272
6 changed files with 134 additions and 9 deletions

View File

@@ -56,6 +56,13 @@ class IDF_Upload extends Pluf_Model
'size' => 250,
'verbose' => __('summary'),
),
'ext_file' =>
array(
'type' => 'Pluf_DB_Field_Varchar',
'blank' => false,
'size' => 250,
'verbose' => __('External File URL'),
),
'changelog' =>
array(
'type' => 'Pluf_DB_Field_Text',
@@ -140,6 +147,54 @@ class IDF_Upload extends Pluf_Model
return $this->file;
}
/**
* Get the file size of any remote resource (using get_headers()),
* either in bytes or - default - as human-readable formatted string.
*
* @author Stephan Schmitz <eyecatchup@gmail.com>
* @license MIT <http://eyecatchup.mit-license.org/>
* @url <https://gist.github.com/eyecatchup/f26300ffd7e50a92bc4d>
*
* @param string $url Takes the remote object's URL.
* @param boolean $formatSize Whether to return size in bytes or formatted.
* @param boolean $useHead Whether to use HEAD requests. If false, uses GET.
* @return string Returns human-readable formatted size
* or size in bytes (default: formatted).
*
* <code>
* //example
* echo getRemoteFilesize('https://github.com/eyecatchup/SEOstats/archive/master.zip');
* </code>
*/
private function getRemoteFilesize($url, $formatSize = true, $useHead = true)
{
if (false !== $useHead) {
stream_context_set_default(array('http' => array('method' => 'HEAD')));
}
$head = array_change_key_case(get_headers($url, 1));
// content-length of download (in bytes), read from Content-Length: field
$clen = isset($head['content-length']) ? $head['content-length'] : 0;
// cannot retrieve file size, return "-1"
if (!$clen) {
return -1;
}
if (!$formatSize) {
return $clen; // return size in bytes
}
$size = $clen;
switch ($clen) {
case $clen < 1024:
$size = $clen .' B'; break;
case $clen < 1048576:
$size = round($clen / 1024, 2) .' KiB'; break;
case $clen < 1073741824:
$size = round($clen / 1048576, 2) . ' MiB'; break;
case $clen < 1099511627776:
$size = round($clen / 1073741824, 2) . ' GiB'; break;
}
return $size; // return formatted size
}
function _toIndex()
{
return '';
@@ -150,7 +205,11 @@ class IDF_Upload extends Pluf_Model
if ($this->id == '') {
$this->creation_dtime = gmdate('Y-m-d H:i:s');
$this->modif_dtime = gmdate('Y-m-d H:i:s');
$this->md5 = md5_file ($this->getFullPath());
if ($this->ext_file == "") {
$this->md5 = md5_file ($this->getFullPath());
} else {
$this->filesize = $this->getRemoteFilesize($this->ext_file, false);
}
}
}