All files / src/implementation metadata.ts

94.23% Statements 98/104
90% Branches 27/30
93.33% Functions 14/15
94.17% Lines 97/103
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                                            4x 4x 4x 4x 4x   4x 156x           4x           64x         64x 64x 64x   4x         4x   4x 12x 12x 7x   5x 5x       4x 21x 17x   4x 4x 4x 4x 4x     12x   4x 4x 4x           12x 5x   7x     4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x             12x 12x         12x 12x 12x 29x 29x 29x 29x 29x       29x   12x   4x     4x 4x     4x             12x     4x         12x 12x 12x 12x 192x 192x   12x 12x     4x         12x 12x     12x 12x     4x           24x 24x 24x 384x 384x 192x     24x     4x 11x 11x 4x   7x 16x 16x 1x 1x     15x 1x          
/**
 * Copyright 2017 Google Inc.
 *
 * 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.
 */
 
/**
 * @fileoverview Documentation for the metadata format
 */
import { Metadata } from '../metadata';
 
import { AuthWrapper } from './authwrapper';
import * as json from './json';
import { Location } from './location';
import * as path from './path';
import * as type from './type';
import * as UrlUtils from './url';
 
export function noXform_(metadata: Metadata, value: any): any {
  return value;
}
 
/**
 * @struct
 */
export class Mapping {
  local: string;
  writable: boolean;
  xform: (p1: Metadata, p2: any) => any;
 
  constructor(
    public server: string,
    opt_local?: string | null,
    opt_writable?: boolean,
    opt_xform?: (p1: Metadata, p2: any) => any | null
  ) {
    this.local = opt_local || server;
    this.writable = !!opt_writable;
    this.xform = opt_xform || noXform_;
  }
}
type Mappings = Mapping[];
 
export { Mappings };
 
let mappings_: Mappings | null = null;
 
export function xformPath(fullPath: any): string {
  let valid = type.isString(fullPath);
  if (!valid || fullPath.length < 2) {
    return fullPath;
  } else {
    fullPath = fullPath as string;
    return path.lastComponent(fullPath);
  }
}
 
export function getMappings(): Mappings {
  if (mappings_) {
    return mappings_;
  }
  let mappings = [];
  mappings.push(new Mapping('bucket'));
  mappings.push(new Mapping('generation'));
  mappings.push(new Mapping('metageneration'));
  mappings.push(new Mapping('name', 'fullPath', true));
 
  function mappingsXformPath(metadata: Metadata, fullPath: any): string {
    return xformPath(fullPath);
  }
  let nameMapping = new Mapping('name');
  nameMapping.xform = mappingsXformPath;
  mappings.push(nameMapping);
 
  /**
   * Coerces the second param to a number, if it is defined.
   */
  function xformSize(metadata: Metadata, size: any): number | null | undefined {
    if (type.isDef(size)) {
      return +(size as number);
    } else {
      return size;
    }
  }
  let sizeMapping = new Mapping('size');
  sizeMapping.xform = xformSize;
  mappings.push(sizeMapping);
  mappings.push(new Mapping('timeCreated'));
  mappings.push(new Mapping('updated'));
  mappings.push(new Mapping('md5Hash', null, true));
  mappings.push(new Mapping('cacheControl', null, true));
  mappings.push(new Mapping('contentDisposition', null, true));
  mappings.push(new Mapping('contentEncoding', null, true));
  mappings.push(new Mapping('contentLanguage', null, true));
  mappings.push(new Mapping('contentType', null, true));
  mappings.push(new Mapping('metadata', 'customMetadata', true));
 
  /**
   * Transforms a comma-separated string of tokens into a list of download
   * URLs.
   */
  function xformTokens(metadata: Metadata, tokens: any): string[] {
    let valid = type.isString(tokens) && tokens.length > 0;
    Iif (!valid) {
      // This can happen if objects are uploaded through GCS and retrieved
      // through list, so we don't want to throw an Error.
      return [];
    }
    let encode = encodeURIComponent;
    let tokensList = tokens.split(',');
    let urls = tokensList.map(function(token: string) {
      let bucket: string = metadata['bucket'] as string;
      let path: string = metadata['fullPath'] as string;
      let urlPart = '/b/' + encode(bucket) + '/o/' + encode(path);
      let base = UrlUtils.makeDownloadUrl(urlPart);
      let queryString = UrlUtils.makeQueryString({
        alt: 'media',
        token: token
      });
      return base + queryString;
    });
    return urls;
  }
  mappings.push(
    new Mapping('downloadTokens', 'downloadURLs', false, xformTokens)
  );
  mappings_ = mappings;
  return mappings_;
}
 
export function addRef(metadata: Metadata, authWrapper: AuthWrapper) {
  function generateRef() {
    let bucket: string = metadata['bucket'] as string;
    let path: string = metadata['fullPath'] as string;
    let loc = new Location(bucket, path);
    return authWrapper.makeStorageReference(loc);
  }
  Object.defineProperty(metadata, 'ref', { get: generateRef });
}
 
export function fromResource(
  authWrapper: AuthWrapper,
  resource: { [name: string]: any },
  mappings: Mappings
): Metadata {
  let metadata: Metadata = {} as Metadata;
  metadata['type'] = 'file';
  let len = mappings.length;
  for (let i = 0; i < len; i++) {
    let mapping = mappings[i];
    metadata[mapping.local] = mapping.xform(metadata, resource[mapping.server]);
  }
  addRef(metadata, authWrapper);
  return metadata;
}
 
export function fromResourceString(
  authWrapper: AuthWrapper,
  resourceString: string,
  mappings: Mappings
): Metadata | null {
  let obj = json.jsonObjectOrNull(resourceString);
  Iif (obj === null) {
    return null;
  }
  let resource = obj as Metadata;
  return fromResource(authWrapper, resource, mappings);
}
 
export function toResourceString(
  metadata: Metadata,
  mappings: Mappings
): string {
  let resource: {
    [prop: string]: any;
  } = {};
  let len = mappings.length;
  for (let i = 0; i < len; i++) {
    let mapping = mappings[i];
    if (mapping.writable) {
      resource[mapping.server] = metadata[mapping.local];
    }
  }
  return JSON.stringify(resource);
}
 
export function metadataValidator(p: any) {
  let validType = p && type.isObject(p);
  if (!validType) {
    throw 'Expected Metadata object.';
  }
  for (let key in p) {
    let val = p[key];
    if (key === 'customMetadata') {
      Eif (!type.isObject(val)) {
        throw 'Expected object for \'customMetadata\' mapping.';
      }
    } else {
      if (type.isNonNullObject(val)) {
        throw "Mapping for '" + key + "' cannot be an object.";
      }
    }
  }
}