不多说,在弄一个基于FileSytem/IndexedDB的小应用,目前处于基础开发阶段,

我们在使用FileSystem的时候无疑是需要知道浏览器的定额(配额的),怎么去查询,当然可以查询 Quota Management API

个人觉得还是略有复杂,为了避免不停的then,同时更少的关注内部的区别,再简单包装了一下。

不多说,代码:

/**
* 参考的API:
* http://w3c.github.io/quota-api/
*
*/ //文件系统请求标识
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem
//根据URL取得文件的读取权限
window.resolveLocalFileSystemURL = window.resolveLocalFileSystemURL || window.webkitResolveLocalFileSystemURL //临时储存和永久存储
navigator.temporaryStorage = navigator.temporaryStorage || navigator.webkitTemporaryStorage;
navigator.persistentStorage = navigator.persistentStorage || navigator.webkitPersistentStorage; //常量
const _TEMPORARY = 'temporary', _PERSISTENT = 'persistent' /**
* 转为promise,主要是把 a.b(param1,param2,successCallback,errorCall) 转为promise
* @param {*期待的是函数} obj
* @param {*上下文} ctx
* @param {*参数} args
*/
function toPromise(obj, ctx, ...args) {
if (!obj) return obj //如果已经是Promise对象
if ('function' == typeof obj.then) return obj //若obj是函数直接转换
if ('function' == typeof obj) return _toPromise(obj) return obj; //函数转成 promise
function _toPromise(fn) {
return new Promise(function (resolve, reject) { fn.call(ctx, ...args, (...ags) => {
//多个参数返回数组,单个直接返回对象
resolve(ags && ags.length > 1 ? ags : ags[0])
}, (err) => {
reject(err)
}) })
}
} /**
* 查询和申请定额
* 测试脚本:
* 使用情况: FileStorageQuota.instance.queryInfo().then(data=>console.log(data))
* 申请空间: FileStorageQuota.instance.requestPersistentQuota().then(data=>console.log(data))
*/
class FileStorageQuota { constructor() { let supportedTypes = [_TEMPORARY, _PERSISTENT]; this.storageQuota = navigator.storageQuota || {
storages: { [_TEMPORARY]: navigator.webkitTemporaryStorage, [_PERSISTENT]: navigator.webkitPersistentStorage },
queryInfo: function (type) {
return toPromise(this.storages[type].queryUsageAndQuota, this.storages[type]).then(arr => {
return { usage: arr[0], quota: arr[1] }
})
},
requestPersistentQuota: function (requestQuota) {
return toPromise(this.storages[_PERSISTENT].requestQuota, this.storages[_PERSISTENT], requestQuota * 1024 * 1024).then(quota => {
return { quota }
})
},
supportedTypes
}
this.supportedTypes = supportedTypes
this._instance = null //实例
} /**
* 获得实例
*/
static get instance() {
return !!this._instance ? this._instance : this._instance = new FileStorageQuota()
} /**
* 已经分配的额度和适用查询
* @param {*类型 window.TEMPORAR(0) |window.PERSISTENT(1) } type
*/
queryInfo(type = window.TEMPORARY) { return new Promise((resolve, reject) => {
this.storageQuota.queryInfo(this.supportedTypes[type])
.then(storageInfo => resolve({ quota: this.tansferBytes(storageInfo.quota), usage: this.tansferBytes(storageInfo.usage) }))
.catch(this.errorHandler(reject))
}) } /**
* 请求配额,只有PERSISTENT才需要用户允许,
* 返回值是你请求的和已经分配的大值
* @param {* window.TEMPORAR(0) |window.PERSISTENT(1)} type
* @param {* 请求的配额大小} requestQuota
*/
async requestPersistentQuota(requestQuota = 5) {
let { quota: quotaM, usage } = await this.queryInfo(window.PERSISTENT)
if (requestQuota > quotaM) {
return new Promise((resolve, reject) =>
this.storageQuota.requestPersistentQuota(requestQuota * 1024 * 1024)
.then(storageInfo => {
return resolve({ quota: this.tansferBytes(storageInfo.quota), usage: this.tansferBytes(storageInfo.usage || usage) })
})
.catch(this.errorHandler(reject)))
}
return { quota: Math.max(requestQuota, quotaM), usage }
} /**
* 把bytes换算成KB,M,G等
* @param {* bytes的长度} bytesLength
* @param {* 转为目标的单位} target
*/
tansferBytes(bytesLength, target = 'M') {
let m = {
'Byte': 0,
'KB': 1,
'M': 2,
'G': 3
}
return bytesLength / Math.pow(1024, m[target] || 0)
} /**
* Promise里面的错误处理
* @param {*} reject
*/
errorHandler(reject) {
return (error) => {
reject(error)
}
}
}

测试:

FileStorageQuota.instance.queryInfo().then(data=>console.log(data))

Object {quota: 2767.7421379089355, usage: 22.347068786621094}

  

FileStorageQuota.instance.requestPersistentQuota().then(data=>console.log(data))

Object {quota: 10240, usage: 0}

源码地址:https://github.com/xiangwenhu/BlogCodes/tree/master/client/FileSystem

参考:

Quota Management API

StorageQuota - Web APIs | MDN

File System 定额(配额查询)的更多相关文章

  1. 谷歌三大核心技术(一)The Google File System中文版

    谷歌三大核心技术(一)The Google File System中文版  The Google File System中文版 译者:alex 摘要 我们设计并实现了Google GFS文件系统,一个 ...

  2. File System 之本地文件系统

    上一篇文章提到了,最近做一个基于 File System/IndexedDB的应用,上一篇是定额和使用的查询. 因为LocalFileSystem只有chrome支持,有点尴尬,如果按需加载又何来尴尬 ...

  3. The Google File System 中文版

    摘要 我们设计并实现了Google文件系统,一个面向分布式数据密集型应用的.可伸缩的分布式文件系统.虽然运行在廉价的日用硬件设备上,但是它依然了提供容错功能,为大量客户机提供了很高的总体性能. 虽然与 ...

  4. 【转】谷歌三大核心技术(一)The Google File System中文版

      The Google File System中文版 译者:alex 摘要 我们设计并实现了Google GFS文件系统,一个面向大规模数据密集型应用的.可伸缩的分布式文件系统.GFS虽然运行在廉价 ...

  5. Google File System中文版

    英文原文地址: Google File system 译文原文地址: The Google File System中文版 Google File System中文版 摘要 我们设计并实现了Google ...

  6. The Google File System中文版

    译者:alex 摘要 我们设计并实现了Google GFS文件系统,一个面向大规模数据密集型应用的.可伸缩的分布式文件系统.GFS虽然运行在廉价的普遍硬件设备上,但是它依然了提供灾难冗余的能力,为大量 ...

  7. 大数据理论篇HDFS的基石——Google File System

    Google File System 但凡是要开始讲大数据的,都绕不开最初的Google三驾马车:Google File System(GFS), MapReduce,BigTable. 为这一切的基 ...

  8. The Google File System 翻译和理解

    The Google File System 摘要 GFS 是一个可扩展的分布式文件系统,用于大型分布式数据密集型应用上.它可以运行在便宜的普通硬件上,提供了高性能和一定的容错性. 1. 分布式文件系 ...

  9. 磁盘、分区及Linux文件系统 [Disk, Partition, Linux File System]

    1.磁盘基础知识 1.1 物理结构 硬盘的物理结构一般由磁头与碟片.电动机.主控芯片与排线等部件组成:当主电动机带动碟片旋转时,副电动机带动一组(磁头)到相对应的碟片上并确定读取正面还是反面的碟面,磁 ...

随机推荐

  1. ChromeExtension那些事儿

    Chrome Extension是什么呢? 简而言之,就是Chrome扩展,它是基于Chrome浏览器的,我们可以理解它为一个独立运行在Chrome浏览器下的APP,当然核心编程语言就是JavaScr ...

  2. 星云測试- Android应用深度体检专业平台

    星云測试-给你的Android应用做个深度体检   星云測试- Android应用深度体检专业平台 星云在线云測试(简称星云測试www.teststars.cc)是全球第一个公布并商用的数字化精准软件 ...

  3. 利用java mail发送邮件(转)

    JavaMail是SUN提供给开发者在应用程序中实现邮件发送和接收功能而提供的一套标准开发类库,支持经常使用的邮件协议,如SMTP.POP3.IMAP.开发者使用JavaMail编写邮件程序时,无需考 ...

  4. HTTP Status 500 - Request processing failed; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement

    1.什么操作出现:当我在项目中添加产品或者修改时,浏览器出现HTTP Status 500 - Request processing failed; nested exception is org.h ...

  5. 页面的新开页,window.open的hacker

    一.window.open如何进行hack 网上看的办法很多,归根接地还是不能解决掉,只有通过a标签的target属性 $obj.click(function(){  var newTab=windo ...

  6. linux vi/vim编辑文件显示行号

    方法一(最尴尬的方法): 1.显示当前行行号,在VI的命令模式下输入 :nu 2.显示所有行号,在VI的命令模式下输入 :set nu #这是:set number 的简写 方法二(最好的方法): 使 ...

  7. Android查缺补漏(View篇)--自定义 View 的基本流程

    View是Android很重要的一部分,常用的View有Button.TextView.EditView.ListView.GridView.各种layout等等,开发者通过对这些View的各种组合以 ...

  8. JDBC结果集rs.next()注意事项

    写在前面: 用JDBC从数据库中查询数据要用到结果集ResultSet,其中我们在获取结果的时候经常用到rs.next()方法来判断是否查询到了数据. 但是要特别注意,next()方法用一次,游标就往 ...

  9. 【python】字符串

    >>> str1="welcom to China">>> str1[2:4]'lc'>>> str1[7]'t'>&g ...

  10. iOS开发--SQLite重要框架FMDB的使用

    什么是FMDB: FMDB是一个和iOS的SQLite数据库操作相关的第三方框架.主要把C语言操作数据库的代码用OC进行了封装.使用者只需调用该框架的API就能用来创建并连接数据库,创建表,查询等. ...