summaryrefslogtreecommitdiff
path: root/src/librygel-server/rygel-import-resource.vala
blob: ff619c9f538f29d0ef24913fb5b35da1f1a8d83c (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
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
/*
 * Copyright (C) 2008-2010 Nokia Corporation.
 *
 * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
 *                               <zeeshan.ali@nokia.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;
using Soup;

internal enum Rygel.TransferStatus {
    COMPLETED,
    ERROR,
    IN_PROGRESS,
    STOPPED
}

/**
 * Responsible for handling ImportResource action.
 */
internal class Rygel.ImportResource : GLib.Object, Rygel.StateMachine {
    private static uint32 last_transfer_id = 0;

    // In arguments
    public string source_uri;
    public string destination_uri;

    // Out arguments
    public uint32 transfer_id;

    public TransferStatus status;
    public int64 bytes_copied;
    public int64 bytes_total;

    private MediaFileItem item;
    private Session session;

    public string status_as_string {
        get {
            switch (this.status) {
                case TransferStatus.COMPLETED:
                    return "COMPLETED";
                case TransferStatus.ERROR:
                    return "ERROR";
                case TransferStatus.IN_PROGRESS:
                    return "IN_PROGRESS";
                case TransferStatus.STOPPED:
                default:
                    return "STOPPED";
            }
        }
    }

    public Cancellable cancellable { get; set; }

    private HTTPServer http_server;
    private MediaContainer root_container;
    private ServiceAction action;
    private FileOutputStream output_stream;

    public ImportResource (ContentDirectory    content_dir,
                           owned ServiceAction action) {
        this.root_container = content_dir.root_container;
        this.http_server = content_dir.http_server;
        this.cancellable = new Cancellable ();
        this.action = (owned) action;

        last_transfer_id++;
        this.transfer_id = last_transfer_id;

        this.bytes_copied = 0;
        this.bytes_total = 0;

        this.status = TransferStatus.IN_PROGRESS;
        this.session = new Session ();

        content_dir.cancellable.cancelled.connect (() => {
            this.cancellable.cancel ();
        });
    }

    public async void run () {
        // Start by parsing the 'in' arguments
        this.action.get ("SourceURI",
                            typeof (string),
                            out this.source_uri,
                         "DestinationURI",
                            typeof (string),
                            out this.destination_uri);

        try {
            if (this.source_uri == null) {
                throw new ContentDirectoryError.INVALID_ARGS
                                        ("Must provide source URI");
            }

            if (this.destination_uri == null) {
                throw new ContentDirectoryError.NO_SUCH_DESTINATION_RESOURCE
                                        ("Must provide destination URI");
            }

            // Set action return arguments
            this.action.set ("TransferID", typeof (uint32), this.transfer_id);

            this.item = yield this.fetch_item ();
        } catch (Error error) {
            warning (_("Failed to get original URI for “%s”: %s"),
                     this.destination_uri,
                     error.message);

            this.action.return_error (error.code, error.message);
            this.status = TransferStatus.ERROR;
            this.completed ();

            return;
        }

        var queue = ObjectRemovalQueue.get_default ();
        queue.dequeue (this.item);

        try {
            var source_file = File.new_for_uri (this.item.get_primary_uri ());
            this.output_stream = yield source_file.replace_async
                                        (null,
                                         false,
                                         FileCreateFlags.PRIVATE,
                                         Priority.DEFAULT,
                                         this.cancellable);
            var message = new Message ("GET", source_uri);

            debug ("Importing resource from %s to %s",
                   source_uri,
                   this.item.get_primary_uri ());

            var stream = yield this.session.send_async (message, Priority.DEFAULT, this.cancellable);
            if (!(message.status_code >= 200 && message.status_code <= 299)) {
                handle_transfer_error (message);

                this.completed();
                return;
            }

            this.bytes_total = message.response_headers.get_content_length ();
            this.action.return_success ();
            this.action = null;
            this.bytes_copied = yield this.output_stream.splice_async (stream,
                                                           OutputStreamSpliceFlags.CLOSE_SOURCE |
                                                           OutputStreamSpliceFlags.CLOSE_TARGET,
                                                           Priority.DEFAULT,
                                                           this.cancellable);

            if (this.bytes_total == 0) {
                this.bytes_total = this.bytes_copied;
            } else if (this.bytes_total != this.bytes_copied) {
                warning ("Importing sizes did not match: %z vs. %z", this.bytes_total, this.bytes_copied);
                this.status = TransferStatus.ERROR;
            }

            if (this.status == TransferStatus.IN_PROGRESS) {
                this.status = TransferStatus.COMPLETED;
            }
        } catch (Error err) {
            warning ("%s", err.message);
            if (err is IOError.CANCELLED) {
                this.status = TransferStatus.STOPPED;
            } else {
                this.status = TransferStatus.ERROR;
            }

            yield queue.remove_now (this.item, this.cancellable);
        }

        this.completed ();
    }

    private async MediaFileItem fetch_item () throws Error {
        HTTPItemURI uri;
        try {
            uri = new HTTPItemURI.from_string (this.destination_uri,
                                               this.http_server);
        } catch (Error error) {
            throw new ContentDirectoryError.NO_SUCH_DESTINATION_RESOURCE
                                            (error.message);
        }
        var media_object = yield this.root_container.find_object (uri.item_id,
                                                                  null);
        string msg = null;

        var media_item = media_object as MediaFileItem;

        if (media_object == null ||
            media_item == null ||
            !(media_object.parent is WritableContainer)) {
            msg = _("URI “%s” invalid for importing contents to").printf
                                        (this.destination_uri);
        }

        else if (!media_item.place_holder) {
            msg = _("Pushing data to non-empty item “%s” not allowed").printf
                                        (media_object.id);
        } else if (media_object.get_uris ().is_empty) {
            assert_not_reached ();
        }

        if (msg != null) {
            throw new ContentDirectoryError.INVALID_ARGS (msg);
        }

        return media_item;
    }

    private void handle_transfer_error (Message message) {
        this.status = TransferStatus.ERROR;
        try {
            this.output_stream.close (this.cancellable);
            var file = File.new_for_uri (this.item.get_primary_uri ());
            file.delete (this.cancellable);
        } catch (Error error) {};

        var phrase = Status.get_phrase (message.status_code);
        warning (_("Failed to import file from %s: %s"),
                 this.source_uri,
                 phrase);

        if (action == null) {
            return;
        }

        if (message.status_code == Soup.Status.NOT_FOUND ||
            message.status_code < 100) {
            this.action.return_error (714, phrase);
        } else {
            this.action.return_error (715, phrase);
        }
    }
}