]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/File_thumbnail.php
File_thumbnail fixes (run scripts/upgrade.php)
[quix0rs-gnu-social.git] / classes / File_thumbnail.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
21
22 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
23
24 /**
25  * Table Definition for file_thumbnail
26  */
27
28 class File_thumbnail extends Managed_DataObject
29 {
30     public $__table = 'file_thumbnail';                  // table name
31     public $file_id;                         // int(4)  primary_key not_null
32     public $url;                             // varchar(255)  unique_key
33     public $filename;                        // varchar(255)
34     public $width;                           // int(4)  primary_key
35     public $height;                          // int(4)  primary_key
36     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
37
38     public static function schemaDef()
39     {
40         return array(
41             'fields' => array(
42                 'file_id' => array('type' => 'int', 'not null' => true, 'description' => 'thumbnail for what URL/file'),
43                 'url' => array('type' => 'varchar', 'length' => 255, 'description' => 'URL of thumbnail'),
44                 'filename' => array('type' => 'varchar', 'length' => 255, 'description' => 'if stored locally, filename is put here'),
45                 'width' => array('type' => 'int', 'description' => 'width of thumbnail'),
46                 'height' => array('type' => 'int', 'description' => 'height of thumbnail'),
47                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
48             ),
49             'primary key' => array('file_id', 'width', 'height'),
50             'indexes' => array(
51                 'file_thumbnail_file_id_idx' => array('file_id'),
52             ),
53             'foreign keys' => array(
54                 'file_thumbnail_file_id_fkey' => array('file', array('file_id' => 'id')),
55             )
56         );
57     }
58
59     /**
60      * Save oEmbed-provided thumbnail data
61      *
62      * @param object $data
63      * @param int $file_id
64      */
65     public static function saveNew($data, $file_id) {
66         if (!empty($data->thumbnail_url)) {
67             // Non-photo types such as video will usually
68             // show us a thumbnail, though it's not required.
69             self::saveThumbnail($file_id,
70                                 $data->thumbnail_url,
71                                 $data->thumbnail_width,
72                                 $data->thumbnail_height);
73         } else if ($data->type == 'photo') {
74             // The inline photo URL given should also fit within
75             // our requested thumbnail size, per oEmbed spec.
76             self::saveThumbnail($file_id,
77                                 $data->url,
78                                 $data->width,
79                                 $data->height);
80         }
81     }
82
83     /**
84      * Save a thumbnail record for the referenced file record.
85      *
86      * FIXME: Add error handling
87      *
88      * @param int $file_id
89      * @param string $url
90      * @param int $width
91      * @param int $height
92      */
93     static function saveThumbnail($file_id, $url, $width, $height, $filename=null)
94     {
95         $tn = new File_thumbnail;
96         $tn->file_id = $file_id;
97         $tn->url = $url;
98         $tn->filename = $filename;
99         $tn->width = intval($width);
100         $tn->height = intval($height);
101         $tn->insert();
102         return $tn;
103     }
104
105     static function path($filename)
106     {
107         // TODO: Store thumbnails in their own directory and don't use File::path here
108         return File::path($filename);
109     }
110
111     public function getUrl()
112     {
113         return $this->url;
114     }
115
116     public function delete($useWhere=false)
117     {
118         if (!empty($this->filename) && file_exists(File_thumbnail::path($this->filename))) {
119             $deleted = @unlink(self::path($this->filename));
120             if (!$deleted) {
121                 common_log(LOG_ERR, sprintf('Could not unlink existing file: "%s"', self::path($this->filename)));
122             }
123         }
124
125         return parent::delete($useWhere);
126     }
127
128     public function getFile()
129     {
130         $file = new File();
131         $file->id = $this->file_id;
132         if (!$file->find(true)) {
133             throw new NoResultException($file);
134         }
135         return $file;
136     }
137 }