Source

lib/asset/uploadAnimation.js

  1. // Includes
  2. const http = require('../util/http.js').func
  3. const getGeneralToken = require('../util/getGeneralToken').func
  4. const configureItem = require('../develop/configureItem.js').func
  5. // Args
  6. exports.required = ['data']
  7. exports.optional = ['itemOptions', 'assetId', 'jar']
  8. // Docs
  9. /**
  10. * 🔐 Upload an animation, either as a new asset or by overwriting an existing animation.
  11. * @category Asset
  12. * @alias uploadAnimation
  13. * @param {string | Stream} data - The rbxm file containing the KeyframeSequence.
  14. * @param {object=} itemOptions - The options for the upload. Only optional if assetId is not provided.
  15. * @param {string=} itemOptions.name - The name of the animation.
  16. * @param {string=} itemOptions.description - The description for the animation.
  17. * @param {boolean=} itemOptions.copyLocked - If the animation is copy-locked.
  18. * @param {boolean=} itemOptions.allowComments - If comments are allowed.
  19. * @param {number=} itemOptions.groupId - The group to upload the animation to. This is ignored if the assetId is provided.
  20. * @param {number=} assetId - An existing assetId to overwrite.
  21. * @returns {Promise<number>}
  22. * @example const noblox = require("noblox.js")
  23. * const fs = require("fs")
  24. * // Login using your cookie
  25. * const assetId = await noblox.uploadAnimation(fs.readFileSync("./KeyframeSequence.rbxm"), {
  26. * name: "A cool animation",
  27. * description: "This is a very cool animation",
  28. * copyLocked: false, //The asset is allowed to be copied.
  29. * allowComments: false
  30. * }, 7132858975)
  31. **/
  32. // Define
  33. function upload (data, itemOptions, assetId, jar, token) {
  34. const httpOpt = {
  35. url: assetId ? `//www.roblox.com/ide/publish/uploadexistinganimation?assetID=${assetId}&isGamesAsset=False` : '//www.roblox.com/ide/publish/uploadnewanimation?AllID=1',
  36. options: {
  37. resolveWithFullResponse: true,
  38. method: 'POST',
  39. jar,
  40. body: data,
  41. headers: {
  42. 'X-CSRF-TOKEN': token,
  43. 'Content-Type': 'application/xml',
  44. 'User-Agent': 'RobloxStudio/WinInet RobloxApp/0.483.1.425021 (GlobalDist; RobloxDirectDownload)'
  45. }
  46. }
  47. }
  48. if (!assetId && itemOptions) {
  49. const copyLocked = itemOptions.copyLocked
  50. const allowComments = itemOptions.allowComments
  51. httpOpt.url += '&assetTypeName=Animation&genreTypeId=1&name=' +
  52. itemOptions.name +
  53. '&description=' +
  54. (itemOptions.description || '') +
  55. '&ispublic=' +
  56. (copyLocked != null ? !copyLocked : false) +
  57. '&allowComments=' +
  58. (allowComments != null ? allowComments : true) +
  59. '&groupId=' +
  60. (itemOptions.groupId || '')
  61. } else if (!assetId) {
  62. throw new Error('ItemOptions is required for new assets.')
  63. }
  64. return http(httpOpt)
  65. .then(function (res) {
  66. if (res.statusCode === 200) {
  67. const resultId = assetId || Number(res.body)
  68. if (assetId && itemOptions) {
  69. const copyLocked = itemOptions.copyLocked
  70. const allowComments = itemOptions.allowComments
  71. return configureItem({
  72. id: resultId,
  73. name: itemOptions.name,
  74. description: itemOptions.description,
  75. enableComments: (allowComments != null ? allowComments : true),
  76. sellForRobux: (copyLocked != null ? !copyLocked : false)
  77. }).then(function () {
  78. return resultId
  79. })
  80. } else {
  81. return resultId
  82. }
  83. } else {
  84. throw new Error('Animation upload failed, confirm that all item options, asset options, and upload data are valid.')
  85. }
  86. })
  87. }
  88. exports.func = function (args) {
  89. const jar = args.jar
  90. return getGeneralToken({
  91. jar
  92. }).then(function (token) {
  93. return upload(args.data, args.itemOptions, args.assetId, args.jar, token)
  94. })
  95. }