Advertisement
lignite0

SP24 - image deserialize node.js

Jul 19th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const base = require('base-x');
  2.  
  3. const baser62 = base("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
  4.  
  5. const min = 0x00000000;
  6. const max = 0xffffffff;
  7.  
  8. function getRandomInt() {
  9.     return Math.floor(Math.random() * (max - min)) + min;
  10. }
  11.  
  12. class BufferOffset {
  13.     constructor() {
  14.         this.size = 0;
  15.         this.data = [];
  16.     }
  17.  
  18.     write(length, data) {
  19.         this.data.push({length, data});
  20.         this.size += length;
  21.     }
  22.  
  23.     make() {
  24.         let offset = 0;
  25.         const buffer = Buffer.allocUnsafe(this.size);
  26.         for (let {data, length} of this.data) {
  27.             buffer.writeUIntBE(data, offset, length);
  28.             offset += length;
  29.         }
  30.  
  31.         return buffer;
  32.     }
  33. }
  34.  
  35. // b.write(4, getRandomInt()); // Random
  36. // b.write(4, Date.now() / 1000); // Creation timestamp
  37. // b.write(1, 0x55); // Object type
  38. // b.write(1, 0x44); // Object localization
  39. // b.write(3, 0x333333); // Application Id
  40. // b.write(2, 0x1111); // Image width
  41. // b.write(2, 0x2222); // Image height
  42. // b.write(2, 0x6666); // Provider Id
  43. // b.write(4, 0x77777777); // Product creation timestamp
  44. // b.write(5, 0x8888888888); // Product random
  45. // b.write(3, 0x999999); // Product counter
  46. // b.write(4, getRandomInt()); // Original image random
  47. // b.write(4, Date.now() / 1000); // original image timestamp
  48. // b.write(2, 0x2222); // Original image width
  49. // b.write(2, 0x6666); // Original image height
  50.  
  51. // b.write(1, 0x11); // Object type
  52. // b.write(1, 0x22); // Object localization
  53. //
  54. // b.write(4, Date.now() / 1000); // Creation timestamp
  55. // b.write(4, getRandomInt()); // Random
  56. // b.write(4, getRandomInt()); // Couter
  57. //
  58. // b.write(2, 0x2222); // Original image width
  59. // b.write(2, 0x6666); // Original image height
  60. //
  61. // b.write(1, 0x11); // Object type
  62. // b.write(1, 0x22); // Object localization
  63. //
  64. // b.write(4, Date.now() / 1000); // Creation timestamp
  65. // b.write(4, getRandomInt()); // Random
  66. // b.write(4, getRandomInt()); // Couter
  67. //
  68. // b.write(3, 0x333333); // Application Id
  69. // b.write(2, 0x4444); // Provider Id
  70. // b.write(2, 0x5555); // Image width
  71. // b.write(2, 0x6666); // Image height
  72. //
  73. // b.write(4, 0x77777777); // Product creation timestamp
  74. // b.write(4, 0x88888888); // Product random
  75. // b.write(4, 0x99999999); // Product counter
  76.  
  77. // const b = new BufferOffset();
  78. // b.write(2, 0x1111); // Object type
  79. // b.write(2, 0x2222); // Object localization
  80. // b.write(2, 0x4444); // Wholesaler Id
  81. // b.write(2, 0x5555); // Image width
  82. // b.write(2, 0x6666); // Image height
  83. // b.write(4, 0x21232f29); // Image checksum
  84. // b.write(4, 0x7a57a5a7); // Image checksum
  85. // b.write(4, 0x43894a0e); // Image checksum
  86. // b.write(4, 0x4a801fc3); // Image checksum
  87. // const buffer = b.make();
  88.  
  89.  
  90. const b = new BufferOffset();
  91.  
  92. b.write(1, 0x11); // Object type
  93. // b.write(2, 0x5555); // Thumb image width
  94. // b.write(2, 0x6666); // Thumb image height
  95. b.write(2, 0x2222); // Object localization
  96. b.write(2, 0x4444); // Wholesaler Id
  97. b.write(2, 0x5555); // Image width
  98. b.write(2, 0x6666); // Image height
  99. b.write(4, 0x21232f29); // Image checksum
  100. b.write(4, 0x7a57a5a7); // Image checksum
  101. b.write(4, 0x43894a0e); // Image checksum
  102. b.write(4, 0x4a801fc3); // Image checksum
  103. b.write(1, 0x11); // File extension
  104. const buffer = b.make();
  105.  
  106. // 21232f29 7a57a5a7 43894a0e 4a801fc3
  107. // d033e22ae348aeb5660fc2140aec35850c4da997
  108.  
  109. console.log(b.size);
  110. console.log(buffer);
  111. const result = baser62.encode(buffer);
  112. console.log(result.length);
  113. console.log(result);
  114.  
  115. const wholesale_yns_products = {
  116.     images: [
  117.         {
  118.             _id: "4a801fc343894a0e7a57a5a7",
  119.         }
  120.     ]
  121. };
  122.  
  123. const app_products = {
  124.     images: [
  125.         {
  126.             _id: "4a801fc343894a0e7a57a5a7",
  127.         }
  128.     ]
  129. };
  130.  
  131. const app_images = {
  132.     _id: "4a801fc343894a0e7a57a5a7",
  133.     kind: "storage",
  134.     primary: {
  135.         bucket: "bsB2dbx82Ao8e128eKcSF6CPPitiPXZF",
  136.     },
  137.     thumbs: [
  138.         {
  139.             bucket: "eM5mjMfPXL8nJFkgNgdVj7uaijFrL8YcxQKHVeZt",
  140.         },
  141.         {
  142.             bucket: "eM5mjMfPXL8nJFkgNgdVj7uaijFrL8YcxQKHVeZt",
  143.         },
  144.     ],
  145.     source: {
  146.         id: 101,
  147.         kind: "wholesaler",
  148.         url: "https://yns.pl/id.jpg"
  149.     },
  150. };
  151.  
  152. const deserialized = {
  153.     type: 0x00,
  154.     kind: "wholesaler-product-image",
  155.     storage: {
  156.         localization: "waw1",
  157.         auth: "d40b57d4f91d4d1fb3722913c1d03c9b"
  158.     },
  159.     source: {
  160.         id: 101,
  161.         type: "wholesaler",
  162.     },
  163.     url: "https://storage.waw1.cloud.ovh.net/v1//ws0064/wpi/eM5mjMfPXL8nJFkgNgdVj7uaijFrL8YcxQKHVeZt.png",
  164.     bucket: "eM5mjMfPXL8nJFkgNgdVj7uaijFrL8YcxQKHVeZt",
  165.     width: 1600,
  166.     height: 1200,
  167.     checksum: "21232f297a57a5a743894a0e4a801fc3"
  168. };
  169.  
  170. const deserialized_thumb = {
  171.     type: 0x00,
  172.     kind: "wholesaler-product-image-thumb",
  173.     storage: {
  174.         localization: "waw1",
  175.         auth: "d40b57d4f91d4d1fb3722913c1d03c9b"
  176.     },
  177.     source: {
  178.         id: 101,
  179.         type: "wholesaler",
  180.     },
  181.     url: "https://storage.waw1.cloud.ovh.net/v1//ws0064/wpi/eM5mjMfPXL8nJFkgNgdVj7uaijFrL8YcxQKHVeZt.png",
  182.     bucket: "eM5mjMfPXL8nJFkgNgdVj7uaijFrL8YcxQKHVeZt",
  183.     width: 1600,
  184.     height: 1200,
  185.     checksum: "21232f297a57a5a743894a0e4a801fc3"
  186. };
  187.  
  188.  
  189. // 0x00 = null
  190. // 0x10 = null
  191. // 0x01 = static images created by developers
  192. // 0x11 = static images thumbs created by developers
  193. // 0x02 = static images created by client
  194. // 0x12 = static images thumbs by client
  195. // 0x03 = example products images
  196. // 0x13 = example products thumbs
  197. // 0x04 = products images
  198. // 0x14 = products thumbs
  199. // 0x15 = integrations images
  200. // 0x15 = integrations thumbs
  201. // 0x16 = integrations images
  202. // 0x16 = integrations thumbs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement