+
+ /**
+ * 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, default = ''
+ * @param integer $filesize File size in bytes. optional, default = null
+ * @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
+ * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+ */
+ public static function store($data, $uid, $filename, $filetype = '' , $filesize = null, $allow_cid = '', $allow_gid = '', $deny_cid = '', $deny_gid = '')
+ {
+ if ($filetype === '') {
+ $filetype = Mimetype::getContentType($filename);
+ }
+
+ if (is_null($filesize)) {
+ $filesize = strlen($data);
+ }
+
+ $backend_ref = DI::storage()->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' => (string)DI::storage(),
+ 'backend-ref' => $backend_ref
+ ];
+
+ $r = DBA::insert('attach', $fields);
+ if ($r === true) {
+ return DBA::lastInsertId();
+ }
+ return $r;
+ }
+
+ /**
+ * Store new file metadata in db and binary in default backend from existing file
+ *
+ * @param $src
+ * @param $uid
+ * @param string $filename
+ * @param string $allow_cid
+ * @param string $allow_gid
+ * @param string $deny_cid
+ * @param string $deny_gid
+ * @return boolean True on success
+ * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+ */
+ public static 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, '', null, $allow_cid, $allow_gid, $deny_cid, $deny_gid);
+ }
+
+
+ /**
+ * Update an attached file
+ *
+ * @param array $fields Contains the fields that are updated
+ * @param array $conditions Condition array with the key values
+ * @param Image $img Image 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 successful?
+ *
+ * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+ * @see \Friendica\Database\DBA::update
+ */
+ public static function update($fields, $conditions, Image $img = null, array $old_fields = [])
+ {
+ if (!is_null($img)) {
+ // get items to update
+ $items = self::selectToArray(['backend-class','backend-ref'], $conditions);
+
+ foreach($items as $item) {
+ try {
+ $backend_class = DI::storageManager()->getWritableStorageByName($item['backend-class'] ?? '');
+ $fields['backend-ref'] = $backend_class->put($img->asString(), $item['backend-ref'] ?? '');
+ } catch (InvalidClassStorageException $storageException) {
+ DI::logger()->debug('Storage class not found.', ['conditions' => $conditions, 'exception' => $storageException]);
+ } catch (ReferenceStorageException $referenceStorageException) {
+ DI::logger()->debug('Item doesn\'t exist.', ['conditions' => $conditions, 'exception' => $referenceStorageException]);
+ }
+ }
+ }
+
+ $fields['edited'] = DateTimeFormat::utcNow();
+
+ return DBA::update('attach', $fields, $conditions, $old_fields);
+ }
+
+
+ /**
+ * Delete info from table and data from storage
+ *
+ * @param array $conditions Field condition(s)
+ * @param array $options Options array, Optional
+ *
+ * @return boolean
+ *
+ * @throws \Exception
+ * @see \Friendica\Database\DBA::delete
+ */
+ public static function delete(array $conditions, array $options = [])
+ {
+ // get items to delete data info
+ $items = self::selectToArray(['backend-class','backend-ref'], $conditions);
+
+ foreach($items as $item) {
+ try {
+ $backend_class = DI::storageManager()->getWritableStorageByName($item['backend-class'] ?? '');
+ $backend_class->delete($item['backend-ref'] ?? '');
+ } catch (InvalidClassStorageException $storageException) {
+ DI::logger()->debug('Storage class not found.', ['conditions' => $conditions, 'exception' => $storageException]);
+ } catch (ReferenceStorageException $referenceStorageException) {
+ DI::logger()->debug('Item doesn\'t exist.', ['conditions' => $conditions, 'exception' => $referenceStorageException]);
+ }
+ }
+
+ return DBA::delete('attach', $conditions, $options);
+ }
+}