/
mms_attachment.c
301 lines (273 loc) · 8.7 KB
/
mms_attachment.c
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* Copyright (C) 2013-2014 Jolla Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include "mms_attachment.h"
#include "mms_file_util.h"
#include "mms_codec.h"
#include <magic.h>
/* Logging */
#define MMS_LOG_MODULE_NAME mms_attachment_log
#include "mms_lib_log.h"
#include "mms_error.h"
MMS_LOG_MODULE_DEFINE("mms-attachment");
#define MMS_ATTACHMENT_DEFAULT_TYPE "application/octet-stream"
G_DEFINE_TYPE(MMSAttachment, mms_attachment, G_TYPE_OBJECT);
#define MMS_ATTACHMENT(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj), MMS_TYPE_ATTACHMENT, MMSAttachment))
#define MMS_ATTACHMENT_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_CLASS((obj), MMS_TYPE_ATTACHMENT, MMSAttachmentClass))
#define REGION_TEXT "Text"
#define REGION_MEDIA "Media"
#define MEDIA_TEXT "text"
#define MEDIA_IMAGE "img"
#define MEDIA_VIDEO "video"
#define MEDIA_AUDIO "audio"
#define MEDIA_OTHER "ref"
static
void
mms_attachment_finalize(
GObject* object)
{
MMSAttachment* at = MMS_ATTACHMENT(object);
g_mapped_file_unref(at->map);
if (!at->config->keep_temp_files &&
!(at->flags & MMS_ATTACHMENT_DONT_DELETE_FILES)) {
char* dir = g_path_get_dirname(at->file_name);
remove(at->file_name);
rmdir(dir);
g_free(dir);
}
g_free(at->file_name);
g_free(at->content_type);
g_free(at->content_location);
g_free(at->content_id);
G_OBJECT_CLASS(mms_attachment_parent_class)->finalize(object);
}
static
void
mms_attachment_class_init(
MMSAttachmentClass* klass)
{
G_OBJECT_CLASS(klass)->finalize = mms_attachment_finalize;
}
static
void
mms_attachment_init(
MMSAttachment* attachment)
{
MMS_VERBOSE_("%p", attachment);
}
static
char*
mms_attachment_get_path(
const char* file,
GError** error)
{
char* path = g_malloc(PATH_MAX);
if (realpath(file, path)) {
if (g_file_test(path, G_FILE_TEST_IS_REGULAR)) {
char* fname = g_strdup(path);
g_free(path);
return fname;
} else {
MMS_ERROR(error, MMS_LIB_ERROR_IO, "%s not found", file);
}
} else {
MMS_ERROR(error, MMS_LIB_ERROR_IO, "%s: %s\n", file, strerror(errno));
}
return NULL;
}
gboolean
mms_attachment_write_smil(
FILE* f,
MMSAttachment** ats,
int n,
GError** error)
{
if (fputs(
"<!DOCTYPE smil PUBLIC \"-//W3C//DTD SMIL 1.0//EN\" "
"\"http://www.w3.org/TR/REC-smil/SMIL10.dtd\">\n"
"<smil>\n"
" <head>\n"
" <layout>\n"
" <root-layout height=\"160\" width=\"120\"/>\n"
" <region fit=\"scroll\" height=\"100%\" left=\"0\" "
"top=\"0\" width=\"100%\" id=\"" REGION_TEXT "\"/>\n"
" <region fit=\"meet\" height=\"100%\" left=\"0\" "
"top=\"0\" width=\"100%\" id=\"" REGION_MEDIA "\"/>\n"
" </layout>\n"
" </head>\n"
" <body>\n"
" <par dur=\"5000ms\">\n", f) >= 0) {
int i;
for (i=0; i<n; i++) {
const MMSAttachment* at = ats[i];
const char* elem;
const char* region;
MMS_ASSERT(!(at->flags & MMS_ATTACHMENT_SMIL));
if (g_str_has_prefix(at->content_type, "text/")) {
elem = "text";
region = REGION_TEXT;
} else {
region = REGION_MEDIA;
if (g_str_has_prefix(at->content_type, "image/")) {
elem = "img";
} else if (g_str_has_prefix(at->content_type, "video/")) {
elem = "video";
} else if (g_str_has_prefix(at->content_type, "audio/")) {
elem = "audio";
} else {
elem = "ref";
}
}
if (fprintf(f, " <%s src=\"%s\" region=\"%s\"/>\n", elem,
at->content_location, region) < 0) {
break;
}
}
if (i == n && fputs(" </par>\n </body>\n</smil>\n", f) >= 0) {
return TRUE;
}
}
MMS_ERROR(error, MMS_LIB_ERROR_IO, "Error writing SMIL: %s",
strerror(errno));
return FALSE;
}
MMSAttachment*
mms_attachment_new_smil(
const MMSConfig* config,
const char* path,
MMSAttachment** ats,
int n,
GError** error)
{
MMSAttachment* smil = NULL;
int fd = open(path, O_CREAT|O_RDWR|O_TRUNC, MMS_FILE_PERM);
if (fd >= 0) {
FILE* f = fdopen(fd, "w");
if (f) {
gboolean ok = mms_attachment_write_smil(f, ats, n, error);
fclose(f);
if (ok) {
MMSAttachmentInfo ai;
ai.file_name = path;
ai.content_type = SMIL_CONTENT_TYPE "; charset=utf-8";
ai.content_id = NULL;
smil = mms_attachment_new(config, &ai, error);
MMS_ASSERT(smil && (smil->flags & MMS_ATTACHMENT_SMIL));
}
} else {
MMS_ERROR(error, MMS_LIB_ERROR_IO,
"Failed to open file %s: %s", path, strerror(errno));
close(fd);
}
} else {
MMS_ERROR(error, MMS_LIB_ERROR_IO,
"Failed to create file %s: %s", path, strerror(errno));
}
return smil;
}
MMSAttachment*
mms_attachment_new(
const MMSConfig* config,
const MMSAttachmentInfo* info,
GError** error)
{
char* path = mms_attachment_get_path(info->file_name, error);
if (path) {
GMappedFile* map = g_mapped_file_new(path, FALSE, error);
if (map) {
MMSAttachment* at = g_object_new(MMS_TYPE_ATTACHMENT, NULL);
at->config = config;
at->file_name = path;
at->map = map;
if (info->content_type) {
char** ct = mms_parse_http_content_type(info->content_type);
if (ct) {
at->content_type = mms_unparse_http_content_type(ct);
if (!strcmp(ct[0], SMIL_CONTENT_TYPE)) {
at->flags |= MMS_ATTACHMENT_SMIL;
}
g_strfreev(ct);
}
}
if (!at->content_type) {
/* Use magic to determine mime type */
const char* default_charset = "utf-8";
const char* content_type = NULL;
const char* charset = NULL;
const char* ct[4];
int n;
magic_t magic = magic_open(MAGIC_MIME_TYPE);
if (magic) {
if (magic_load(magic, NULL) == 0) {
content_type = magic_file(magic, path);
}
}
/* Magic detects SMIL as text/html */
if ((!content_type ||
g_str_has_prefix(content_type, "text/")) &&
mms_file_is_smil(path)) {
content_type = SMIL_CONTENT_TYPE;
}
if (!content_type) {
MMS_WARN("No mime type for %s", path);
content_type = MMS_ATTACHMENT_DEFAULT_TYPE;
}
if (!strcmp(content_type, SMIL_CONTENT_TYPE)) {
at->flags |= MMS_ATTACHMENT_SMIL;
charset = default_charset;
} else if (g_str_has_prefix(content_type, "text/")) {
charset = default_charset;
}
n = 0;
ct[n++] = content_type;
if (charset) {
ct[n++] = "charset";
ct[n++] = charset;
}
ct[n++] = NULL;
at->content_type = mms_unparse_http_content_type((char**)ct);
if (magic) magic_close(magic);
}
at->content_location = g_path_get_basename(path);
at->content_id = (info->content_id && info->content_id[0]) ?
g_strdup(info->content_id) :
g_strdup(at->content_location);
MMS_DEBUG("%s: %s", path, at->content_type);
return at;
}
g_free(path);
}
return NULL;
}
MMSAttachment*
mms_attachment_ref(
MMSAttachment* at)
{
if (at) g_object_ref(MMS_ATTACHMENT(at));
return at;
}
void
mms_attachment_unref(
MMSAttachment* at)
{
if (at) g_object_unref(MMS_ATTACHMENT(at));
}
/*
* Local Variables:
* mode: C
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/