scratch

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 138  →  ?path2? @ 139
/view.php
@@ -14,6 +14,52 @@
### 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) {
@@ -25,10 +71,10 @@
array_values(
array_filter(
array_map(
function ($file) use ($config, $finfo) {
function ($file) use ($config, $finfo, $tags) {
#### Get the file hash.
$fileHash = array_shift(explode('.', $file));
#### Build the user path.
$userPath = join(
DIRECTORY_SEPARATOR,
@@ -37,7 +83,7 @@
$file
)
);
#### If the extension is not allowed then skip this file.
$fileExtension = pathinfo($userPath, PATHINFO_EXTENSION);
if (!isset($fileExtension) ||
@@ -44,7 +90,7 @@
!in_array(strtoupper($fileExtension),
array_map('strtoupper', $config['ALLOWED_FILE_EXTENSIONS'])))
return null;
#### Hook for various file extensions.
$opengraph = FALSE;
switch (strtoupper($fileExtension)) {
@@ -63,7 +109,7 @@
$config['ASSET_HASH_SIZE']
)
);
### Build the user path.
$previewFile = join(
DIRECTORY_SEPARATOR,
@@ -72,13 +118,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);
@@ -104,6 +150,11 @@
default:
return null;
}
if(!empty($tags) && !in_array($fileHash, $tags)) {
return array();
}
return array(
'url' => $fileHash,
'type' => finfo_file($finfo, $userPath),
@@ -131,4 +182,4 @@
)
);
 
finfo_close($finfo);
finfo_close($finfo);