File: poppler-media.cc

package info (click to toggle)
poppler 20.09.0-3.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 16,092 kB
  • sloc: cpp: 151,577; ansic: 30,155; python: 367; makefile: 42; sh: 36
file content (273 lines) | stat: -rw-r--r-- 7,781 bytes parent folder | download
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
/* poppler-media.cc: glib interface to MediaRendition
 *
 * Copyright (C) 2010 Carlos Garcia Campos <carlosgc@gnome.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "config.h"

#include <cerrno>

#include <goo/gfile.h>

#include "poppler-media.h"
#include "poppler-private.h"

/**
 * SECTION: poppler-media
 * @short_description: Media
 * @title: PopplerMedia
 */

typedef struct _PopplerMediaClass PopplerMediaClass;

struct _PopplerMedia
{
    GObject parent_instance;

    gchar *filename;

    gchar *mime_type;
    Object stream;
};

struct _PopplerMediaClass
{
    GObjectClass parent_class;
};

G_DEFINE_TYPE(PopplerMedia, poppler_media, G_TYPE_OBJECT)

static void poppler_media_finalize(GObject *object)
{
    PopplerMedia *media = POPPLER_MEDIA(object);

    if (media->filename) {
        g_free(media->filename);
        media->filename = nullptr;
    }

    if (media->mime_type) {
        g_free(media->mime_type);
        media->mime_type = nullptr;
    }

    media->stream = Object();

    G_OBJECT_CLASS(poppler_media_parent_class)->finalize(object);
}

static void poppler_media_class_init(PopplerMediaClass *klass)
{
    GObjectClass *gobject_class = G_OBJECT_CLASS(klass);

    gobject_class->finalize = poppler_media_finalize;
}

static void poppler_media_init(PopplerMedia *media) { }

PopplerMedia *_poppler_media_new(const MediaRendition *poppler_media)
{
    PopplerMedia *media;

    g_assert(poppler_media != nullptr);

    media = POPPLER_MEDIA(g_object_new(POPPLER_TYPE_MEDIA, nullptr));

    if (poppler_media->getIsEmbedded()) {
        const GooString *mime_type;

        media->stream = poppler_media->getEmbbededStreamObject()->copy();
        mime_type = poppler_media->getContentType();
        if (mime_type)
            media->mime_type = g_strdup(mime_type->c_str());
    } else {
        media->filename = g_strdup(poppler_media->getFileName()->c_str());
    }

    return media;
}

/**
 * poppler_media_get_filename:
 * @poppler_media: a #PopplerMedia
 *
 * Returns the media clip filename, in case of non-embedded media. filename might be
 * a local relative or absolute path or a URI
 *
 * Return value: a filename, return value is owned by #PopplerMedia and should not be freed
 *
 * Since: 0.14
 */
const gchar *poppler_media_get_filename(PopplerMedia *poppler_media)
{
    g_return_val_if_fail(POPPLER_IS_MEDIA(poppler_media), NULL);
    g_return_val_if_fail(!poppler_media->stream.isStream(), NULL);

    return poppler_media->filename;
}

/**
 * poppler_media_is_embedded:
 * @poppler_media: a #PopplerMedia
 *
 * Whether the media clip is embedded in the PDF. If the result is %TRUE, the embedded stream
 * can be saved with poppler_media_save() or poppler_media_save_to_callback() function.
 * If the result is %FALSE, the media clip filename can be retrieved with
 * poppler_media_get_filename() function.
 *
 * Return value: %TRUE if media clip is embedded, %FALSE otherwise
 *
 * Since: 0.14
 */
gboolean poppler_media_is_embedded(PopplerMedia *poppler_media)
{
    g_return_val_if_fail(POPPLER_IS_MEDIA(poppler_media), FALSE);

    return poppler_media->stream.isStream();
}

/**
 * poppler_media_get_mime_type:
 * @poppler_media: a #PopplerMedia
 *
 * Returns the media clip mime-type
 *
 * Return value: the mime-type, return value is owned by #PopplerMedia and should not be freed
 *
 * Since: 0.14
 */
const gchar *poppler_media_get_mime_type(PopplerMedia *poppler_media)
{
    g_return_val_if_fail(POPPLER_IS_MEDIA(poppler_media), NULL);

    return poppler_media->mime_type;
}

static gboolean save_helper(const gchar *buf, gsize count, gpointer data, GError **error)
{
    FILE *f = (FILE *)data;
    gsize n;

    n = fwrite(buf, 1, count, f);
    if (n != count) {
        g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(errno), "Error writing to media file: %s", g_strerror(errno));
        return FALSE;
    }

    return TRUE;
}

/**
 * poppler_media_save:
 * @poppler_media: a #PopplerMedia
 * @filename: name of file to save
 * @error: (allow-none): return location for error, or %NULL.
 *
 * Saves embedded stream of @poppler_media to a file indicated by @filename.
 * If @error is set, %FALSE will be returned.
 * Possible errors include those in the #G_FILE_ERROR domain
 * and whatever the save function generates.
 *
 * Return value: %TRUE, if the file successfully saved
 *
 * Since: 0.14
 */
gboolean poppler_media_save(PopplerMedia *poppler_media, const char *filename, GError **error)
{
    gboolean result;
    FILE *f;

    g_return_val_if_fail(POPPLER_IS_MEDIA(poppler_media), FALSE);
    g_return_val_if_fail(poppler_media->stream.isStream(), FALSE);

    f = openFile(filename, "wb");

    if (f == nullptr) {
        gchar *display_name = g_filename_display_name(filename);
        g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(errno), "Failed to open '%s' for writing: %s", display_name, g_strerror(errno));
        g_free(display_name);
        return FALSE;
    }

    result = poppler_media_save_to_callback(poppler_media, save_helper, f, error);

    if (fclose(f) < 0) {
        gchar *display_name = g_filename_display_name(filename);
        g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(errno), "Failed to close '%s', all data may not have been saved: %s", display_name, g_strerror(errno));
        g_free(display_name);
        return FALSE;
    }

    return result;
}

#define BUF_SIZE 1024

/**
 * poppler_media_save_to_callback:
 * @poppler_media: a #PopplerMedia
 * @save_func: (scope call): a function that is called to save each block of data that the save routine generates.
 * @user_data: user data to pass to the save function.
 * @error: (allow-none): return location for error, or %NULL.
 *
 * Saves embedded stream of @poppler_media by feeding the produced data to @save_func. Can be used
 * when you want to store the media clip stream to something other than a file, such as
 * an in-memory buffer or a socket. If @error is set, %FALSE will be
 * returned. Possible errors include those in the #G_FILE_ERROR domain and
 * whatever the save function generates.
 *
 * Return value: %TRUE, if the save successfully completed
 *
 * Since: 0.14
 */
gboolean poppler_media_save_to_callback(PopplerMedia *poppler_media, PopplerMediaSaveFunc save_func, gpointer user_data, GError **error)
{
    Stream *stream;
    gchar buf[BUF_SIZE];
    int i;
    gboolean eof_reached = FALSE;

    g_return_val_if_fail(POPPLER_IS_MEDIA(poppler_media), FALSE);
    g_return_val_if_fail(poppler_media->stream.isStream(), FALSE);

    stream = poppler_media->stream.getStream();
    stream->reset();

    do {
        int data;

        for (i = 0; i < BUF_SIZE; i++) {
            data = stream->getChar();
            if (data == EOF) {
                eof_reached = TRUE;
                break;
            }
            buf[i] = data;
        }

        if (i > 0) {
            if (!(save_func)(buf, i, user_data, error)) {
                stream->close();
                return FALSE;
            }
        }
    } while (!eof_reached);

    stream->close();

    return TRUE;
}