summaryrefslogtreecommitdiff
path: root/src/plugins/media-export/rygel-media-export-extractor.vala
blob: 93d5fe9cab9e06ba3dbca30625805d844d291c28 (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
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
/*
 * Copyright (C) 2016 Jens Georg <mail@jensge.org>
 *
 * Author: Jens Georg <mail@jensge.org>
 *
 * 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
 */

public errordomain ExtractorError {
    GENERAL,
    INVALID
}

public class Rygel.MediaExport.Extractor : Object {
    private const string INVALID_CHARS = "()[]<>{}!@#$^&*+=|\\/\"'?~";
    private const string CONVERT_CHARS = "\t_\\.";
    private const string BLOCK_PATTERN = "%s[^%s]*%s";
    private const string[] BLOCKS = { "()", "{}", "[]", "<>" };
    private const string[] BLACKLIST = {
        "720p", "1080p", "x264", "ws", "proper", "real.repack", "repack",
        "hdtv", "pdtv", "notv", "dsr", "DVDRip", "divx", "xvid"
    };

    private const string[] VIDEO_SUFFIXES = {
        "webm", "mkv", "flv", "ogv", "ogg", "avi", "mov", "wmv", "mp4",
        "m4v", "mpeg", "mpg", "iso", "mp3", "m4a", "png", "jpg", "jpeg",
        "ogv", "oga", "3gp",
        // DIDL playlists
        "xml"
    };

    private static Regex char_remove_regex;
    private static Regex char_convert_regex;
    private static Regex space_compress_regex;
    private static Regex[] block_regexes;
    private static Regex[] blacklist_regexes;
    private static Regex[] video_suffix_regexes;

    public File file { get; construct set; }
    public bool extract_metadata { get; construct set; default = true; }

    protected VariantDict serialized_info;

    /**
     * Factory method for creating specific extractors depending on the
     * content type of the file
     */
    public static Extractor create_for_file (File   file,
                                             string content_type,
                                             bool   extract_metadata) {
        if (!extract_metadata) {
            return new Extractor (file, false);
        }

        var is_text = content_type.has_prefix ("text/") ||
                      content_type.has_suffix ("xml");
        if (content_type == "application/x-cd-image") {
            return new DVDParser (file);
        }

        if (is_text) {
            return new PlaylistExtractor (file);
        }

        if (content_type == "image/jpeg" ||
            content_type == "image/png") {
            return new ImageExtractor (file);
        }

        return new GenericExtractor (file);
    }

    private Extractor (File file, bool extract_metadata) {
        Object (file: file, extract_metadata: extract_metadata);
    }

    public override void constructed () {
        this.serialized_info = new VariantDict ();
    }

    public virtual async void run () throws Error {
        var file_info = yield file.query_info_async (FileAttribute.STANDARD_TYPE + "," +
                                                     FileAttribute.STANDARD_CONTENT_TYPE + "," +
                                                     FileAttribute.STANDARD_SIZE + "," +
                                                     FileAttribute.TIME_MODIFIED + "," +
                                                     FileAttribute.STANDARD_DISPLAY_NAME,
                                                     FileQueryInfoFlags.NONE);
        var display_name = file_info.get_display_name ();

        if (extract_metadata) {
            var title = this.strip_invalid_entities (display_name);
            this.serialized_info.insert (Serializer.TITLE, "s", title);
        } else {
            this.serialized_info.insert (Serializer.TITLE, "s", display_name);
        }

        var mtime = file_info.get_attribute_uint64 (FileAttribute.TIME_MODIFIED);
        this.serialized_info.insert (Serializer.MODIFIED, "t", mtime);

        var dt = new DateTime.from_unix_utc ((int64) mtime);
        var date = "%sZ".printf (dt.format ("%Y-%m-%dT%H:%M:%S"));
        this.serialized_info.insert (Serializer.DATE, "s", date);

        var content_type = ContentType.get_mime_type
                                        (file_info.get_content_type ());


        if (!extract_metadata) {
            if (content_type.has_prefix ("video/")) {
                this.serialized_info.insert (Serializer.UPNP_CLASS, "s", UPNP_CLASS_VIDEO);
            } else if (content_type.has_prefix ("image/")) {
                this.serialized_info.insert (Serializer.UPNP_CLASS, "s", UPNP_CLASS_PHOTO);
            } else if (content_type.has_prefix ("audio/") || content_type == "application/ogg") {
                this.serialized_info.insert (Serializer.UPNP_CLASS, "s", UPNP_CLASS_MUSIC);
            } else { // application/xml or text/xml
                // Do nothing. Should at least try to parse a DIDL_S playlist here?
            }
        }

        this.serialized_info.insert (Serializer.MIME_TYPE, "s", content_type);
        this.serialized_info.insert (Serializer.SIZE, "t", file_info.get_size ());
        var id = Checksum.compute_for_string (ChecksumType.MD5,
                                              file.get_uri ());
        this.serialized_info.insert (Serializer.ID, "s", id);
        this.serialized_info.insert (Serializer.URI, "s", file.get_uri ());
     }

    public new Variant? @get () {
        // If the date has a timezone offset, make sure it contains a
        // colon bgo#702231, DLNA 7.3.21.1
        var date  = this.serialized_info.lookup_value (Serializer.DATE,
                                                       VariantType.STRING);
        if ("T" in date.get_string ()) {
            var fixed_date = new Soup.Date.from_string (date.get_string ());
            var new_date = fixed_date.to_string (Soup.DateFormat.ISO8601_FULL);

            this.serialized_info.insert (Serializer.DATE, "s", new_date);
        }

        return this.serialized_info.end ();
    }

    private string strip_invalid_entities (string original) {
        if (char_remove_regex == null) {
            try {
                var regex_string = Regex.escape_string (INVALID_CHARS);
                char_remove_regex = new Regex ("[%s]".printf (regex_string));
                regex_string = Regex.escape_string (CONVERT_CHARS);
                char_convert_regex = new Regex ("[%s]".printf (regex_string));
                space_compress_regex = new Regex ("\\s+");
                block_regexes = new Regex[0];

                foreach (var block in BLOCKS) {
                    var block_re = BLOCK_PATTERN.printf (
                                      Regex.escape_string ("%C".printf (block[0])),
                                      Regex.escape_string ("%C".printf (block[1])),
                                      Regex.escape_string ("%C".printf (block[1])));
                    block_regexes += new Regex (block_re);
                }

                foreach (var blacklist in BLACKLIST) {
                    blacklist_regexes += new Regex (Regex.escape_string
                                                    (blacklist));
                }

                foreach (var suffix in VIDEO_SUFFIXES) {
                    var regex = new Regex (Regex.escape_string (suffix),
                                           RegexCompileFlags.CASELESS);

                    video_suffix_regexes += regex;
                }
            } catch (RegexError error) {
                assert_not_reached ();
            }
        }

        string p;

        p = original;

        try {
            foreach (var re in blacklist_regexes) {
                p = re.replace_literal (p, -1, 0, "");
            }

            foreach (var re in video_suffix_regexes) {
                p = re.replace_literal (p, -1, 0, "");
            }

            foreach (var re in block_regexes) {
                p = re.replace_literal (p, -1, 0, "");
            }

            p = char_remove_regex.replace_literal (p, -1, 0, "");
            p = char_convert_regex.replace_literal (p, -1, 0, " ");
            p = space_compress_regex.replace_literal (p, -1, 0, " ");

            p._strip ();

            return p;
        } catch (RegexError error) {
            assert_not_reached ();
        }
    }
}