/
MediaFile.php
160 lines (141 loc) · 5.46 KB
/
MediaFile.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
require_once 'Dase/DBO/Autogen/MediaFile.php';
class Dase_DBO_MediaFile extends Dase_DBO_Autogen_MediaFile
{
public $url = '';
function getItem()
{
$item = new Dase_DBO_Item($this->db);
$item->load($this->item_id);
return $item;
}
function getCollection()
{
$coll = new Dase_DBO_Collection($this->db);
$coll->load($this->getItem()->collection_id);
return $coll;
}
public function moveFileToDeleted($path_to_media) {
$c = $this->getCollection();
$dest = $path_to_media.'/'.$c->ascii_id.'/deleted/'.$this->filename;
$src = $this->getLocalPath($path_to_media);
//don't delete if it is a media icon
$icon_path = BASE_PATH.'/www/images/thumb_icons/'.$this->filename;
if (file_exists($icon_path)) { return; }
if (file_exists($src) && copy($src,$dest)) {
@unlink($src);
}
}
public function getLocalPath($path_to_media)
{
$c = $this->getCollection();
$subdir = Dase_Util::getSubdir($this->p_serial_number);
$path = $path_to_media.'/'.
$c->ascii_id.'/'.
$this->size.'/'.
$subdir.'/'.
$this->filename;
if (file_exists($path)) {
return $path;
} else {
$path = $path_to_media.'/'.
$c->ascii_id.'/'.
$this->size.'/'.
$this->filename;
return $path;
}
}
function getLink($app_root) {
return "$app_root/media/{$this->p_collection_ascii_id}/$this->size/$this->filename";
}
function getRelativeLink() {
return "media/{$this->p_collection_ascii_id}/$this->size/$this->filename";
}
public static function getUniqueBaseIdent($db,$title,$collection_ascii_id)
{
$check_ident = Dase_Util::dirify($title);
$mf = new Dase_DBO_MediaFile($db);
$mf->p_serial_number = $check_ident;
$mf->p_collection_ascii_id = $collection_ascii_id;
if (!$mf->findOne()) {
return $check_ident;
} else {
$check_ident = $check_ident.time();
return Dase_DBO_MediaFile::getUniqueBaseIdent($db,$check_ident,$collection_ascii_id);
}
}
function asAtom($app_root)
{
$entry = new Dase_Atom_Entry;
//may need to add edit links here
return $this->injectAtomEntryData($entry,$app_root);
}
function getDerivatives()
{
$m = new Dase_DBO_MediaFile($this->db);
$m->p_collection_ascii_id = $this->p_collection_ascii_id;
$m->p_serial_number = $this->p_serial_number;
$m->orderBy('width');
$m->addWhere('size',$this->size,'!=');
return $m->find();
}
function injectAtomEntryData(Dase_Atom_Entry $entry,$app_root)
{
$d = "http://daseproject.org/ns/1.0";
//this function assumes p_collection_ascii_id & p_serial_number are set
$entry->setId($this->getLink($app_root));
$entry->setTitle($this->filename);
$entry->addAuthor();
$entry->setUpdated($this->updated);
$entry->setSummary('');
//for AtomPub
$entry->setEdited($this->updated);
$edit_url = $app_root.'/media/'.$this->p_collection_ascii_id.'/'.$this->p_serial_number;
$entry->addLink($edit_url,'edit');
$ext = Dase_File::$types_map[$this->mime_type]['ext'];
$edit_media_url = $app_root.'/media/'.$this->p_collection_ascii_id.'/'.$this->p_serial_number;
$entry->addLink($edit_media_url,'edit-media');
//to which we can post form-encoded or json metadata pairs:
$entry->addLink(
$app_root.'/item/'.$this->p_collection_ascii_id.'/'.$this->p_serial_number.'/metadata',
'http://daseproject.org/relation/edit-metadata');
//link to the owning dase item using iana registered 'up' rel value
$entry->addLink(
$app_root.'/item/'.$this->p_collection_ascii_id.'/'.$this->p_serial_number.'.atom',
'up');
$entry->setMediaContent($this->getLink($app_root),$this->mime_type);
$media_group = $entry->addElement('media:group',null,Dase_Atom::$ns['media']);
//todo: beef up w/ bitrate, samplingrate, etc.
foreach ($this->getDerivatives() as $med) {
if ($med->size == 'thumbnail') {
//$media_thumbnail = $entry->addElement('media:thumbnail',null,Dase_Atom::$ns['media']);
$media_thumbnail = $media_group->appendChild($entry->dom->createElementNS(Dase_Atom::$ns['media'],'thumbnail'));
$media_thumbnail->setAttribute('url',$med->getLink($app_root));
$media_thumbnail->setAttribute('width',$med->width);
$media_thumbnail->setAttribute('height',$med->height);
}
if ($med->size == 'viewitem') {
//$media_viewitem = $entry->addElement('media:content',null,Dase_Atom::$ns['media']);
$media_viewitem = $media_group->appendChild($entry->dom->createElementNS(Dase_Atom::$ns['media'],'content'));
$media_viewitem->setAttribute('url',$med->getLink($app_root));
$media_viewitem->setAttribute('width',$med->width);
$media_viewitem->setAttribute('height',$med->height);
$media_viewitem->setAttribute('fileSize',$med->file_size);
$media_viewitem->setAttribute('type',$med->mime_type);
$media_category = $media_viewitem->appendChild($entry->dom->createElement('media:category'));
$media_category->appendChild($entry->dom->createTextNode($med->size));
}
if ($med->size != 'thumbnail' && $med->size != 'viewitem') {
$media_content = $media_group->appendChild($entry->dom->createElementNS(Dase_Atom::$ns['media'],'content'));
$media_content->setAttribute('url',$med->getLink($app_root));
$media_content->setAttribute('width',$med->width);
$media_content->setAttribute('height',$med->height);
$media_content->setAttribute('fileSize',$med->file_size);
$media_content->setAttribute('type',$med->mime_type);
$media_category = $media_content->appendChild($entry->dom->createElement('media:category'));
$media_category->appendChild($entry->dom->createTextNode($med->size));
}
}
return $entry->asXml();
}
}