@harishreddym/baqend
Version:
Baqend JavaScript SDK
1,161 lines (928 loc) • 43.9 kB
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Baqend JavaScript SDK 2.14.1 - Source: lib/binding/File.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link rel="shortcut icon" type="image/x-icon" href="img/favicon.ico">
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/bootstrap-baqend.min.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
<link type="text/css" rel="stylesheet" href="styles/font-awesome-4.7.0.css">
</head>
<body>
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="navbar-inner container">
<!-- Collapsed navigation -->
<div class="navbar-header">
<!-- Expander button -->
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<!-- Main title -->
<a class="navbar-brand" href="/"><img src="img/logo.png"></a>
</div>
<!-- Expanded navigation -->
<div id="nav" class="navbar-collapse collapse">
<!-- Search -->
<form class="navbar-right form-inline search-form">
<div class="form-group search-form-group">
<input type="search" class="form-control search-input" id="search-query" placeholder="Search Guide" name="q" autocomplete="off">
<div id="search-results" class="search-results">
<p class="search-no-results">Please enter a search query ...</p>
</div>
<i class="search-icon fa fa-search"></i>
</div>
</form>
<!-- Main navigation -->
<ul class="nav navbar-nav">
<li>
<a href="https://dashboard.baqend.com/">Dashboard</a>
</li>
<!-- Guide Navigation -->
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Guide <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="https://www.baqend.com/guide/">Home</a></li>
<li><a href="https://www.baqend.com/guide/#speed-kit">Speed Kit</a></li>
<li><a href="https://www.baqend.com/guide/#platform">Platform</a></li>
<li><a href="https://www.baqend.com/guide/roadmap/">Roadmap</a></li>
<li><a href="https://www.baqend.com/guide/topics/faq/">FAQ</a></li>
</ul>
</li>
<!-- Starter Kits -->
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Starter Kits <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="https://www.baqend.com/guide/starter-kits/">Starter Kits Overview</a></li>
<li><a href="https://www.baqend.com/guide/starter-kits/angular2/">Angular 2 Starter Kit</a></li>
<li><a href="https://www.baqend.com/guide/starter-kits/react/">React and Redux Starter Kit</a></li>
<li><a href="https://www.baqend.com/guide/starter-kits/bootstrap/">Bootstrap Starter Kit</a></li>
<li><a href="https://www.baqend.com/guide/starter-kits/ionic2/">Ionic 2 Starter Kit</a></li>
<li><a href="https://www.baqend.com/guide/starter-kits/ionic/">Ionic Starter Kit</a></li>
</ul>
</li>
<li class="active">
<a href="baqend.html">JS API</a>
</li>
<li>
<a href="https://www.baqend.com/tutorial.html">Tutorial</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="box gray pt-32 pb-32">
<div class="container">
<div class="row">
<!-- <div class="col-md-3"></div> -->
<div class="bs-sidebar hidden-print fixed affix" role="complementary">
<input class="filter form-control input-sm" type="text" placeholder="FILTER"/>
<ul class="nav bs-sidenav">
<li class=""><a href="Acl.html" class="nav-name">Acl</a></li>
<li class=""><a href="EntityManager.html" class="nav-name">EntityManager</a></li>
<li class=""><a href="EntityManagerFactory.html" class="nav-name">EntityManagerFactory</a></li>
<li class=""><a href="GeoPoint.html" class="nav-name">GeoPoint</a></li>
<li class=""><a href="RealtimeEvent.html" class="nav-name">RealtimeEvent</a></li>
<li class=""><a href="baqend.html" class="nav-name">baqend</a></li>
<li class="">
<a href="binding.html" class="nav-name">binding</a>
<ul class="nav">
<li class=""><a href="binding.Accessor.html" class="nav-name">Accessor</a></li>
<li class=""><a href="binding.DeviceFactory.html" class="nav-name">DeviceFactory</a></li>
<li class=""><a href="binding.Enhancer.html" class="nav-name">Enhancer</a></li>
<li class=""><a href="binding.Entity.html" class="nav-name">Entity</a></li>
<li class=""><a href="binding.EntityFactory.html" class="nav-name">EntityFactory</a></li>
<li class=""><a href="binding.Factory.html" class="nav-name">Factory</a></li>
<li class=""><a href="binding.File.html" class="nav-name">File</a></li>
<li class=""><a href="binding.FileFactory.html" class="nav-name">FileFactory</a></li>
<li class=""><a href="binding.Managed.html" class="nav-name">Managed</a></li>
<li class=""><a href="binding.ManagedFactory.html" class="nav-name">ManagedFactory</a></li>
<li class=""><a href="binding.Role.html" class="nav-name">Role</a></li>
<li class=""><a href="binding.User.html" class="nav-name">User</a></li>
<li class=""><a href="binding.UserFactory.html" class="nav-name">UserFactory</a></li>
</ul>
</li>
<li class="">
<a href="caching.html" class="nav-name">caching</a>
<ul class="nav">
<li class=""><a href="caching.BloomFilter.html" class="nav-name">BloomFilter</a></li>
</ul>
</li>
<li class="">
<a href="connector.html" class="nav-name">connector</a>
<ul class="nav">
<li class=""><a href="connector.Connector.html" class="nav-name">Connector</a></li>
<li class=""><a href="connector.FetchConnector.html" class="nav-name">FetchConnector</a></li>
<li class=""><a href="connector.IFrameConnector.html" class="nav-name">IFrameConnector</a></li>
<li class=""><a href="connector.Message.html" class="nav-name">Message</a></li>
<li class=""><a href="connector.NodeConnector.html" class="nav-name">NodeConnector</a></li>
<li class=""><a href="connector.ObservableStream.html" class="nav-name">ObservableStream</a></li>
<li class=""><a href="connector.WebSocketConnector.html" class="nav-name">WebSocketConnector</a></li>
<li class=""><a href="connector.XMLHttpConnector.html" class="nav-name">XMLHttpConnector</a></li>
<li class=""><a href="connector.ChannelMessage.html" class="nav-name">ChannelMessage</a></li>
</ul>
</li>
<li class="">
<a href="error.html" class="nav-name">error</a>
<ul class="nav">
<li class=""><a href="error.CommunicationError.html" class="nav-name">CommunicationError</a></li>
<li class=""><a href="error.EntityExistsError.html" class="nav-name">EntityExistsError</a></li>
<li class=""><a href="error.IllegalEntityError.html" class="nav-name">IllegalEntityError</a></li>
<li class=""><a href="error.PersistentError.html" class="nav-name">PersistentError</a></li>
<li class=""><a href="error.RollbackError.html" class="nav-name">RollbackError</a></li>
</ul>
</li>
<li class="">
<a href="metamodel.html" class="nav-name">metamodel</a>
<ul class="nav">
<li class=""><a href="metamodel.Attribute.html" class="nav-name">Attribute</a></li>
<li class=""><a href="metamodel.BasicType.html" class="nav-name">BasicType</a></li>
<li class=""><a href="metamodel.CollectionAttribute.html" class="nav-name">CollectionAttribute</a></li>
<li class=""><a href="metamodel.DbIndex.html" class="nav-name">DbIndex</a></li>
<li class=""><a href="metamodel.EmbeddableType.html" class="nav-name">EmbeddableType</a></li>
<li class=""><a href="metamodel.EntityType.html" class="nav-name">EntityType</a></li>
<li class=""><a href="metamodel.ListAttribute.html" class="nav-name">ListAttribute</a></li>
<li class=""><a href="metamodel.ManagedType.html" class="nav-name">ManagedType</a></li>
<li class=""><a href="metamodel.MapAttribute.html" class="nav-name">MapAttribute</a></li>
<li class=""><a href="metamodel.Metamodel.html" class="nav-name">Metamodel</a></li>
<li class=""><a href="metamodel.ModelBuilder.html" class="nav-name">ModelBuilder</a></li>
<li class=""><a href="metamodel.PluralAttribute.html" class="nav-name">PluralAttribute</a></li>
<li class=""><a href="metamodel.SetAttribute.html" class="nav-name">SetAttribute</a></li>
<li class=""><a href="metamodel.SingularAttribute.html" class="nav-name">SingularAttribute</a></li>
<li class=""><a href="metamodel.Type.html" class="nav-name">Type</a></li>
</ul>
</li>
<li class="">
<a href="model.html" class="nav-name">model</a>
<ul class="nav">
<li class=""><a href="model.Device.html" class="nav-name">Device</a></li>
<li class=""><a href="model.Role.html" class="nav-name">Role</a></li>
<li class=""><a href="model.User.html" class="nav-name">User</a></li>
</ul>
</li>
<li class="">
<a href="partialupdate.html" class="nav-name">partialupdate</a>
<ul class="nav">
<li class=""><a href="partialupdate.EntityPartialUpdateBuilder.html" class="nav-name">EntityPartialUpdateBuilder</a></li>
<li class=""><a href="partialupdate.PartialUpdateBuilder.html" class="nav-name">PartialUpdateBuilder</a></li>
<li class=""><a href="partialupdate.UpdateOperation.html" class="nav-name">UpdateOperation</a></li>
</ul>
</li>
<li class="">
<a href="query.html" class="nav-name">query</a>
<ul class="nav">
<li class=""><a href="query.Builder.html" class="nav-name">Builder</a></li>
<li class=""><a href="query.Filter.html" class="nav-name">Filter</a></li>
<li class=""><a href="query.Node.html" class="nav-name">Node</a></li>
<li class=""><a href="query.Operator.html" class="nav-name">Operator</a></li>
<li class=""><a href="query.Query.html" class="nav-name">Query</a></li>
<li class=""><a href="query.Stream.html" class="nav-name">Stream</a></li>
<li class=""><a href="query.Condition.html" class="nav-name">Condition</a></li>
</ul>
</li>
<li class="">
<a href="util.html" class="nav-name">util</a>
<ul class="nav">
<li class=""><a href="util.Code.html" class="nav-name">Code</a></li>
<li class=""><a href="util.Lockable.html" class="nav-name">Lockable</a></li>
<li class=""><a href="util.Logger.html" class="nav-name">Logger</a></li>
<li class=""><a href="util.Metadata.html" class="nav-name">Metadata</a></li>
<li class=""><a href="util.Modules.html" class="nav-name">Modules</a></li>
<li class=""><a href="util.Permission.html" class="nav-name">Permission</a></li>
<li class=""><a href="util.PushMessage.html" class="nav-name">PushMessage</a></li>
<li class=""><a href="util.TokenStorage.html" class="nav-name">TokenStorage</a></li>
<li class=""><a href="util.ValidationResult.html" class="nav-name">ValidationResult</a></li>
<li class=""><a href="util.Validator.html" class="nav-name">Validator</a></li>
<li class=""><a href="util.TokenStorageFactory.html" class="nav-name">TokenStorageFactory</a></li>
</ul>
</li>
</ul>
</div>
<div class="col-md-12" id="main">
<div class="content">
<h1 class="page-title">Source: lib/binding/File.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>'use strict';
const error = require('../error');
const Acl = require('../Acl');
const uuid = require('../util/uuid').uuid;
const message = require('../message');
const StatusCode = require('../connector/Message').StatusCode;
const deprecated = require('../util/deprecated');
const trailingSlashIt = require('./trailingSlashIt').trailingSlashIt;
const FILE_BUCKET = '/file';
const FILE_BUCKET_LENGTH = FILE_BUCKET.length;
const ID = Symbol('Id');
const METADATA = Symbol('Metadata');
const DATA = Symbol('Data');
/**
* Creates a file object, which represents one specific file reference.
* This File object can afterwards be used to up- and download the file contents or to retrieves and change the files
* metadata.
*
* The file data can be uploaded and downloaded as:
*
* <table class="table">
* <tr>
* <th>type</th>
* <th>JavaScript type</th>
* <th>Description</th>
* </tr>
* <tr>
* <td>'arraybuffer'</td>
* <td>ArrayBuffer</td>
* <td>The content is represented as a fixed-length raw binary data buffer</td>
* </tr>
* <tr>
* <td>'blob'</th>
* <td>Blob</td>
* <td>The content is represented as a simple blob</td>
* </tr>
* <tr>
* <td>'json'</td>
* <td>object|array|string</td>
* <td>The file content is represented as json</td>
* </tr>
* <tr>
* <td>'text'</td>
* <td>string</td>
* <td>The file content is represented through the string</td>
* </tr>
* <tr>
* <td>'base64'</td>
* <td>string</td>
* <td>The file content as base64 encoded string</td>
* </tr>
* <tr>
* <td>'data-url'</td>
* <td>string</td>
* <td>A data url which represents the file content</td>
* </tr>
* </table>
*
*
* binding.File
*/
class File {
/**
* The complete id of the file, including folder and name
* @type {string}
* @readonly
*/
get id() {
return this[ID];
}
/**
* The fully url to the file, can be directly used to link the file, i.e. in link tags ot image sources
* @type {string}
* @readonly
*/
get url() {
if (this.isFolder) {
throw new Error('Url can not be created for folders.');
}
return this.db.createURL(this.id, this.bucket !== 'www');
}
/**
* The name of the file
* @type {string}
* @readonly
*/
get name() {
return this.id.substring(this.id.lastIndexOf('/', this.id.length - 2) + 1);
}
/**
* The mimeType of the file, only accessible after fetching the metadata or downloading/uploading/providing the file
* @type {string}
* @readonly
*/
get mimeType() {
if (this.isFolder) {
throw new Error('A folder has no mimeType');
}
this.checkAvailable();
return this[METADATA].mimeType;
}
/**
* The current file acl, only accessible after fetching the metadata or downloading/uploading/providing the file
* @type {Acl}
* @readonly
*/
get acl() {
this.checkAvailable();
return this[METADATA].acl;
}
/**
* The last modified date of the file, only accessible after fetching the metadata
* or downloading/uploading/providing the eTag
* @type {?Date}
* @readonly
*/
get lastModified() {
if (this.isFolder) {
throw new Error('A folder has no lastModified');
}
this.checkAvailable();
return this[METADATA].lastModified;
}
/**
* The creation date of the file, only accessible after fetching the metadata
* or downloading/uploading/providing the eTag
* @type {?Date}
* @readonly
*/
get createdAt() {
if (this.isFolder) {
throw new Error('A folder has no creation date');
}
this.checkAvailable();
return this[METADATA].createdAt;
}
/**
* The eTag of the file, only accessible after fetching the metadata or downloading/uploading/providing the file
* @type {string}
* @readonly
*/
get eTag() {
if (this.isFolder) {
throw new Error('A folder has no eTag');
}
this.checkAvailable();
return this[METADATA].eTag;
}
/**
* The custom headers of the file, only accessible after fetching the metadata or downloading/uploading/providing
* the file
* @type {Object<string,string>}
* @readonly
*/
get headers() {
if (this.isFolder) {
throw new Error('A folder has no custom headers');
}
this.checkAvailable();
return this[METADATA].headers;
}
/**
* The size of the file, only accessible after fetching the metadata or downloading/uploading/providing the file
* @type {number}
* @readonly
*/
get size() {
if (this.isFolder) {
throw new Error('A folder has no size');
}
this.checkAvailable();
return this[METADATA].size;
}
/**
* @type {string}
* @readonly
*/
get bucket() {
return this.id.substring(FILE_BUCKET_LENGTH + 1, this.id.indexOf('/', FILE_BUCKET_LENGTH + 1));
}
/**
* @type {string}
* @readonly
*/
get key() {
return this.id.substring(this.id.indexOf('/', FILE_BUCKET_LENGTH + 1) + 1);
}
/**
* The full path of the file.
* @type {string}
* @readonly
*/
get path() {
return this.id.substring(FILE_BUCKET_LENGTH);
}
/**
* The parent folder of the file.
* @type {string}
* @readonly
*/
get parent() {
return this.id.substring(FILE_BUCKET_LENGTH, this.id.lastIndexOf('/', this.id.length - 2));
}
/**
* Indicates if the metadata are loaded.
* @type {boolean}
* @readonly
*/
get isMetadataLoaded() {
return !!this[METADATA];
}
/**
* Creates a new file object which represents a file at the given id. Data which is provided to the constructor will
* be uploaded by invoking {@link upload()}
* @param {object|string} fileOptions The fileOptions used to create a new file object, or just the id of the file
* @param {string=} fileOptions.id The id of the file.
* @param {string=} fileOptions.name The filename without the id. If omitted and data is provided as a file object,
* the {@link File#name} will be used otherwise a UUID will be generated.
* @param {string} [fileOptions.parent="/www"] The parent folder which contains the file
* @param {string} [fileOptions.path="/www"] The full path of the file.
* You might either specify the path of the file or a combination of parent and file name.
* @param {string|Blob|File|ArrayBuffer|json=} fileOptions.data The initial file content, which will be uploaded by
* invoking {@link #upload} later on.
* @param {string=} fileOptions.type A optional type hint used to correctly interpret the provided data
* @param {string=} fileOptions.mimeType The mimType of the file. Defaults to the mimeType of the provided data if
* it is a file object, blob or data-url
* @param {number=} fileOptions.size The size of the file content in bytes
* @param {string=} fileOptions.eTag The optional current ETag of the file
* @param {string|Date=} fileOptions.lastModified The optional last modified date
* @param {Acl=} fileOptions.acl The file acl which will be set, if the file is uploaded afterwards
* @param {Object<string,string>} [fileOptions.headers] The custom headers which will be send with the file after
* uploading it
*/
constructor(fileOptions) {
// Is fileOptions just an id?
const opt = typeof fileOptions === 'string' ? { id: fileOptions } : (fileOptions || {});
if (opt.id) {
// Check validity of id
const nameSeparator = opt.id.indexOf('/', '/file/'.length);
if (nameSeparator === -1 || opt.id.indexOf('/file/') !== 0) {
throw new Error('Invalid file reference ' + opt.id);
}
this[ID] = opt.id;
} else {
this[ID] = this.createIdFromOptions(opt);
}
// Assign metadata
this.setDataOptions(opt);
/**
* Specifies whether this file is a folder.
* @type {boolean}
* @readonly
*/
this.isFolder = this.id.charAt(this.id.length - 1) === '/';
}
/**
* Parses an E-Tag header
* @param {string=} eTag The E-Tag to parse or something falsy
* @return {?string} Returns the parsed E-Tag or null, if it could not be parsed
*/
static parseETag(eTag) {
if (!eTag) {
return null;
}
const match = eTag.match(/^(?:[wW]\/)?["'](.*)["']$/);
if (!match) {
return null;
}
return match[1];
}
/**
* Uploads the file content which was provided in the constructor or by uploadOptions.data
* @param {object=} uploadOptions The upload options
* @param {string|Blob|File|ArrayBuffer|json} [uploadOptions.data] The initial file content, which will be uploaded by
* invoking {@link #upload} later on.
* @param {string=} uploadOptions.type A optional type hint used to correctly interpret the provided data
* @param {string=} uploadOptions.mimeType The mimType of the file. Defaults to the mimeType of the provided data if
* it is a file object, blob or data-url
* @param {string=} uploadOptions.eTag The optional current ETag of the file
* @param {string=} uploadOptions.lastModified The optional last modified date
* @param {Acl=} uploadOptions.acl The file acl which will be set, if the file is uploaded afterwards
* @param {Object<string,string>} [uploadOptions.headers] The custom headers which will be send with the file after
* uploading it
* @param {boolean} [uploadOptions.force=false] force the upload and overwrite any existing files without validating
* it
* @param {connector.Message~progressCallback} [uploadOptions.progress] listen to progress changes during upload
* @param {binding.File~fileCallback=} doneCallback The callback is invoked after the upload succeed successfully
* @param {binding.File~failCallback=} failCallback The callback is invoked if any error is occurred
* @return {Promise<binding.File>} A promise which will be fulfilled with this file object
* where the metadata is updated
*/
upload(uploadOptions, doneCallback, failCallback) {
const opt = uploadOptions || {};
if (this.isFolder) {
throw new Error('A folder cannot be uploaded');
}
this.setDataOptions(opt);
const uploadMessage = new message.UploadFile(this.bucket, this.key)
.entity(this[DATA].data, this[DATA].type);
const meta = this[METADATA];
if (meta) {
uploadMessage.acl(meta.acl);
uploadMessage.contentLength(meta.size);
uploadMessage.mimeType(meta.mimeType);
uploadMessage.customHeaders(meta.headers);
}
uploadMessage.progress(opt.progress);
this.conditional(uploadMessage, opt);
this.db.addToBlackList(this.id);
return this.db.send(uploadMessage).then((response) => {
this[DATA] = null;
this.fromJSON(response.entity);
return this;
}).then(doneCallback, failCallback);
}
/**
* Download a file and providing it in the requested type
* @param {object=} downloadOptions The download options
* @param {string} [downloadOptions.type="blob"] The type used to provide the file
* @param {string} [downloadOptions.refresh=false] Indicates to make a revalidation request and not use the cache
* @param {binding.File~downloadCallback=} doneCallback The callback is invoked after the download succeed
* successfully
* @param {binding.File~failCallback=} failCallback The callback is invoked if any error is occurred
* @return {Promise<string|Blob|File|ArrayBuffer|json>} A promise which will be fulfilled with the downloaded
* file content
*/
download(downloadOptions, doneCallback, failCallback) {
const opt = downloadOptions || {};
if (this.isFolder) {
throw new Error('A folder cannot be downloaded');
}
const type = opt.type || 'blob';
const downloadMessage = new message.DownloadFile(this.bucket, this.key)
.responseType(type);
this.db.ensureCacheHeader(this.id, downloadMessage, opt.refresh);
return this.db.send(downloadMessage).then((response) => {
this.db.addToWhiteList(this.id);
this.fromHeaders(response.headers);
return response.entity;
}, (e) => {
if (e.status === StatusCode.OBJECT_NOT_FOUND) {
return null;
}
throw e;
}).then(doneCallback, failCallback);
}
/**
* Deletes a file
* @param {object=} deleteOptions The delete options
* @param {boolean} [deleteOptions.force=false] force the deletion without verifying any version
* @param {binding.File~deleteCallback=} doneCallback The callback is invoked after the deletion succeed successfully
* @param {binding.File~failCallback=} failCallback The callback is invoked if any error is occurred
* @return {Promise<binding.File|binding.File[]>} A promise which will be fulfilled with this file object,
* or with a list of all deleted files, if this file is an folder
*/
delete(deleteOptions, doneCallback, failCallback) {
const opt = deleteOptions || {};
const deleteMessage = new message.DeleteFile(this.bucket, this.key);
this.conditional(deleteMessage, opt);
if (!this.isFolder) {
this.db.addToBlackList(this.id);
}
return this.db.send(deleteMessage).then((response) => {
if (!this.isFolder) {
return this;
}
return response.entity.map(fileId => this.db.File(fileId));
}).then(doneCallback, failCallback);
}
/**
* Creates the file id from given options.
* @param {*} fileOptions
* @return {string}
* @private
*/
createIdFromOptions(fileOptions) {
/** @var {string} */
let path;
if (fileOptions.path) {
path = fileOptions.path;
} else {
const parent = trailingSlashIt(fileOptions.parent || '/www');
if (parent.length < 3) {
throw new Error('Invalid parent name: ' + parent);
}
const name = fileOptions.name || (fileOptions.data && fileOptions.data.name) || uuid();
path = parent + name;
}
// Add leading slash if missing
if (path.charAt(0) !== '/') {
path = '/' + path;
}
// Check path validity
if (path.indexOf('//') !== -1 || path.length < 3) {
throw new Error('Invalid path: ' + path);
}
return FILE_BUCKET + path;
}
/**
* Makes the given message a conditional request based on the file metadata
* @param {connector.Message} msg The message to make conditional
* @param {object} options additional request options
* @param {boolean} [options.force=false] Force the request operation by didn't make it conditional
* @return {void}
*/
conditional(msg, options) {
if (options.force) {
return;
}
const meta = this[METADATA];
if (!meta || (!meta.lastModified && !meta.eTag)) {
msg.ifNoneMatch('*');
return;
}
msg.ifUnmodifiedSince(meta.lastModified);
msg.ifMatch(meta.eTag);
}
/**
* Gets the file metadata of a file
* @param {Object=} options The load metadata options
* @param {boolean} [options.refresh=false] Force a revalidation while fetching the metadata
* @param {binding.File~fileCallback=} doneCallback The callback is invoked after the metadata is fetched
* @param {binding.File~failCallback=} failCallback The callback is invoked if any error has occurred
* @return {Promise<binding.File>} A promise which will be fulfilled with this file
*/
loadMetadata(options, doneCallback, failCallback) {
const opt = options || {};
if (this.isFolder) {
throw new Error('A folder has no matadata.');
}
const msg = new message.GetFileMetadata(this.bucket, this.key);
this.db.ensureCacheHeader(this.id, msg, opt.refresh);
return this.db.send(msg).then((response) => {
// do not white list the file, because head-request does not revalidate the cache.
this.fromHeaders(response.headers);
return this;
}, (e) => {
if (e.status === StatusCode.OBJECT_NOT_FOUND) {
return null;
}
throw e;
}).then(doneCallback, failCallback);
}
/**
* Updates the matadata of this file.
* @param {Object=} options The save metadata options
* @param {boolean} [options.force=false] force the update and overwrite the existing metadata without validating it
* @param {binding.File~fileCallback=} doneCallback The callback is invoked after the metadata is saved
* @param {binding.File~failCallback=} failCallback The callback is invoked if any error has occurred
* @return {Promise<binding.File>} A promise which will be fulfilled with this file
*/
saveMetadata(options, doneCallback, failCallback) {
const opt = options || {};
const json = this.toJSON();
const msg = new message.UpdateFileMetadata(this.bucket, this.key)
.entity(json);
this.conditional(msg, opt);
return this.db.send(msg).then((response) => {
this.fromJSON(response.entity);
return this;
}).then(doneCallback, failCallback);
}
/**
* Validates and sets the file metadata based on the given options
* @param {object} options
* @private
*/
setDataOptions(options) {
const data = options.data;
const type = options.type;
if (!data) {
return;
}
// Set data
this[DATA] = { type, data };
const mimeType = this.guessMimeType(options);
this.fromJSON(Object.assign({}, options, { mimeType }));
}
/**
* Gets the MIME type of given file options.
* @param {object} options
* @return {?string} Returns the guessed MIME type or null, if it could not be guessed.
* @private
*/
guessMimeType(options) {
const mimeType = options.mimeType;
if (mimeType) {
return mimeType;
}
if (typeof Blob !== 'undefined' && options.data instanceof Blob) {
return options.data.type;
}
if (options.type === 'data-url') {
const match = options.data.match(/^data:(.+?)(;base64)?,.*$/);
return match[1];
}
return null;
}
/**
* @param {Object<string,string>} headers
* @return {void}
* @private
*/
fromHeaders(headers) {
this.fromJSON({
eTag: File.parseETag(headers.etag),
lastModified: headers['last-modified'],
createdAt: headers['baqend-created-at'],
mimeType: headers['content-type'],
acl: headers['baqend-acl'] && JSON.parse(headers['baqend-acl']),
size: +headers['baqend-size'],
headers: headers['baqend-custom-headers'] && JSON.parse(headers['baqend-custom-headers']),
});
}
/**
* Deserialize the given JSON file metadata back to this file instance
*
* If the JSON object contains an ID, it must match with this file ID, otherwise an exception is thrown.
*
* @param {json} json The json to deserialize
* @return {void}
*/
fromJSON(json) {
if (json.id && this.id !== json.id) {
throw new Error('This file id ' + this.id + ' does not match the given json id ' + json.id);
}
const meta = this[METADATA] || {};
let acl;
if (json.acl instanceof Acl) {
acl = json.acl;
} else {
acl = meta.acl || new Acl();
if (json.acl) {
acl.fromJSON(json.acl);
}
}
// keep last known lastModified, createdAt, eTag and headers
this[METADATA] = Object.assign({}, this[METADATA], {
mimeType: json.mimeType,
lastModified: (json.lastModified && new Date(json.lastModified)) || meta.lastModified,
createdAt: (json.createdAt && new Date(json.createdAt)) || meta.createdAt,
eTag: json.eTag || meta.eTag,
acl,
size: typeof json.size === 'number' ? json.size : json.contentLength,
headers: json.headers || meta.headers || {},
});
}
/**
* Serialize the file metadata of this object to json
* @return {json} The serialized file metadata as json
*/
toJSON() {
this.checkAvailable();
const meta = this[METADATA];
return {
id: this.id,
mimeType: meta.mimeType,
eTag: meta.eTag,
acl: meta.acl.toJSON(),
size: meta.size,
lastModified: meta.lastModified && meta.lastModified.toISOString(),
createdAt: meta.createdAt && meta.createdAt.toISOString(),
headers: meta.headers,
};
}
/**
* Checks whenever metadata are already loaded of the file, throws an error otherwise
* @return {void}
*/
checkAvailable() {
if (!this.isMetadataLoaded) {
throw new error.PersistentError('The file metadata of ' + this.id + ' is not available.');
}
}
/**
* The database connection to use
* @name db
* @type {EntityManager}
* @memberOf File.prototype
* @field
* @readonly
*/
}
/**
* The database connection to use
* @member File.prototype
*/
deprecated(File.prototype, '_db', 'db');
deprecated(File.prototype, '_conditional', 'conditional');
deprecated(File.prototype, '_setMetadata', 'setDataOptions');
deprecated(File.prototype, '_checkAvailable', 'checkAvailable');
/**
* The file callback is called, when the asynchronous operation completes successfully
* @callback binding.File~fileCallback
* @param {binding.File} file The updated file metadata
* @return {*} A Promise, result or undefined
*/
/**
* The download callback is called, when the asynchronous download completes successfully
* @callback binding.File~downloadCallback
* @param {string|Blob|File|ArrayBuffer|json} data The download file content in the requested format
* @return {*} A Promise, result or undefined
*/
/**
* The delete callback is called, when the asynchronous deletion completes successfully
* @callback binding.File~deleteCallback
* @param {binding.File} data The file metadata
* @return {*} A Promise, result or undefined
*/
/**
* The fail callback is called, when the asynchronous operation is rejected by an error
* @callback binding.File~failCallback
* @param {error.PersistentError} error The error which reject the operation
* @return {*} A Promise, result or undefined
*/
module.exports = File;
</code></pre>
</article>
</section>
<!-- <footer>
<strong>Baqend JavaScript SDK 2.14.1</strong><br>
© 2019 Baqend GmbH<br>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Sun, 06 Jan 2019 05:00:21 GMT
</footer> -->
</div>
</div>
</div>
</div>
</div>
<div id="footer" style="position:relative; z-index:1;">
<div class="container">
<div class="row">
<div class="col-xs-6 col-sm-3 sitemap">
<h4>Product</h4>
<a href="https://www.baqend.com/platform.html">Platform</a>
<a href="https://www.baqend.com/speedkit.html">Speed Kit</a>
<a href="https://www.baqend.com/features.html">Features</a>
<a href="https://www.baqend.com/pricing.html">Platform Pricing</a>
<a href="https://www.baqend.com/pricing_speedkit.html">Speed Kit Pricing</a>
<a href="https://www.baqend.com/agencies.html">Agencies</a>
<a href="https://www.baqend.com/enterprise.html">Enterprise</a>
<a href="https://www.baqend.com/features.html#download">Community Edition</a>
<a href="https://www.baqend.com/support.html">Support</a>
</div>
<div class="col-xs-6 col-sm-3 sitemap">
<h4>Company</h4>
<a href="https://www.baqend.com/about.html">About us</a>
<a href="https://medium.baqend.com">Baqend Blog</a>
<a href="https://www.baqend.com/hiring.html">Jobs</a>
<a href="https://www.baqend.com/press.html">Press</a>
<a href="https://thesis.app.baqend.com/">Bachelor/Master theses</a>
<a href="https://www.weblabs.hamburg/">WebLabs.Hamburg</a>
</div>
<div class="col-xs-6 col-sm-3 sitemap">
<h4>Developer</h4>
<a href="https://dashboard.baqend.com/login">Login</a>
<a href="https://dashboard.baqend.com/register">Sign Up</a>
<a href="https://www.baqend.com/guide">Developer Docs</a>
<a href="https://www.baqend.com/guide/roadmap/">Roadmap</a>
<a href="https://www.baqend.com/guide/starter-kits">Starter Kits</a>
<a href="https://www.baqend.com/js-sdk/latest/baqend.html">JS SDK</a>
<a href="https://stackoverflow.com/questions/tagged/baqend/">Stackoverflow</a>
</div>
<div class="col-xs-6 col-sm-3 contact">
<h4>Contact</h4>
<p class="contact-information">
<span>
Baqend GmbH<br />
Stresemannstr. 23<br />
22769 Hamburg<br />
Germany
</span>
</p>
<p class="contact-information">
Email: <a style="display: inline-block" href="mailto:support@baqend.com">support@baqend.com</a>
</p>
<p class="contact-information">
Tel: <a style="display: inline-block" href="tel:+494060940539">+49 40 60940539</a>
</p>
</div>
</div>
<div class="row">
<div class="col-md-12 text-center social">
<a href="https://twitter.com/baqendcom"><i class="fa fa-twitter fa-fw"></i></a>
<a href="https://github.com/Baqend"><i class="fa fa-github fa-fw"></i></a>
<a href="https://medium.baqend.com/"><i class="fa fa-medium fa-fw"></i></a>
<a href="https://www.facebook.com/baqend"><i class="fa fa-facebook fa-fw"></i></a>
<a href="https://www.youtube.com/channel/UCsImg6Ts8UEp6-7LE9CP2-Q"><i class="fa fa-youtube fa-fw"></i></a>
</div>
</div>
</div>
<footer>
<div class="container">
<div class="legal text-center">
<div>
<strong>Baqend JavaScript SDK 2.14.1</strong><br>
© 2019 Baqend GmbH<br>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Sun, 06 Jan 2019 05:00:21 GMT
</div>
<a href="mailto:info@baqend.com">Contact</a> -
<a href="https://dashboard.baqend.com/privacy">Privacy Policy</a> -
<a href="https://dashboard.baqend.com/terms">Terms of Service</a> -
<a href="https://dashboard.baqend.com/imprint">Imprint</a>
</div>
</div>
</footer>
</div>
<script> prettyPrint(); </script>
<script src="https://code.jquery.com/jquery-1.12.1.min.js"></script>
<script src="scripts/bootstrap.min.js"> </script>
<script src="scripts/linenumber.js"> </script>
<script src="scripts/filter.js"> </script>
<script data-main="scripts/search.js" src="scripts/require.js"></script>
</body>
</html>