File.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. * Constructor.
  23. * name {DOMString} name of the file, without path information
  24. * fullPath {DOMString} the full path of the file, including the name
  25. * type {DOMString} mime type
  26. * lastModifiedDate {Date} last modified date
  27. * size {Number} size of the file in bytes
  28. */
  29. var File = function(name, localURL, type, lastModifiedDate, size){
  30. this.name = name || '';
  31. this.localURL = localURL || null;
  32. this.type = type || null;
  33. this.lastModified = lastModifiedDate || null;
  34. // For backwards compatibility, store the timestamp in lastModifiedDate as well
  35. this.lastModifiedDate = lastModifiedDate || null;
  36. this.size = size || 0;
  37. // These store the absolute start and end for slicing the file.
  38. this.start = 0;
  39. this.end = this.size;
  40. };
  41. /**
  42. * Returns a "slice" of the file. Since Cordova Files don't contain the actual
  43. * content, this really returns a File with adjusted start and end.
  44. * Slices of slices are supported.
  45. * start {Number} The index at which to start the slice (inclusive).
  46. * end {Number} The index at which to end the slice (exclusive).
  47. */
  48. File.prototype.slice = function(start, end) {
  49. var size = this.end - this.start;
  50. var newStart = 0;
  51. var newEnd = size;
  52. if (arguments.length) {
  53. if (start < 0) {
  54. newStart = Math.max(size + start, 0);
  55. } else {
  56. newStart = Math.min(size, start);
  57. }
  58. }
  59. if (arguments.length >= 2) {
  60. if (end < 0) {
  61. newEnd = Math.max(size + end, 0);
  62. } else {
  63. newEnd = Math.min(end, size);
  64. }
  65. }
  66. var newFile = new File(this.name, this.localURL, this.type, this.lastModified, this.size);
  67. newFile.start = this.start + newStart;
  68. newFile.end = this.start + newEnd;
  69. return newFile;
  70. };
  71. module.exports = File;