mediaService.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. const path = require('path');
  2. const fs = require('fs');
  3. const { AccessModel, ACCESS_TYPE, ACCESS_TO, ACCESS_GRANT } = require('./access.js');
  4. function Media()
  5. {
  6. }
  7. function MediaStruct(i) {
  8. this.path = i.path;
  9. this.fileName = path.parse(i.path).name;
  10. this.md5sum = i.md5sum;
  11. this.date = i.date;
  12. this.meta = {};
  13. this.tags = [];
  14. this.fixedTags = [];
  15. this.accessType = -1;
  16. }
  17. MediaStruct.prototype.pushMeta = function(key, value) {
  18. if (key && value && !this.meta[key]) {
  19. let type = '';
  20. if (['photochamberImport', 'dateTime'].indexOf(key) >= 0)
  21. type = 'date';
  22. else if (['height', 'width', 'iso', 'focal', 'fNumber', 'exposureTime'].indexOf(key) >= 0)
  23. type = 'number';
  24. else if (['geoHash', 'gpsLocation'].indexOf(key) >= 0)
  25. type = 'geoData';
  26. else if (['artist', 'camera', 'lensModel', 'exposureTimeStr', 'libraryPath', 'compression',
  27. 'software', 'geoCountry'].indexOf(key) >= 0)
  28. type = 'string';
  29. else if (['fileSize'].indexOf(key) >= 0)
  30. type = 'octet';
  31. else console.log(`Unknown meta type ${key} (${value})`);
  32. this.meta[key] = { type: type, value: value };
  33. }
  34. }
  35. MediaStruct.prototype.pushTag = function(tag, isFixedTag) {
  36. if (!tag)
  37. return;
  38. if (!isFixedTag && this.tags.indexOf(tag) === -1)
  39. this.tags.push(tag);
  40. if (isFixedTag && this.fixedTags.indexOf(tag) === -1)
  41. this.fixedTags.push(tag);
  42. }
  43. MediaStruct.prototype.computeAccess = function(accessList) {
  44. if (this.accessType > -1)
  45. return this.accessType;
  46. if (!fs.existsSync(this.path))
  47. return this.accessType = ACCESS_GRANT.none;
  48. const checkTag = function(tags, access) {
  49. if (!tags.length)
  50. return false;
  51. for (let i of tags)
  52. if (i.startsWith(access.accessToData+'/') || i === access.accessToData)
  53. return true;
  54. return false;
  55. }
  56. const checkMeta = function(metas, access) {
  57. if (!access.accessToDataDeserialized)
  58. return false;
  59. let metaKey = Object.keys(access.accessToDataDeserialized)[0];
  60. let meta = metas[metaKey]?.value;
  61. return meta && metaKey && meta == access.accessToDataDeserialized[metaKey];
  62. }
  63. this.accessType = ACCESS_GRANT.none;
  64. for (let i of accessList) {
  65. if (i.accessTo === ACCESS_TO.everything ||
  66. (i.accessTo === ACCESS_TO.item && i.accessToData === this.md5sum) ||
  67. (i.accessTo === ACCESS_TO.meta && checkMeta(this.meta, i)) ||
  68. (i.accessTo === ACCESS_TO.tag && checkTag([].concat(this.fixedTags, this.tags), i))) {
  69. if (i.grant === ACCESS_GRANT.write)
  70. return this.accessType = ACCESS_GRANT.write;
  71. this.accessType = ACCESS_GRANT.read;
  72. }
  73. }
  74. return this.accessType;
  75. }
  76. MediaStruct.prototype.HaveAccess = function(accessList) {
  77. return this.computeAccess(accessList) > 0;
  78. }
  79. async function buildAccessList(app, accessIds) {
  80. accessIds = Object.keys(accessIds || {}).reduce((acc, i) => {
  81. accessIds[i].linkId && acc.links.push(accessIds[i].linkId);
  82. accessIds[i].ldapDn && acc.ldap.push(accessIds[i].ldapDn);
  83. accessIds[i].email && acc.emails.push(accessIds[i].email);
  84. return acc;
  85. }, {links:[], emails: [], ldap: []});
  86. accessIds.accData = [].concat(accessIds.ldap, accessIds.emails, accessIds.links);
  87. accessIds.links = accessIds.links.map(x => '?').join(',');
  88. accessIds.emails = accessIds.emails.map(x => '?').join(',');
  89. accessIds.ldap = accessIds.ldap.map(x => '?').join(',');
  90. let accessList = (await app.databaseHelper.runSql(`select * from access where (
  91. (type=${ACCESS_TYPE.ldapAccount} AND typeData in (${accessIds.ldap})) OR
  92. (type=${ACCESS_TYPE.email} AND typeData in (${accessIds.emails})) OR
  93. (type=${ACCESS_TYPE.link} AND typeData in (${accessIds.links})) OR
  94. type=${ACCESS_TYPE.everyOne}
  95. )`, accessIds.accData)).map(data => {
  96. let result = new AccessModel;
  97. result.fromDb(data);
  98. return result;
  99. });
  100. return accessList || [];
  101. }
  102. function reduceReqToMediaStruct(acc, i) {
  103. let obj = acc[i.md5sum] = acc[i.md5sum] || new MediaStruct(i);
  104. obj.pushMeta(i.metaKey, i.metaValue);
  105. obj.pushTag(i.mediaTag, i.isFixedTag);
  106. return acc;
  107. }
  108. module.exports.fetchOne = async function(app, md5sum, accessList) {
  109. let result = ((await app.databaseHelper.runSql(`
  110. select mediaFile.path, mediaFile.md5sum, mediaFile.date,
  111. mediaMeta.key as metaKey, mediaMeta.value as metaValue,
  112. mediaTag.tag as mediaTag, mediaTag.fromMeta as isFixedTag
  113. from mediaFile
  114. left join mediaMeta on mediaMeta.md5sum=mediaFile.md5sum
  115. left join mediaTag on mediaTag.md5sum=mediaFile.md5sum
  116. where mediaFile.md5sum=?`, md5sum)) || []).reduce(reduceReqToMediaStruct, {})[md5sum] || null;
  117. accessList = await buildAccessList(app, accessList);
  118. return result?.HaveAccess(accessList) ? result : null;
  119. }
  120. module.exports.fetchMedias = async function(app, startTs, count) {
  121. let result = ((await app.databaseHelper.runSql(`
  122. select mediaFile.path, mediaFile.md5sum, mediaFile.date,
  123. mediaMeta.key as metaKey, mediaMeta.value as metaValue,
  124. mediaTag.tag as mediaTag, mediaTag.fromMeta as isFixedTag
  125. from mediaFile
  126. left join mediaMeta on mediaMeta.md5sum=mediaFile.md5sum
  127. left join mediaTag on mediaTag.md5sum=mediaFile.md5sum
  128. where mediaFile.md5sum in
  129. (select md5sum from mediaFile `
  130. +(startTs ? "where date <? " : "")
  131. +"order by date desc limit ?)", startTs ? [startTs, count] : [count])) || [])
  132. .reduce(reduceReqToMediaStruct, {});
  133. result = Object.keys(result).map(i => result[i]).sort((a, b) => b.date-a.date);
  134. return result;
  135. };
  136. module.exports.fetchMediasWithAccess = async function(app, startTs, count, access) {
  137. let result = [];
  138. let lastTs = startTs;
  139. access = await buildAccessList(app, access);
  140. while (result.length < count) {
  141. let tmp = await module.exports.fetchMedias(app, lastTs, 25);
  142. if (!tmp.length)
  143. return result;
  144. lastTs = tmp[tmp.length-1].date;
  145. tmp = tmp.filter(i => i.HaveAccess(access));
  146. if (tmp.length)
  147. result = result.concat(tmp);
  148. }
  149. return result.slice(0, count);
  150. };
  151. module.exports.getMediaRange = async function(app) {
  152. let result = await app.databaseHelper.runSql("select min(date) as _min, max(date) as _max from mediaFile");
  153. return [ result?.[0]?._min || 0, result?.[0]?._max || 0 ];
  154. }