Advertisement
Guest User

pastebin.ts

a guest
Aug 13th, 2018
1,236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.07 KB | None | 0 0
  1. import * as vscode from 'vscode';
  2. import { ShareCodeClasses } from './sharecode';
  3. let rp = require('request-promise');
  4. let request = require("request"); //old
  5. let i18next = require('i18next');
  6. let parseString = require('xml2js').parseString;
  7.  
  8. export module pastebin
  9. {
  10. enum privacySelection
  11. {
  12. public,
  13. unlisted,
  14. private
  15. }
  16.  
  17. interface privacyQuickPick extends vscode.QuickPickItem
  18. {
  19. id: privacySelection
  20. }
  21.  
  22. enum expireDateSelection
  23. {
  24. never,
  25. tenMinutes,
  26. oneHour,
  27. oneDay,
  28. oneWeek,
  29. twoWeeks,
  30. oneMonth
  31. }
  32.  
  33. interface expireDateQuickPick extends vscode.QuickPickItem
  34. {
  35. id: expireDateSelection
  36. value: string
  37. }
  38.  
  39. export class pastebin implements ShareCodeClasses.Service
  40. {
  41. private configuration: ShareCodeClasses.Configuration
  42. private dateTimeHandler: ShareCodeClasses.dateTime
  43. private fileHandler: ShareCodeClasses.tmpFile
  44. private devKey: string = "c3f4c23b158eb018d5c7930417964ea8"
  45. private option: string = "paste"
  46. private privacy: number = 1
  47. private expireDate: string = "N"
  48. private possibleFormats: Array<string> = ['4cs', '6502acme', '6502kickass', '6502tasm', 'abap', 'actionscript', 'actionscript3', 'ada', 'aimms', 'algol68', 'apache', 'applescript', 'apt_sources', 'arm', 'asm', 'asp', 'asymptote', 'autoconf', 'autohotkey', 'autoit', 'avisynth', 'awk', 'bascomavr', 'bash', 'basic4gl', 'dos', 'bibtex', 'blitzbasic', 'b3d', 'bmx', 'bnf', 'boo', 'bf', 'c', 'c_winapi', 'c_mac', 'cil', 'csharp', 'cpp', 'cpp-winapi', 'cpp-qt', 'c_loadrunner', 'caddcl', 'cadlisp', 'cfdg', 'chaiscript', 'chapel', 'clojure', 'klonec', 'klonecpp', 'cmake', 'cobol', 'coffeescript', 'cfm', 'css', 'cuesheet', 'd', 'dart', 'dcl', 'dcpu16', 'dcs', 'delphi', 'oxygene', 'diff', 'div', 'dot', 'e', 'ezt', 'ecmascript', 'eiffel', 'email', 'epc', 'erlang', 'fsharp', 'falcon', 'fo', 'f1', 'fortran', 'freebasic', 'freeswitch', 'gambas', 'gml', 'gdb', 'genero', 'genie', 'gettext', 'go', 'groovy', 'gwbasic', 'haskell', 'haxe', 'hicest', 'hq9plus', 'html4strict', 'html5', 'icon', 'idl', 'ini', 'inno', 'intercal', 'io', 'ispfpanel', 'j', 'java', 'java5', 'javascript', 'jcl', 'jquery', 'json', 'julia', 'kixtart', 'latex', 'ldif', 'lb', 'lsl2', 'lisp', 'llvm', 'locobasic', 'logtalk', 'lolcode', 'lotusformulas', 'lotusscript', 'lscript', 'lua', 'm68k', 'magiksf', 'make', 'mapbasic', 'matlab', 'mirc', 'mmix', 'modula2', 'modula3', '68000devpac', 'mpasm', 'mxml', 'mysql', 'nagios', 'netrexx', 'newlisp', 'nginx', 'nimrod', 'text', 'nsis', 'oberon2', 'objeck', 'objc', 'ocaml-brief', 'ocaml', 'octave', 'pf', 'glsl', 'oobas', 'oracle11', 'oracle8', 'oz', 'parasail', 'parigp', 'pascal', 'pawn', 'pcre', 'per', 'perl', 'perl6', 'php', 'php-brief', 'pic16', 'pike', 'pixelbender', 'plsql', 'postgresql', 'postscript', 'povray', 'powershell', 'powerbuilder', 'proftpd', 'progress', 'prolog', 'properties', 'providex', 'puppet', 'purebasic', 'pycon', 'python', 'pys60', 'q', 'qbasic', 'qml', 'rsplus', 'racket', 'rails', 'rbs', 'rebol', 'reg', 'rexx', 'robots', 'rpmspec', 'ruby', 'gnuplot', 'rust', 'sas', 'scala', 'scheme', 'scilab', 'scl', 'sdlbasic', 'smalltalk', 'smarty', 'spark', 'sparql', 'sqf', 'sql', 'standardml', 'stonescript', 'sclang', 'swift', 'systemverilog', 'tsql', 'tcl', 'teraterm', 'thinbasic', 'typoscript', 'unicon', 'uscript', 'ups', 'urbi', 'vala', 'vbnet', 'vbscript', 'vedit', 'verilog', 'vhdl', 'vim', 'visualprolog', 'vb', 'visualfoxpro', 'whitespace', 'whois', 'winbatch', 'xbasic', 'xml', 'xorg_conf', 'xpp', 'yaml', 'z80', 'zxbasic']
  49. private anonym: boolean
  50.  
  51. constructor(anonym: boolean)
  52. {
  53. this.anonym = anonym
  54. this.configuration = new ShareCodeClasses.Configuration('pastebin')
  55. this.fileHandler = new ShareCodeClasses.tmpFile('pastebin')
  56. this.dateTimeHandler = new ShareCodeClasses.dateTime()
  57. }
  58.  
  59. public upload(fileToUpload: ShareCodeClasses.file): Promise<string>
  60. {
  61. fileToUpload.codeFormat = this.setCodeFormat(fileToUpload.codeFormat)
  62. if (this.isAnonym())
  63. {
  64. return this.uploadFile(fileToUpload)
  65. }
  66. else
  67. {
  68. return this.auth().then(() =>
  69. {
  70. return this.uploadFile(fileToUpload)
  71. });
  72. }
  73. }
  74.  
  75. public open(): Promise<string>
  76. {
  77. return new Promise(resolve =>
  78. {
  79. this.auth()
  80. .then(() =>
  81. {
  82. return rp.post({
  83. url: 'https://pastebin.com/api/api_post.php',
  84. formData: this.generateListFormData()
  85. })
  86. })
  87. .then(xml =>
  88. {
  89. try
  90. {
  91. parseString('<root>\n' + xml + '</root>\n', (err, result) =>
  92. {
  93. let pastes = result.root.paste.map(paste =>
  94. {
  95. let desc = paste.paste_title[0]
  96. if (desc == "")
  97. {
  98. desc = i18next.t("errors.noDescription")
  99. }
  100. return {
  101. label: desc,
  102. description: this.dateTimeHandler.getDateUnix(paste.paste_date[0]),
  103. pasteKey: paste.paste_key[0]
  104. }
  105. })
  106. vscode.window.showQuickPick(pastes).then((selected: any) =>
  107. {
  108. if (selected != undefined)
  109. {
  110. request.post({
  111. url: 'https://pastebin.com/api/api_raw.php',
  112. formData: this.generateGetFormData(selected.pasteKey)
  113. }, (err, httpResponse, raw) =>
  114. {
  115. this.fileHandler.saveFile(selected.pasteKey, selected.label, raw)
  116. vscode.workspace.openTextDocument(this.fileHandler.getFilePath(selected.pasteKey, selected.label))
  117. .then((doc: vscode.TextDocument) =>
  118. {
  119. vscode.window.showTextDocument(doc)
  120. resolve()
  121. });
  122. })
  123. }
  124. })
  125. });
  126. } catch (error)
  127. {
  128. //TODO: Errorhandling
  129. }
  130. })
  131. .catch(err =>
  132. {
  133. console.log(err)
  134. })
  135. })
  136. }
  137.  
  138. public isNotConfigured(): boolean
  139. {
  140. return this.configuration.get("authtoken") == null
  141. }
  142.  
  143. public isAnonym(): boolean
  144. {
  145. return this.anonym
  146. }
  147.  
  148. private getPrivacyQuickPickItems(): Array<privacyQuickPick>
  149. {
  150. return [
  151. {
  152. id: privacySelection.public,
  153. label: i18next.t("public"),
  154. description: ""
  155. }, {
  156. id: privacySelection.unlisted,
  157. label: i18next.t("unlisted"),
  158. description: ""
  159. }, {
  160. id: privacySelection.private,
  161. label: i18next.t("private"),
  162. description: ""
  163. }
  164. ]
  165. }
  166.  
  167. private getExpireDateQuickPickItems(): Array<expireDateQuickPick>
  168. {
  169. return [
  170. {
  171. id: expireDateSelection.never,
  172. label: i18next.t("never"),
  173. value: "N",
  174. description: ""
  175. }, {
  176. id: expireDateSelection.tenMinutes,
  177. label: i18next.t("minute", { count: 10 }),
  178. value: "10M",
  179. description: ""
  180. }, {
  181. id: expireDateSelection.oneHour,
  182. label: i18next.t("hour", { count: 1 }),
  183. value: "1H",
  184. description: ""
  185. }, {
  186. id: expireDateSelection.oneDay,
  187. label: i18next.t("day", { count: 1 }),
  188. value: "1D",
  189. description: ""
  190. }, {
  191. id: expireDateSelection.oneWeek,
  192. label: i18next.t("week", { count: 1 }),
  193. value: "1W",
  194. description: ""
  195. }, {
  196. id: expireDateSelection.twoWeeks,
  197. label: i18next.t("week", { count: 2 }),
  198. value: "2W",
  199. description: ""
  200. }, {
  201. id: expireDateSelection.oneMonth,
  202. label: i18next.t("month", { count: 1 }),
  203. value: "1M",
  204. description: ""
  205. }
  206. ]
  207. }
  208.  
  209. private auth(): Promise<void>
  210. {
  211. let userNameG, userPass;
  212. if (this.isNotConfigured())
  213. {
  214. return Promise.resolve(
  215. vscode.window.showInputBox({
  216. placeHolder: i18next.t("pastebin.username"),
  217. value: this.configuration.get("username")
  218. })
  219. .then((userName: string) =>
  220. {
  221. userNameG = userName
  222. return this.configuration.set('username', userName)
  223. })
  224. .then(() =>
  225. {
  226. return vscode.window.showInputBox({
  227. placeHolder: i18next.t("pastebin.password"),
  228. password: true
  229. })
  230. })
  231. .then((userPass: string) =>
  232. {
  233. return rp.post({
  234. url: 'https://pastebin.com/api/api_login.php',
  235. formData: {
  236. api_dev_key: this.devKey,
  237. api_user_name: userNameG,
  238. api_user_password: userPass
  239. }
  240. })
  241. // , (err, httpResponse, body: string) =>
  242. // {
  243. // if (body.startsWith("Bad API request"))
  244. // {
  245. // return Promise.reject(body.split('Bad API request, ').pop())
  246. // } else
  247. // {
  248. // return this.configuration.set('authtoken', body)
  249. // }
  250. // });
  251. })
  252. .then(body =>
  253. {
  254. if (body.startsWith("Bad API request"))
  255. {
  256. return Promise.reject(body.split('Bad API request, ').pop())
  257. } else
  258. {
  259. return this.configuration.set('authtoken', body)
  260. }
  261. })
  262. .then(text =>
  263. {
  264. console.log(this.configuration.get('authtoken'))
  265. return null
  266. })
  267. // .catch(err =>
  268. // {
  269. // vscode.window.showErrorMessage(err)
  270. // })
  271. )
  272. }
  273. else
  274. {
  275. return Promise.resolve()
  276. }
  277. }
  278.  
  279. private setCodeFormat(codeFormat: string): string
  280. {
  281. if (this.possibleFormats.lastIndexOf(codeFormat) >= 0)
  282. {
  283. return codeFormat
  284. }
  285. return null
  286. }
  287.  
  288. private generateUploadFormData(fileToUpload: ShareCodeClasses.file): Object
  289. {
  290. var data = {
  291. api_dev_key: this.devKey,
  292. api_option: 'paste',
  293. api_paste_private: this.privacy,
  294. api_paste_expire_date: this.expireDate,
  295. api_paste_name: fileToUpload.fileName,
  296. api_paste_code: fileToUpload.code
  297. }
  298. if (fileToUpload.codeFormat != null)
  299. {
  300. data['api_paste_format'] = fileToUpload.codeFormat
  301. }
  302. if (this.configuration.get('authtoken') != null)
  303. data['api_user_key'] = this.configuration.get('authtoken')
  304. return data
  305. }
  306.  
  307. private uploadFile(fileToUpload: ShareCodeClasses.file): Promise<string>
  308. {
  309. return new Promise<string>((resolve) =>
  310. {
  311. vscode.window.showQuickPick(this.getPrivacyQuickPickItems(), {
  312. placeHolder: i18next.t('pastebin.choosePrivacy')
  313. }
  314. ).then(
  315. (privacy: privacyQuickPick) =>
  316. {
  317. this.privacy = privacy.id
  318. }).then(
  319. () =>
  320. {
  321. vscode.window.showQuickPick(this.getExpireDateQuickPickItems(), {
  322. placeHolder: i18next.t('pastebin.chooseExpireDate')
  323. }).then(
  324. (expire: expireDateQuickPick) =>
  325. {
  326. this.expireDate = expire.value
  327. }).then(
  328. () =>
  329. {
  330. request.post({
  331. url: 'https://pastebin.com/api/api_post.php',
  332. formData: this.generateUploadFormData(fileToUpload)
  333. }, (err, httpResponse, body) =>
  334. {
  335. console.log(body)
  336. resolve(body);
  337. })
  338. })
  339. })
  340. })
  341. }
  342.  
  343. private generateListFormData(): Object
  344. {
  345. var data = {
  346. api_dev_key: this.devKey,
  347. api_user_key: this.configuration.get('authtoken'),
  348. api_option: 'list',
  349. api_results_limit: 50
  350. }
  351. return data
  352. }
  353.  
  354. private generateGetFormData(pasteKey: string): Object
  355. {
  356. var data = {
  357. api_dev_key: this.devKey,
  358. api_user_key: this.configuration.get('authtoken'),
  359. api_option: 'show_paste',
  360. api_paste_key: pasteKey
  361. }
  362. return data
  363. }
  364. }
  365. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement