Code coverage report for lib\services\file\models\fileresult.js

Statements: 100% (60 / 60)      Branches: 87.5% (21 / 24)      Functions: 100% (9 / 9)      Lines: 100% (60 / 60)      Ignored: none     

All files » lib/services/file/models/ » fileresult.js
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                                  1   1 1 1 1   1 165 165 165     1 5 5 10 5 5 5 5       5       5     1                                       1 160 160 1110 586       160 160 160   160 42 630 630 630                 1 60 120 4       60   60     60     60 38   38 37     38                 1 74 518 39       74   74     74     74     74     74     74     74   74 2         1
// 
// Copyright (c) Microsoft and contributors.  All rights reserved.
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//   http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// 
// See the License for the specific language governing permissions and
// limitations under the License.
// 
 
// Module dependencies.
var _ = require('underscore');
 
var azureCommon = require('./../../../common/common');
var azureutil = azureCommon.util;
var Constants = azureCommon.Constants;
var HeaderConstants = Constants.HeaderConstants;
 
function FileResult(share, directory, name) {
  this.share = share;
  this.directory = directory;
  this.name = name;
}
 
FileResult.parse = function (entryXml) {
  var listResult = new FileResult();
  for (var propertyName in entryXml) {
    if (propertyName === 'Properties') {
      listResult[propertyName.toLowerCase()] = { };
      for (var subPropertyName in entryXml[propertyName]) {
        Eif (entryXml[propertyName].hasOwnProperty(subPropertyName)) {
          listResult[propertyName.toLowerCase()][subPropertyName.toLowerCase()] = entryXml[propertyName][subPropertyName];
        }
      }
    } else {
      listResult[propertyName.toLowerCase()] = entryXml[propertyName];
    }
  }
 
  return listResult;
};
 
var responseHeaders = {
  'acceptRanges': 'ACCEPT_RANGES',
 
  'contentType': 'CONTENT_TYPE',
  'contentEncoding': 'CONTENT_ENCODING',
  'contentLanguage': 'CONTENT_LANGUAGE',
  'contentMD5': 'CONTENT_MD5',
  'cacheControl': 'CACHE_CONTROL',
  'contentRange': 'CONTENT_RANGE',
  'contentLength': 'CONTENT_LENGTH',
  'contentDisposition': 'CONTENT_DISPOSITION',
  
  'copySource': 'COPY_SOURCE',
  'copyStatus': 'COPY_STATUS',
  'copyCompletionTime': 'COPY_COMPLETION_TIME',
  'copyStatusDescription': 'COPY_STATUS_DESCRIPTION',
  'copyId': 'COPY_ID',
  'copyProgress': 'COPY_PROGRESS'
};
 
FileResult.prototype.getPropertiesFromHeaders = function (headers, content) {
  var self = this;
  var setFilePropertyFromHeaders = function (fileProperty, headerProperty) {
    if (!self[fileProperty] && headers[headerProperty.toLowerCase()]) {
      self[fileProperty] = headers[headerProperty.toLowerCase()];
    }
  };
 
  setFilePropertyFromHeaders('etag', HeaderConstants.ETAG);
  setFilePropertyFromHeaders('lastModified', HeaderConstants.LAST_MODIFIED);
  setFilePropertyFromHeaders('requestId', HeaderConstants.REQUEST_ID);   
 
  if (content) {
     _.chain(responseHeaders).pairs().each(function (pair) {
      var property = pair[0];
      var header = HeaderConstants[pair[1]];
      setFilePropertyFromHeaders(property, header);
    });   
   } 
};
 
/**
* This method sets the HTTP headers and is used by all methods except setFileProperties and createFile. 
* Those methods will set the x-ms-* headers using setProperties.
*/
FileResult.setHeaders = function (webResource, options) {
  var setHeaderProperty = function (headerProperty, fileProperty) {
    if (options[fileProperty]) {
      webResource.withHeader(headerProperty, options[fileProperty]);
    }
  };
 
  Eif (options) {
    // Content-MD5
    setHeaderProperty(HeaderConstants.CONTENT_MD5, 'contentMD5');
 
    // Content-Length
    setHeaderProperty(HeaderConstants.CONTENT_LENGTH, 'contentLength');
 
    // Range
    if (!azureutil.objectIsNull(options.rangeStart)) {
      var range = 'bytes=' + options.rangeStart + '-';
 
      if (!azureutil.objectIsNull(options.rangeEnd)) {
        range += options.rangeEnd;
      }
 
      webResource.withHeader(HeaderConstants.STORAGE_RANGE, range);
    }   
  }
};
 
/**
* This method sets the x-ms-* headers and is used by setFileProperties and createFile. 
* All other methods will set the regular HTTP headers using setHeaders.
*/
FileResult.setProperties = function (webResource, options) {
  var setHeaderProperty = function (headerProperty, fileProperty) {
    if (options[fileProperty]) {
      webResource.withHeader(headerProperty, options[fileProperty]);
    }
  };
 
  Eif (options) {
    // Content-Type
    setHeaderProperty(HeaderConstants.FILE_CONTENT_TYPE, 'contentType');
 
    // Content-Encoding
    setHeaderProperty(HeaderConstants.FILE_CONTENT_ENCODING, 'contentEncoding');
 
    // Content-MD5
    setHeaderProperty(HeaderConstants.FILE_CONTENT_MD5, 'contentMD5');
 
    // Content-Language
    setHeaderProperty(HeaderConstants.FILE_CONTENT_LANGUAGE, 'contentLanguage');
 
    // Content-Disposition
    setHeaderProperty(HeaderConstants.FILE_CONTENT_DISPOSITION, 'contentDisposition');
 
    // Cache-Control
    setHeaderProperty(HeaderConstants.FILE_CACHE_CONTROL, 'cacheControl');
 
    // Content-Length
    setHeaderProperty(HeaderConstants.FILE_CONTENT_LENGTH, 'contentLength');
 
    if (options.metadata) {
      webResource.addOptionalMetadataHeaders(options.metadata);
    }
  }
};
 
module.exports = FileResult;