Skip to content
Snippets Groups Projects
Commit d0377b19 authored by Robin Appelman's avatar Robin Appelman
Browse files

Cache: normalize mimetypes

parent ad3badea
No related branches found
No related tags found
No related merge requests found
...@@ -94,6 +94,42 @@ ...@@ -94,6 +94,42 @@
</table> </table>
<table>
<name>*dbprefix*mimetypes</name>
<declaration>
<field>
<name>id</name>
<type>integer</type>
<default>0</default>
<notnull>true</notnull>
<autoincrement>1</autoincrement>
<length>4</length>
</field>
<field>
<name>mimetype</name>
<type>text</type>
<default></default>
<notnull>true</notnull>
<length>64</length>
</field>
<index>
<name>mimetype_id_index</name>
<unique>true</unique>
<field>
<name>mimetype</name>
<sorting>ascending</sorting>
</field>
</index>
</declaration>
</table>
<table> <table>
<name>*dbprefix*filecache</name> <name>*dbprefix*filecache</name>
...@@ -151,18 +187,18 @@ ...@@ -151,18 +187,18 @@
<field> <field>
<name>mimetype</name> <name>mimetype</name>
<type>text</type> <type>integer</type>
<default></default> <default></default>
<notnull>true</notnull> <notnull>true</notnull>
<length>64</length> <length>4</length>
</field> </field>
<field> <field>
<name>mimepart</name> <name>mimepart</name>
<type>text</type> <type>integer</type>
<default></default> <default></default>
<notnull>true</notnull> <notnull>true</notnull>
<length>32</length> <length>4</length>
</field> </field>
<field> <field>
......
...@@ -36,6 +36,9 @@ class Cache { ...@@ -36,6 +36,9 @@ class Cache {
*/ */
private $numericId; private $numericId;
private $mimetypeIds = array();
private $mimetypes = array();
/** /**
* @param \OC\Files\Storage\Storage|string $storage * @param \OC\Files\Storage\Storage|string $storage
*/ */
...@@ -61,6 +64,41 @@ class Cache { ...@@ -61,6 +64,41 @@ class Cache {
return $this->numericId; return $this->numericId;
} }
/**
* normalize mimetypes
*
* @param string $mime
* @return int
*/
public function getMimetypeId($mime) {
if (!isset($this->mimetypeIds[$mime])) {
$query = \OC_DB::prepare('SELECT `id` FROM `*PREFIX*mimetypes` WHERE `mimetype` = ?');
$result = $query->execute(array($mime));
if ($row = $result->fetchRow()) {
$this->mimetypeIds[$mime] = $row['id'];
} else {
$query = \OC_DB::prepare('INSERT INTO `*PREFIX*mimetypes`(`mimetype`) VALUES(?)');
$query->execute(array($mime));
$this->mimetypeIds[$mime] = \OC_DB::insertid('*PREFIX*mimetypes');
}
$this->mimetypes[$this->mimetypeIds[$mime]] = $mime;
}
return $this->mimetypeIds[$mime];
}
public function getMimetype($id) {
if (!isset($this->mimetypes[$id])) {
$query = \OC_DB::prepare('SELECT `mimetype` FROM `*PREFIX*mimetypes` WHERE `id` = ?');
$result = $query->execute(array($id));
if ($row = $result->fetchRow()) {
$this->mimetypes[$id] = $row['mimetype'];
} else {
return null;
}
}
return $this->mimetypes[$id];
}
/** /**
* get the stored metadata of a file or folder * get the stored metadata of a file or folder
* *
...@@ -92,6 +130,8 @@ class Cache { ...@@ -92,6 +130,8 @@ class Cache {
$data['size'] = (int)$data['size']; $data['size'] = (int)$data['size'];
$data['mtime'] = (int)$data['mtime']; $data['mtime'] = (int)$data['mtime'];
$data['encrypted'] = (bool)$data['encrypted']; $data['encrypted'] = (bool)$data['encrypted'];
$data['mimetype'] = $this->getMimetype($data['mimetype']);
$data['mimepart'] = $this->getMimetype($data['mimepart']);
} }
return $data; return $data;
...@@ -110,7 +150,12 @@ class Cache { ...@@ -110,7 +150,12 @@ class Cache {
'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, `encrypted` 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, `encrypted`
FROM `*PREFIX*filecache` WHERE parent = ? ORDER BY `name` ASC'); FROM `*PREFIX*filecache` WHERE parent = ? ORDER BY `name` ASC');
$result = $query->execute(array($fileId)); $result = $query->execute(array($fileId));
return $result->fetchAll(); $files = $result->fetchAll();
foreach ($files as &$file) {
$file['mimetype'] = $this->getMimetype($file['mimetype']);
$file['mimepart'] = $this->getMimetype($file['mimepart']);
}
return $files;
} else { } else {
return array(); return array();
} }
...@@ -179,22 +224,23 @@ class Cache { ...@@ -179,22 +224,23 @@ class Cache {
* @param array $data * @param array $data
* @return array * @return array
*/ */
static function buildParts(array $data) { function buildParts(array $data) {
$fields = array('path', 'parent', 'name', 'mimetype', 'size', 'mtime', 'encrypted'); $fields = array('path', 'parent', 'name', 'mimetype', 'size', 'mtime', 'encrypted');
$params = array(); $params = array();
$queryParts = array(); $queryParts = array();
foreach ($data as $name => $value) { foreach ($data as $name => $value) {
if (array_search($name, $fields) !== false) { if (array_search($name, $fields) !== false) {
$params[] = $value;
$queryParts[] = '`' . $name . '`';
if ($name === 'path') { if ($name === 'path') {
$params[] = md5($value); $params[] = md5($value);
$queryParts[] = '`path_hash`'; $queryParts[] = '`path_hash`';
} elseif ($name === 'mimetype') { } elseif ($name === 'mimetype') {
$params[] = substr($value, 0, strpos($value, '/')); $params[] = $this->getMimetypeId(substr($value, 0, strpos($value, '/')));
$queryParts[] = '`mimepart`'; $queryParts[] = '`mimepart`';
$value = $this->getMimetypeId($value);
} }
$params[] = $value;
$queryParts[] = '`' . $name . '`';
} }
} }
return array($queryParts, $params); return array($queryParts, $params);
...@@ -339,6 +385,8 @@ class Cache { ...@@ -339,6 +385,8 @@ class Cache {
$result = $query->execute(array($pattern, $this->numericId)); $result = $query->execute(array($pattern, $this->numericId));
$files = array(); $files = array();
while ($row = $result->fetchRow()) { while ($row = $result->fetchRow()) {
$row['mimetype'] = $this->getMimetype($row['mimetype']);
$row['mimepart'] = $this->getMimetype($row['mimepart']);
$files[] = $row; $files[] = $row;
} }
return $files; return $files;
...@@ -360,6 +408,7 @@ class Cache { ...@@ -360,6 +408,7 @@ class Cache {
SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, `encrypted` SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, `encrypted`
FROM `*PREFIX*filecache` WHERE ' . $where . ' AND `storage` = ?' FROM `*PREFIX*filecache` WHERE ' . $where . ' AND `storage` = ?'
); );
$mimetype = $this->getMimetypeId($mimetype);
$result = $query->execute(array($mimetype, $this->numericId)); $result = $query->execute(array($mimetype, $this->numericId));
return $result->fetchAll(); return $result->fetchAll();
} }
......
...@@ -693,7 +693,7 @@ class View { ...@@ -693,7 +693,7 @@ class View {
$data = $cache->get($internalPath); $data = $cache->get($internalPath);
if ($data) { if ($data and $data['fileid']) {
if ($data['mimetype'] === 'httpd/unix-directory') { if ($data['mimetype'] === 'httpd/unix-directory') {
//add the sizes of other mountpoints to the folder //add the sizes of other mountpoints to the folder
$mountPoints = Filesystem::getMountPoints($path); $mountPoints = Filesystem::getMountPoints($path);
......
...@@ -74,7 +74,7 @@ class OC_Util { ...@@ -74,7 +74,7 @@ class OC_Util {
*/ */
public static function getVersion() { public static function getVersion() {
// hint: We only can count up. So the internal version number of ownCloud 4.5 will be 4.90.0. This is not visible to the user // hint: We only can count up. So the internal version number of ownCloud 4.5 will be 4.90.0. This is not visible to the user
return array(4,91,04); return array(4,91,05);
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment