208
public static function get_mimetypes_from_description($description=null) {
209
if (is_null($description)) {
210
return get_column('artefact_file_mime_types', 'mimetype');
212
return get_column('artefact_file_mime_types', 'mimetype', 'description', $description);
208
215
public static function can_be_disabled() {
225
232
public static function recalculate_quota() {
226
233
$data = get_records_sql_assoc("
227
SELECT a.owner, SUM(f.size) AS usage
234
SELECT a.owner, SUM(f.size) AS bytes
228
235
FROM {artefact} a JOIN {artefact_file_files} f ON a.id = f.artefact
229
236
WHERE a.artefacttype IN ('file', 'image', 'profileicon', 'archive')
230
237
AND a.owner IS NOT NULL
231
238
GROUP BY a.owner", array()
234
return array_map(create_function('$a', 'return $a->usage;'), $data);
241
return array_map(create_function('$a', 'return $a->bytes;'), $data);
850
857
throw new QuotaExceededException(get_string('uploadexceedsquota', 'artefact.file'));
854
$data->filetype = $um->file['type'];
862
// the browser wasn't sure, so use mime_content_type to guess
863
if($um->file['type'] == 'application/octet-stream') {
864
$data->filetype = mime_content_type($um->file['tmp_name']);
867
$data->filetype = $um->file['type'];
855
870
$data->oldextension = $um->original_filename_extension();
856
871
$f = self::new_file($um->file['tmp_name'], $data);
1701
1716
$this->info = $zipinfo;
1719
private function read_entry($name, $isfolder, $size) {
1720
$path = split('/', $name);
1726
for ($i = 0; $i < count($path) - 1; $i++) {
1727
$folder .= $path[$i] . '/';
1728
if (!isset($this->foldernames[$folder])) {
1729
$this->foldernames[$folder] = 1;
1730
$this->info->names[] = $folder;
1731
$this->info->folders++;
1736
$this->info->names[] = $name;
1737
$this->info->files++;
1738
$this->info->totalsize += $size;
1704
1742
public function read_archive() {
1705
1743
if (!$this->handle) {
1706
1744
$this->open_archive();
1714
1752
'totalsize' => 0,
1715
1753
'names' => array(),
1756
$this->foldernames = array();
1717
1758
if ($this->archivetype == 'zip') {
1718
1759
while ($entry = zip_read($this->handle)) {
1719
1760
$name = zip_entry_name($entry);
1720
$this->info->names[] = $name;
1721
if (substr($name, -1) == '/') {
1722
$this->info->folders++;
1725
$this->info->files++;
1726
if ($size = zip_entry_filesize($entry)) {
1727
$this->info->totalsize += $size;
1761
$isfolder = substr($name, -1) == '/';
1762
$size = $isfolder ? 0 : zip_entry_filesize($entry);
1763
$this->read_entry($name, $isfolder, $size);
1732
1766
else if ($this->archivetype == 'tar') {
1733
$foldernames = array();
1734
1767
$list = $this->handle->listContent();
1735
1768
if (empty($list)) {
1736
1769
throw new SystemException("Unknown archive type");
1739
1771
foreach ($list as $entry) {
1740
$path = split('/', $entry['filename']);
1741
if ($isfolder = substr($entry['filename'], -1) == '/') {
1746
for ($i = 0; $i < count($path) - 1; $i++) {
1747
$folder .= $path[$i] . '/';
1748
if (!isset($foldernames[$folder])) {
1749
$foldernames[$folder] = 1;
1750
$this->info->names[] = $folder;
1751
$this->info->folders++;
1756
$this->info->names[] = $entry['filename'];
1757
$this->info->files++;
1758
$this->info->totalsize += $entry['size'];
1772
$isfolder = substr($entry['filename'], -1) == '/';
1773
$size = $isfolder ? 0 : $entry['size'];
1774
$this->read_entry($entry['filename'], $isfolder, $size);
1826
1841
$tempdir = get_config('dataroot') . 'artefact/file/temp';
1827
1842
check_dir_exists($tempdir);
1829
$this->read_archive();
1831
1844
if ($this->archivetype == 'tar') {
1846
$this->read_archive();
1833
1848
// Untar everything into a temp directory first
1834
1849
$tempsubdir = tempnam($tempdir, '');
1835
1850
unlink($tempsubdir);
1858
1873
} else if ($this->archivetype == 'zip') {
1875
$this->open_archive();
1860
1877
$tempfile = tempnam($tempdir, '');
1863
1880
while ($entry = zip_read($this->handle)) {
1864
1881
$name = zip_entry_name($entry);
1865
1882
$folder = dirname($name);
1884
// Create parent folders if necessary
1885
if (!isset($this->data['folderids'][$folder])) {
1888
$path = split('/', $folder);
1889
for ($i = 0; $i < count($path); $i++) {
1890
$child .= $path[$i] . '/';
1891
if (!isset($this->data['folderids'][$child])) {
1892
$this->data['template']->parent = $this->data['folderids'][$parent];
1893
$this->data['template']->title = $path[$i];
1894
$this->create_folder($parent);
1866
1900
$this->data['template']->parent = $this->data['folderids'][$folder];
1867
1901
$this->data['template']->title = basename($name);
1868
1902
if (substr($name, -1) == '/') {