arnaucube
arnaucube/openEventsPlatformAppPublic Self-hosted repository
openEventsPlatformApp/plugins/cordova-plugin-file/www/File.js
plugins/cordova-plugin-file/www/File.jsRaw
 1/*
 2 *
 3 * Licensed to the Apache Software Foundation (ASF) under one
 4 * or more contributor license agreements.  See the NOTICE file
 5 * distributed with this work for additional information
 6 * regarding copyright ownership.  The ASF licenses this file
 7 * to you under the Apache License, Version 2.0 (the
 8 * "License"); you may not use this file except in compliance
 9 * with the License.  You may obtain a copy of the License at
10 *
11 *   http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied.  See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20*/
21
22/**
23 * Constructor.
24 * name {DOMString} name of the file, without path information
25 * fullPath {DOMString} the full path of the file, including the name
26 * type {DOMString} mime type
27 * lastModifiedDate {Date} last modified date
28 * size {Number} size of the file in bytes
29 */
30
31var File = function(name, localURL, type, lastModifiedDate, size){
32    this.name = name || '';
33    this.localURL = localURL || null;
34    this.type = type || null;
35    this.lastModified = lastModifiedDate || null;
36    // For backwards compatibility, store the timestamp in lastModifiedDate as well
37    this.lastModifiedDate = lastModifiedDate || null;
38    this.size = size || 0;
39
40    // These store the absolute start and end for slicing the file.
41    this.start = 0;
42    this.end = this.size;
43};
44
45/**
46 * Returns a "slice" of the file. Since Cordova Files don't contain the actual
47 * content, this really returns a File with adjusted start and end.
48 * Slices of slices are supported.
49 * start {Number} The index at which to start the slice (inclusive).
50 * end {Number} The index at which to end the slice (exclusive).
51 */
52File.prototype.slice = function(start, end) {
53    var size = this.end - this.start;
54    var newStart = 0;
55    var newEnd = size;
56    if (arguments.length) {
57        if (start < 0) {
58            newStart = Math.max(size + start, 0);
59        } else {
60            newStart = Math.min(size, start);
61        }
62    }
63
64    if (arguments.length >= 2) {
65        if (end < 0) {
66            newEnd = Math.max(size + end, 0);
67        } else {
68            newEnd = Math.min(end, size);
69        }
70    }
71
72    var newFile = new File(this.name, this.localURL, this.type, this.lastModified, this.size);
73    newFile.start = this.start + newStart;
74    newFile.end = this.start + newEnd;
75    return newFile;
76};
77
78
79module.exports = File;