3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('GNUSOCIAL')) { exit(1); }
23 * Table Definition for file_oembed
26 class File_oembed extends Managed_DataObject
28 public $__table = 'file_oembed'; // table name
29 public $file_id; // int(4) primary_key not_null
30 public $version; // varchar(20)
31 public $type; // varchar(20)
32 public $mimetype; // varchar(50)
33 public $provider; // varchar(50)
34 public $provider_url; // varchar(191) not 255 because utf8mb4 takes more space
35 public $width; // int(4)
36 public $height; // int(4)
37 public $html; // text()
38 public $title; // varchar(191) not 255 because utf8mb4 takes more space
39 public $author_name; // varchar(50)
40 public $author_url; // varchar(191) not 255 because utf8mb4 takes more space
41 public $url; // varchar(191) not 255 because utf8mb4 takes more space
42 public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
44 public static function schemaDef()
48 'file_id' => array('type' => 'int', 'not null' => true, 'description' => 'oEmbed for that URL/file'),
49 'version' => array('type' => 'varchar', 'length' => 20, 'description' => 'oEmbed spec. version'),
50 'type' => array('type' => 'varchar', 'length' => 20, 'description' => 'oEmbed type: photo, video, link, rich'),
51 'mimetype' => array('type' => 'varchar', 'length' => 50, 'description' => 'mime type of resource'),
52 'provider' => array('type' => 'varchar', 'length' => 50, 'description' => 'name of this oEmbed provider'),
53 'provider_url' => array('type' => 'varchar', 'length' => 191, 'description' => 'URL of this oEmbed provider'),
54 'width' => array('type' => 'int', 'description' => 'width of oEmbed resource when available'),
55 'height' => array('type' => 'int', 'description' => 'height of oEmbed resource when available'),
56 'html' => array('type' => 'text', 'description' => 'html representation of this oEmbed resource when applicable'),
57 'title' => array('type' => 'varchar', 'length' => 191, 'description' => 'title of oEmbed resource when available'),
58 'author_name' => array('type' => 'varchar', 'length' => 50, 'description' => 'author name for this oEmbed resource'),
59 'author_url' => array('type' => 'varchar', 'length' => 191, 'description' => 'author URL for this oEmbed resource'),
60 'url' => array('type' => 'varchar', 'length' => 191, 'description' => 'URL for this oEmbed resource when applicable (photo, link)'),
61 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
63 'primary key' => array('file_id'),
64 'foreign keys' => array(
65 'file_oembed_file_id_fkey' => array('file', array('file_id' => 'id')),
70 static function _getOembed($url) {
72 'maxwidth' => common_config('thumbnail', 'width'),
73 'maxheight' => common_config('thumbnail', 'height'),
76 return oEmbedHelper::getObject($url, $parameters);
77 } catch (Exception $e) {
78 common_log(LOG_INFO, "Error during oembed lookup for $url - " . $e->getMessage());
84 * Fetch an entry by using a File's id
86 static function getByFile(File $file) {
87 $fo = new File_oembed();
88 $fo->file_id = $file->id;
89 if (!$fo->find(true)) {
90 throw new NoResultException($fo);
95 public function getUrl()
101 * Save embedding info for a new file.
103 * @param object $data Services_oEmbed_Object_*
104 * @param int $file_id
106 public static function saveNew($data, $file_id) {
107 $file_oembed = new File_oembed;
108 $file_oembed->file_id = $file_id;
109 if (!isset($data->version)) {
110 common_debug('DEBUGGING oEmbed: data->version undefined in variable $data: '.var_export($data, true));
112 $file_oembed->version = $data->version;
113 $file_oembed->type = $data->type;
114 if (!empty($data->provider_name)) $file_oembed->provider = $data->provider_name;
115 if (!empty($data->provider)) $file_oembed->provider = $data->provider;
116 if (!empty($data->provider_url)) $file_oembed->provider_url = $data->provider_url;
117 if (!empty($data->width)) $file_oembed->width = intval($data->width);
118 if (!empty($data->height)) $file_oembed->height = intval($data->height);
119 if (!empty($data->html)) $file_oembed->html = $data->html;
120 if (!empty($data->title)) $file_oembed->title = $data->title;
121 if (!empty($data->author_name)) $file_oembed->author_name = $data->author_name;
122 if (!empty($data->author_url)) $file_oembed->author_url = $data->author_url;
123 if (!empty($data->url)){
124 $file_oembed->url = $data->url;
125 $given_url = File_redirection::_canonUrl($file_oembed->url);
126 if (! empty($given_url)){
127 $file = File::getKV('url', $given_url);
128 if ($file instanceof File) {
129 $file_oembed->mimetype = $file->mimetype;
131 $file_redir = File_redirection::getKV('url', $given_url);
132 if (empty($file_redir)) {
133 $redir_data = File_redirection::where($given_url);
134 $file_oembed->mimetype = $redir_data['type'];
136 $file_id = $file_redir->file_id;
141 $result = $file_oembed->insert();
142 if ($result === false) {
143 throw new ServerException('Failed to insert File_oembed data into database!');
145 if (!empty($data->thumbnail_url) || ($data->type == 'photo')) {
146 $ft = File_thumbnail::getKV('file_id', $file_id);
147 if ($ft instanceof File_thumbnail) {
148 common_log(LOG_WARNING, "Strangely, a File_thumbnail object exists for new file $file_id",
151 File_thumbnail::saveNew($data, $file_id);