session.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. var util = require('./util');
  2. // 按照文件特征值,缓存 UploadId
  3. var cacheKey = 'cos_sdk_upload_cache';
  4. var expires = 30 * 24 * 3600;
  5. var store;
  6. var cache;
  7. var timer;
  8. var getCache = function () {
  9. var val,
  10. opt = { configName: 'cos-nodejs-sdk-v5-storage' };
  11. if (this.options.ConfCwd) opt.cwd = this.options.ConfCwd;
  12. try {
  13. var Conf = require('conf');
  14. store = new Conf(opt);
  15. val = store.get(cacheKey);
  16. } catch (e) {}
  17. if (!val || !(val instanceof Array)) val = [];
  18. cache = val;
  19. };
  20. var setCache = function () {
  21. try {
  22. if (cache.length) store.set(cacheKey, cache);
  23. else store.delete(cacheKey);
  24. } catch (e) {}
  25. };
  26. var init = function () {
  27. if (cache) return;
  28. getCache.call(this);
  29. // 清理太老旧的数据
  30. var changed = false;
  31. var now = Math.round(Date.now() / 1000);
  32. for (var i = cache.length - 1; i >= 0; i--) {
  33. var mtime = cache[i][2];
  34. if (!mtime || mtime + expires < now) {
  35. cache.splice(i, 1);
  36. changed = true;
  37. }
  38. }
  39. changed && setCache();
  40. };
  41. // 把缓存存到本地
  42. var save = function () {
  43. if (timer) return;
  44. timer = setTimeout(function () {
  45. setCache();
  46. timer = null;
  47. }, 400);
  48. };
  49. var mod = {
  50. using: {},
  51. // 标记 UploadId 正在使用
  52. setUsing: function (uuid) {
  53. mod.using[uuid] = true;
  54. },
  55. // 标记 UploadId 已经没在使用
  56. removeUsing: function (uuid) {
  57. delete mod.using[uuid];
  58. },
  59. // 用上传参数生成哈希值
  60. getFileId: function (FileStat, ChunkSize, Bucket, Key) {
  61. if (FileStat && FileStat.FilePath && FileStat.size && FileStat.ctime && FileStat.mtime && ChunkSize) {
  62. return (
  63. util.md5([FileStat.FilePath].join('::')) +
  64. '-' +
  65. util.md5([FileStat.size, FileStat.ctime, FileStat.mtime, ChunkSize, Bucket, Key].join('::'))
  66. );
  67. } else {
  68. return null;
  69. }
  70. },
  71. // 用上传参数生成哈希值
  72. getCopyFileId: function (copySource, sourceHeaders, ChunkSize, Bucket, Key) {
  73. var size = sourceHeaders['content-length'];
  74. var etag = sourceHeaders.etag || '';
  75. var lastModified = sourceHeaders['last-modified'];
  76. if (copySource && ChunkSize) {
  77. return util.md5([copySource, size, etag, lastModified, ChunkSize, Bucket, Key].join('::'));
  78. } else {
  79. return null;
  80. }
  81. },
  82. // 获取文件对应的 UploadId 列表
  83. getUploadIdList: function (uuid) {
  84. if (!uuid) return null;
  85. init.call(this);
  86. var list = [];
  87. for (var i = 0; i < cache.length; i++) {
  88. if (cache[i][0] === uuid) list.push(cache[i][1]);
  89. }
  90. return list.length ? list : null;
  91. },
  92. // 缓存 UploadId
  93. saveUploadId: function (uuid, UploadId, limit) {
  94. init.call(this);
  95. if (!uuid) return;
  96. // 清理没用的 UploadId
  97. var part1 = uuid.substr(0, uuid.indexOf('-') + 1);
  98. for (var i = cache.length - 1; i >= 0; i--) {
  99. var item = cache[i];
  100. if (item[0] === uuid && item[1] === UploadId) {
  101. cache.splice(i, 1);
  102. } else if (uuid !== item[0] && item[0].indexOf(part1) === 0) {
  103. // 文件路径相同,但其他信息不同,说明文件改变了或上传参数(存储桶、路径、分片大小)变了,直接清理掉
  104. cache.splice(i, 1);
  105. }
  106. }
  107. cache.unshift([uuid, UploadId, Math.round(Date.now() / 1000)]);
  108. if (cache.length > limit) cache.splice(limit);
  109. save();
  110. },
  111. // UploadId 已用完,移除掉
  112. removeUploadId: function (UploadId) {
  113. init.call(this);
  114. delete mod.using[UploadId];
  115. for (var i = cache.length - 1; i >= 0; i--) {
  116. if (cache[i][1] === UploadId) cache.splice(i, 1);
  117. }
  118. save();
  119. },
  120. };
  121. module.exports = mod;