Source: storage/metadata/index.js

import { title } from 'cpb-common';
import { StorageInstance as storage } from '../shared.js';

/**
 * @module cpb-storage/metadata
 */
/**
 * sets the custom file or dir metadata
 * @method module:cpb-storage/metadata.set
 * @async
 * @param {string} bucket - gcs storage bucket name
 * @param {string} file - file or dir name
 * @param {object} metadata - custom metadata object
 * @returns {Promise<Object>} meta.metadata - new custom metadata
 */
async function SetMetadata({ bucket, file, metadata }) {
  if (!bucket) throw new TypeError('!bucket');
  // if (!file) throw new TypeError('!file');
  if (!metadata) throw new TypeError('!metadata');
  title('SetMetadata', { bucket, file, metadata });

  const api = file ? storage.bucket(bucket).file(file) : storage.bucket(bucket);

  async function setMetadata() {
    const [meta] = await api.setMetadata({ metadata });
    return meta.metadata;
  }

  return await setMetadata().catch(console.error);
}

/**
 * gets custom metadata for the bucket or file/dir in the bucket
 * @method module:cpb-storage/metadata.get
 * @async
 * @param {string} bucket - gcs storage bucket name
 * @param {string} file - file or dir name
 * @returns {Promise<Object>} metadata.metadata - file or dir metadata
 */
async function GetMetadata({ bucket, file }) {
  if (!bucket) throw new TypeError('!bucket');
  // if (!file) throw new TypeError('!file');
  title('GetMetadata', { bucket, file });
  const api = file ? storage.bucket(bucket).file(file) : storage.bucket(bucket);

  async function getMetadata() {
    const [metadata] = await api.getMetadata();

    // console.log(`Bucket: ${metadata.bucket}`);
    // console.log(`CacheControl: ${metadata.cacheControl}`);
    // console.log(`ComponentCount: ${metadata.componentCount}`);
    // console.log(`ContentDisposition: ${metadata.contentDisposition}`);
    // console.log(`ContentEncoding: ${metadata.contentEncoding}`);
    // console.log(`ContentLanguage: ${metadata.contentLanguage}`);
    // console.log(`ContentType: ${metadata.contentType}`);
    // console.log(`CustomTime: ${metadata.customTime}`);
    console.log(`Crc32c: ${metadata.crc32c}`);
    console.log(`ETag: ${metadata.etag}`);
    console.log(`Generation: ${metadata.generation}`);
    console.log(`Id: ${metadata.id}`);
    // console.log(`KmsKeyName: ${metadata.kmsKeyName}`);
    console.log(`Md5Hash: ${metadata.md5Hash}`);
    // console.log(`MediaLink: ${metadata.mediaLink}`);
    console.log(`Metageneration: ${metadata.metageneration}`);
    console.log(`Name: ${metadata.name}`);
    console.log(`Size: ${metadata.size}`);
    // console.log(`StorageClass: ${metadata.storageClass}`);
    console.log(`TimeCreated: ${new Date(metadata.timeCreated)}`);
    console.log(`Last Metadata Update: ${new Date(metadata.updated)}`);
    // console.log(`temporaryHold: ${metadata.temporaryHold ? 'enabled' : 'disabled'}`);
    // console.log(`eventBasedHold: ${metadata.eventBasedHold ? 'enabled' : 'disabled'}`);
    if (metadata.retentionExpirationTime) {
      console.log(`retentionExpirationTime: ${new Date(metadata.retentionExpirationTime)}`);
    }
    return metadata;
  }

  return await getMetadata().catch(console.error);
}

/**
 * @exports module:cpb-storage/metadata
 * @description
 * # File and Bucket Metadata Operations
 */
export default {
  get: GetMetadata,
  set: SetMetadata,
};