mediaService.js 7.2 KB

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