summaryrefslogtreecommitdiff
path: root/src/librygel-server/rygel-http-thumbnail-handler.vala
blob: 579c484e48fc3e01bafc795a5e57f4f957041658 (plain)
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
/*
 * Copyright (C) 2008, 2009 Nokia Corporation.
 * Copyright (C) 2012 Intel Corporation.
 * Copyright (C) 2013  Cable Television Laboratories, Inc.
 *
 * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
 *                               <zeeshan.ali@nokia.com>
 *         Jens Georg <jensg@openismus.com>
 *         Craig Pratt <craig@ecaspia.com>
 *
 * This file is part of Rygel.
 *
 * Rygel is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Rygel 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

using GUPnP;

internal class Rygel.HTTPThumbnailHandler : Rygel.HTTPGetHandler {
    private MediaFileItem media_item;
    private int thumbnail_index;
    private Thumbnail thumbnail;

    public HTTPThumbnailHandler (MediaFileItem media_item,
                                 int thumbnail_index,
                                 Cancellable? cancellable)
                                 throws HTTPRequestError {

        this.media_item = media_item;
        this.thumbnail_index = thumbnail_index;
        this.cancellable = cancellable;

        if (media_item is MusicItem) {
            var music_item = media_item as MusicItem;
            this.thumbnail = music_item.album_art;
        } else if (media_item is VisualItem) {
            var visual_item = media_item as VisualItem;
            if (thumbnail_index < visual_item.thumbnails.size) {
                this.thumbnail = visual_item.thumbnails.get (thumbnail_index);
            }
        }

        if (this.thumbnail == null) {
            var msg = ("Thumbnail index %d not found for item '%s");
            throw new HTTPRequestError.NOT_FOUND (msg,
                                                  thumbnail_index,
                                                  media_item.id);
        }
    }

    public override bool supports_transfer_mode (string mode) {
        // Support interactive and background transfers only
        return (mode != TRANSFER_MODE_STREAMING);
    }

    public override void add_response_headers (HTTPGet request)
                                               throws HTTPRequestError {
        // Add Content-Type
        request.msg.response_headers.append ("Content-Type",
                                             thumbnail.mime_type);

        // Add contentFeatures.dlna.org
        var res = this.thumbnail.get_resource
                                        (request.http_server.get_protocol (),
                                         this.thumbnail_index);
        var protocol_info = res.get_protocol_info ().to_string ();
        var pi_fields = protocol_info.split (":", 4);
        request.msg.response_headers.append ("contentFeatures.dlna.org",
                                             pi_fields[3]);

        // Chain-up
        base.add_response_headers (request);
    }

    public override HTTPResponse render_body (HTTPGet request)
                                              throws HTTPRequestError {
        DataSource src;
        try {
            var engine = MediaEngine.get_default ();
            src = engine.create_data_source_for_uri (this.thumbnail.uri);

            return new HTTPResponse (request, this, src);
        } catch (Error err) {
            throw new HTTPRequestError.NOT_FOUND (err.message);
        }
    }

    public override int64 get_resource_size () {
        return thumbnail.size;
    }

    public override bool supports_byte_seek () {
        return true;
    }
}