~ubuntu-branches/debian/stretch/downthemall/stretch

« back to all changes in this revision

Viewing changes to modules/manager/decompressor.js

  • Committer: Package Import Robot
  • Author(s): Michael Meskes
  • Date: 2016-09-21 13:33:55 UTC
  • mto: This revision was merged to the branch mainline in revision 11.
  • Revision ID: package-import@ubuntu.com-20160921133355-ylst3mzzo82mghyw
Tags: upstream-3.0.6
ImportĀ upstreamĀ versionĀ 3.0.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This Source Code Form is subject to the terms of the Mozilla Public
 
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 
3
 * You can obtain one at http://mozilla.org/MPL/2.0/ */
 
4
"use strict";
 
5
 
 
6
const BUFFER_SIZE = 5 * 1024 * 1024;
 
7
const FREQ = 250;
 
8
 
 
9
const DTA = require("api");
 
10
const {TimerManager} = require("support/timers");
 
11
const Prefs = require("preferences");
 
12
 
 
13
const Timers = new TimerManager();
 
14
 
 
15
function Decompressor(download, callback) {
 
16
        this.download = download;
 
17
        this.callback = callback;
 
18
        this.to = download.destinationLocalFile.clone();
 
19
        this.from = download.tmpFile.clone();
 
20
 
 
21
        try {
 
22
                this._outStream = new Instances.FileOutputStream(this.to, 0x04 | 0x08, Prefs.getExt('permissions', 384), 0);
 
23
                this.outStream = new Instances.BinaryOutputStream(
 
24
                        new Instances.BufferedOutputStream(this._outStream, BUFFER_SIZE));
 
25
 
 
26
                let converter = Cc["@mozilla.org/streamconv;1?from=" + download.compression + "&to=uncompressed"]
 
27
                        .createInstance(Ci.nsIStreamConverter);
 
28
 
 
29
                converter.asyncConvertData(
 
30
                        download.compression,
 
31
                        "uncompressed",
 
32
                        this,
 
33
                        null
 
34
                );
 
35
 
 
36
                let chan = Services.oldio.newChannelFromURI(Services.io.newFileURI(this.from));
 
37
                chan.asyncOpen(converter, null);
 
38
        }
 
39
        catch (ex) {
 
40
                try {
 
41
                        if (this.outStream) {
 
42
                                this.outStream.close();
 
43
                        }
 
44
                        if (this.to.exists()) {
 
45
                                this.to.remove(false);
 
46
                        }
 
47
                        if (this.from.exists()) {
 
48
                                this.from.remove(false);
 
49
                        }
 
50
                }
 
51
                catch (exx) {
 
52
                        // XXX: what now?
 
53
                }
 
54
                log(LOG_ERROR, "err. :p", ex);
 
55
                callback.call(download, ex);
 
56
        }
 
57
}
 
58
Decompressor.prototype = {
 
59
        exception: null,
 
60
        QueryInterface: function(iid) {
 
61
                if (iid.equals(Ci.nsISupports) || iid.equals(Ci.nsIStreamListener) || iid.equals(Ci.nsIRequestObserver)) {
 
62
                        return this;
 
63
                }
 
64
                throw Cr.NS_ERROR_NO_INTERFACE;
 
65
        },
 
66
        onStartRequest: function(r, c) {
 
67
                this._timer = Timers.createRepeating(FREQ, this.download.invalidate, this.download);
 
68
        },
 
69
        onStopRequest: function(request, c) {
 
70
                Timers.killTimer(this._timer);
 
71
                // important, or else we don't write out the last buffer and truncate too early. :p
 
72
                this.outStream.flush();
 
73
                try {
 
74
                        this._outStream.QueryInterface(Ci.nsISeekableStream).setEOF();
 
75
                }
 
76
                catch (ex) {
 
77
                        this.exception = ex;
 
78
                }
 
79
                try {
 
80
                        this.outStream.close();
 
81
                        this._outStream.close();
 
82
                }
 
83
                catch (ex) {
 
84
                        // huh?
 
85
                        log(LOG_ERROR, "Decompressor: close streams", ex);
 
86
                }
 
87
                if (this.exception) {
 
88
                        try {
 
89
                                this.to.remove(false);
 
90
                        }
 
91
                        catch (ex) {
 
92
                                // no-op: we're already bad :p
 
93
                        }
 
94
                }
 
95
                try {
 
96
                        this.from.remove(false);
 
97
                }
 
98
                catch (ex) {
 
99
                        log(LOG_ERROR, "Failed to remove tmpFile", ex);
 
100
                }
 
101
                this.callback.call(this.download, this.exception);
 
102
        },
 
103
        onDataAvailable: function(request, c, stream, offset, count) {
 
104
                try {
 
105
                        var binStream = new Instances.BinaryInputStream(stream);
 
106
                        if (count !== this.outStream.write(binStream.readBytes(count), count)) {
 
107
                                throw new Exception("Failed to write!");
 
108
                        }
 
109
                        this.download.partialSize = offset;
 
110
                }
 
111
                catch (ex) {
 
112
                        this.exception = ex;
 
113
                        var reason = 0x804b0002; // NS_BINDING_ABORTED;
 
114
                        request.cancel(reason);
 
115
                }
 
116
        }
 
117
};
 
118
 
 
119
exports.Decompressor = Decompressor;