/view.php |
@@ -14,52 +14,6 @@ |
### Load configuration. |
$config = spyc_load_file('config.yaml'); |
|
### If tags were specified then check whether the hash matches any tags. |
$tags = array(); |
if(isset($_GET['tags']) && !empty($_GET['tags'])) { |
## Extract the token field values. |
$tags = array_filter( |
array_map( |
function($item) { |
return $item->value; |
}, |
array_values( |
json_decode( |
stripslashes( |
$_GET['tags'] |
) |
) |
) |
) |
); |
|
## Connect or create the scratch database. |
$db = new PDO('sqlite:db/scratch.sqlite3'); |
|
## Set the error mode to exceptions. |
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
try { |
$db->beginTransaction(); |
|
## Create tags table if it does not exist. |
$db->query('CREATE TABLE IF NOT EXISTS "tags" ("hash" text NOT NULL COLLATE NOCASE, "tag" text COLLATE NOCASE, UNIQUE("hash", "tag") ON CONFLICT REPLACE)'); |
|
## Select all the hashes that match supplied tags. |
$q = $db->prepare('SELECT "hash" FROM "tags" WHERE tag IN ('.implode(',', array_fill(0, count($tags), '?')).')'); |
foreach($tags as $i => $tag) |
$q->bindValue($i+1, $tag); |
|
$result = $q->execute(); |
$tags = $q->fetchAll(PDO::FETCH_COLUMN, 0); |
$db->commit(); |
} catch(Exception $e) { |
error_log($e); |
## Rollback. |
$db->rollback(); |
} |
} |
|
### Open MIME. |
$finfo = finfo_open(FILEINFO_MIME_TYPE); |
if (!$finfo) { |
@@ -71,10 +25,10 @@ |
array_values( |
array_filter( |
array_map( |
function ($file) use ($config, $finfo, $tags) { |
function ($file) use ($config, $finfo) { |
#### Get the file hash. |
$fileHash = array_shift(explode('.', $file)); |
|
|
#### Build the user path. |
$userPath = join( |
DIRECTORY_SEPARATOR, |
@@ -83,7 +37,7 @@ |
$file |
) |
); |
|
|
#### If the extension is not allowed then skip this file. |
$fileExtension = pathinfo($userPath, PATHINFO_EXTENSION); |
if (!isset($fileExtension) || |
@@ -90,7 +44,7 @@ |
!in_array(strtoupper($fileExtension), |
array_map('strtoupper', $config['ALLOWED_FILE_EXTENSIONS']))) |
return null; |
|
|
#### Hook for various file extensions. |
$opengraph = FALSE; |
switch (strtoupper($fileExtension)) { |
@@ -109,7 +63,7 @@ |
$config['ASSET_HASH_SIZE'] |
) |
); |
|
|
### Build the user path. |
$previewFile = join( |
DIRECTORY_SEPARATOR, |
@@ -118,13 +72,13 @@ |
$previewHash |
) |
); |
|
|
### Do not re-create the thumbnail if it already exists. |
if (file_exists($previewFile.'.'.'jpg')) { |
$opengraph = TRUE; |
break; |
} |
|
|
### Extract thumbnail. |
$ffmpeg = FFMpeg\FFMpeg::create(); |
$video = $ffmpeg->open($userPath); |
@@ -150,11 +104,6 @@ |
default: |
return null; |
} |
|
if(!empty($tags) && !in_array($fileHash, $tags)) { |
return array(); |
} |
|
return array( |
'url' => $fileHash, |
'type' => finfo_file($finfo, $userPath), |
@@ -182,4 +131,4 @@ |
) |
); |
|
finfo_close($finfo); |
finfo_close($finfo); |