pasteContent.js 2.0 KB

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