pasteContent.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. const DatabaseModel = require("./DatabaseModel.js").DatabaseModel;
  2. const mCrypto = require('../src/crypto.js');
  3. function PasteContent(privId, type) {
  4. this.created = new Date();
  5. this.apiKey = null;
  6. this.privId = privId;
  7. this.publicId = privId ? mCrypto.publicKey(privId) : null;
  8. this.type = type;
  9. this.expire = this.created;
  10. this.expired = false;
  11. this.data = null;
  12. this.renew();
  13. }
  14. Object.setPrototypeOf(PasteContent.prototype, DatabaseModel.prototype);
  15. PasteContent.prototype.renew = function() {
  16. if (this.expired)
  17. return;
  18. this.expire = new Date();
  19. this.expire.setMonth(this.expire.getMonth() + 6);
  20. }
  21. PasteContent.prototype.getTableName = function() {
  22. return "pasteContent";
  23. }
  24. PasteContent.prototype.createOrUpdateBase = async function(dbHelper) {
  25. await dbHelper.runSql(`CREATE TABLE IF NOT EXISTS 'pasteContent' (
  26. privId string not null PRIMARY KEY,
  27. publicId string not null,
  28. created datetime not null,
  29. expire datetime,
  30. type string not null,
  31. expired boolean not null,
  32. apiKey string,
  33. data string
  34. )`);
  35. }
  36. PasteContent.prototype.describe = function() {
  37. return {
  38. "privId": this.privId,
  39. "publicId": this.publicId,
  40. "created": this.created.getTime(),
  41. "expire": this.expire?.getTime(),
  42. "type": this.type,
  43. "apiKey": this.apiKey,
  44. "expired": this.expired,
  45. "data": this.data
  46. };
  47. }
  48. PasteContent.prototype.fromDb = function(dbObj) {
  49. this.privId = dbObj['privId'];
  50. this.publicId = dbObj['publicId'];
  51. this.created = new Date(dbObj['created']);
  52. this.expire = dbObj['expire'] ? new Date(dbObj['expire']) : null;
  53. this.apiKey = dbObj['apiKey'];
  54. this.type = dbObj['type'];
  55. this.expired = dbObj['expired'];
  56. this.data = dbObj['data'];
  57. }
  58. module.exports.PasteContent = PasteContent;