Attach: store, update, delete. Model and views

This commit is contained in:
fabrixxm 2019-01-02 16:17:29 +01:00 committed by Hypolite Petovan
parent fc2b804ccc
commit b7b3086263
3 changed files with 143 additions and 31 deletions

View File

@ -8,6 +8,7 @@ use Friendica\Core\Config;
use Friendica\Core\L10n;
use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\Model\Attach;
use Friendica\Model\Contact;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Mimetype;
@ -126,20 +127,11 @@ function wall_attach_post(App $a) {
exit();
}
$filedata = @file_get_contents($src);
$mimetype = Mimetype::getContentType($filename);
$hash = System::createGUID(64);
$created = DateTimeFormat::utcNow();
$fields = ['uid' => $page_owner_uid, 'hash' => $hash, 'filename' => $filename, 'filetype' => $mimetype,
'filesize' => $filesize, 'data' => $filedata, 'created' => $created, 'edited' => $created,
'allow_cid' => '<' . $page_owner_cid . '>', 'allow_gid' => '','deny_cid' => '', 'deny_gid' => ''];
$r = DBA::insert('attach', $fields);
$newid = Attach::storeFile($src, $page_owner_uid, $filename, '<' . $page_owner_cid . '>');
@unlink($src);
if (! $r) {
if ($newid === false) {
$msg = L10n::t('File upload failed.');
if ($r_json) {
echo json_encode(['error' => $msg]);
@ -149,30 +141,14 @@ function wall_attach_post(App $a) {
exit();
}
$r = q("SELECT `id` FROM `attach` WHERE `uid` = %d AND `created` = '%s' AND `hash` = '%s' LIMIT 1",
intval($page_owner_uid),
DBA::escape($created),
DBA::escape($hash)
);
if (! DBA::isResult($r)) {
$msg = L10n::t('File upload failed.');
if ($r_json) {
echo json_encode(['error' => $msg]);
} else {
echo $msg . EOL;
}
exit();
}
if ($r_json) {
echo json_encode(['ok' => true]);
echo json_encode(['ok' => true, 'id' => $newid]);
exit();
}
$lf = "\n";
echo $lf . $lf . '[attachment]' . $r[0]['id'] . '[/attachment]' . $lf;
echo $lf . $lf . '[attachment]' . $newid . '[/attachment]' . $lf;
exit();
// NOTREACHED

View File

@ -149,4 +149,138 @@ class Attach extends BaseObject
return $backendClass::get($backendRef);
}
}
}
/**
* @brief Store new file metadata in db and binary in default backend
*
* @param string $data Binary data
* @param integer $uid User ID
* @param string $filename Filename
* @param string $filetype Mimetype. optional
* @param integer $filesize File size in bytes. optional
* @param string $allow_cid Permissions, allowed contacts. optional, default = ''
* @param string $allow_gid Permissions, allowed groups. optional, default = ''
* @param string $deny_cid Permissions, denied contacts.optional, default = ''
* @param string $deny_gid Permissions, denied greoup.optional, default = ''
*
* @return boolean/integer Row id on success, False on errors
*/
public function store($data, $uid, $filename, $filetype = '' , $filesize = -1, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '')
{
if ($filetype === '') {
$filetype = Mimetype::getContentType($filename);
}
if ($filesize < 0) {
$filesize = strlen($data);
}
$backend_class = StorageManager::getBackend();
$backend_ref = '';
if ($backend_class !== '') {
$backend_ref = $backend_class::put($data);
$data = '';
}
$hash = System::createGUID(64);
$created = DateTimeFormat::utcNow();
$fields = [
'uid' => $uid,
'hash' => $hash,
'filename' => $filename,
'filetype' => $filetype,
'filesize' => $filesize,
'data' => $data,
'created' => $created,
'edited' => $created,
'allow_cid' => $allow_cid,
'allow_gid' => $allow_gid,
'deny_cid' => $deny_cid,
'deny_gid' => $deny_gid,
'backend-class' => $backend_class,
'backend-ref' => $backend_ref
];
$r = DBA::insert('attach', $fields);
if ($r === true) {
return DBA::lastInsertId();
}
}
/**
* @brief Store new file metadata in db and binary in default backend from existing file
*
* @return boolean True on success
*/
public function storeFile($src, $uid, $filename = '', $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '')
{
if ($filename === '') {
$filename = basename($src);
}
$data = @file_get_contents($src);
return self::store($data, $uid, $filename, '', '', $allow_cid, $allow_gid, $deny_cid, $deny_gid);
}
/**
* @brief Update an attached file
*
* @param array $fields Contains the fields that are updated
* @param array $conditions Condition array with the key values
* @param string $data File data to update. Optional, default null.
* @param array|boolean $old_fields Array with the old field values that are about to be replaced (true = update on duplicate)
*
* @return boolean Was the update successfull?
*
* @see \Friendica\Database\DBA::update
*/
public static function update($fields, $conditions, $img = null, array $old_fields = [])
{
if (!is_null($data)) {
// get items to update
$items = self::select(['backend-class','backend-ref'], $conditions);
foreach($items as $item) {
$backend_class = (string)$item['backend-class'];
if ($backend_class !== '') {
$fields['backend-ref'] = $backend_class::put($img->asString(), $item['backend-ref']);
} else {
$fields['data'] = $data;
}
}
}
$fields['edited'] = DateTimeFormat::utcNow();
return DBA::update('attach', $fields, $conditions, $old_fields);
}
/**
* @brief Delete info from table and data from storage
*
* @param array $conditions Field condition(s)
* @param array $options Options array, Optional
*
* @return boolean
*
* @see \Friendica\Database\DBA::delete
*/
public static function delete(array $conditions, array $options = [])
{
// get items to delete data info
$items = self::select(['backend-class','backend-ref'], $conditions);
foreach($items as $item) {
$backend_class = (string)$item['backend-class'];
if ($backend_class !== '') {
$backend_class::delete($item['backend-ref']);
}
}
return DBA::delete('attach', $conditions, $options);
}
}

View File

@ -26,6 +26,8 @@ use Friendica\Model\FileTag;
use Friendica\Model\PermissionSet;
use Friendica\Model\Term;
use Friendica\Model\ItemURI;
use Friendica\Model\Photo;
use Friendica\Model\Attach;
use Friendica\Object\Image;
use Friendica\Protocol\Diaspora;
use Friendica\Protocol\OStatus;
@ -1039,7 +1041,7 @@ class Item extends BaseObject
foreach (explode(", ", $item['attach']) as $attach) {
preg_match("|attach/(\d+)|", $attach, $matches);
if (is_array($matches) && count($matches) > 1) {
DBA::delete('attach', ['id' => $matches[1], 'uid' => $item['uid']]);
Attach::delete(['id' => $matches[1], 'uid' => $item['uid']]);
}
}