mediaService.js 7.2 KB

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