upload_file.mjs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import minimist from 'minimist'
  2. import fs from 'node:fs/promises'
  3. import Path from 'node:path'
  4. import readline from 'node:readline/promises'
  5. import { stdin as input, stdout as output } from 'node:process'
  6. import { ObjectId } from '../app/src/infrastructure/mongodb.mjs'
  7. import Errors from '../app/src/Features/Errors/Errors.js'
  8. import ProjectLocator from '../app/src/Features/Project/ProjectLocator.mjs'
  9. import ProjectEntityUpdateHandler from '../app/src/Features/Project/ProjectEntityUpdateHandler.mjs'
  10. import SafePath from '../app/src/Features/Project/SafePath.mjs'
  11. import { scriptRunner } from './lib/ScriptRunner.mjs'
  12. function usage() {
  13. console.error(`Upload a local file into a project path
  14. Usage: node scripts/upload_file.mjs [options] FILE
  15. Required:
  16. FILE Local filesystem path to file to upload
  17. --project-id ID Project id
  18. --user-id ID User id performing the action
  19. Optional:
  20. --dest PATH Destination project path (default: basename of FILE)
  21. --source VALUE Source label for history
  22. (default: script-upload-file)
  23. --force Allow overwrite when destination already exists
  24. -y Skip interactive confirmation prompt
  25. --dry-run Show what would happen without mutating the project
  26. --help Show this help
  27. Example:
  28. node scripts/upload_file.mjs /tmp/plot.png --project-id=... --user-id=... \
  29. --dest=/figures/plot.png --dry-run`)
  30. }
  31. function parseArgs() {
  32. let unknownArg
  33. const argv = minimist(process.argv.slice(2), {
  34. boolean: ['dry-run', 'force', 'help', 'y'],
  35. string: ['project-id', 'user-id', 'dest', 'source'],
  36. alias: { y: 'yes' },
  37. unknown: arg => {
  38. if (arg.startsWith('-')) {
  39. unknownArg = arg
  40. return false
  41. }
  42. return true
  43. },
  44. })
  45. if (unknownArg) {
  46. throw new Error(`unknown argument: ${unknownArg}`)
  47. }
  48. if (argv._.length === 0) {
  49. throw new Error('provide a local file path as FILE argument')
  50. }
  51. if (argv._.length > 1) {
  52. throw new Error('only one FILE argument is supported')
  53. }
  54. return {
  55. localPath: argv._[0],
  56. projectId: argv['project-id'],
  57. userId: argv['user-id'],
  58. destPath: argv.dest,
  59. source: argv.source || 'script-upload-file',
  60. force: argv.force === true,
  61. dryRun: argv['dry-run'] === true,
  62. assumeYes: argv.y === true || argv.yes === true,
  63. help: argv.help === true,
  64. }
  65. }
  66. function normalizeTargetPath(targetPath) {
  67. if (typeof targetPath !== 'string') {
  68. throw new TypeError('destination path must be a string')
  69. }
  70. if (targetPath.trim().length === 0) {
  71. throw new Error('destination path must not be empty')
  72. }
  73. return targetPath.startsWith('/') ? targetPath : `/${targetPath}`
  74. }
  75. async function validateInputs(opts) {
  76. const { projectId, userId, localPath } = opts
  77. if (!projectId || !ObjectId.isValid(projectId)) {
  78. throw new Error('provide a valid object id as --project-id')
  79. }
  80. if (!userId || !ObjectId.isValid(userId)) {
  81. throw new Error('provide a valid object id as --user-id')
  82. }
  83. if (!localPath || typeof localPath !== 'string') {
  84. throw new Error('provide a local file path as FILE argument')
  85. }
  86. let fileStat
  87. try {
  88. fileStat = await fs.stat(localPath)
  89. } catch (error) {
  90. throw new Error(`local file not found: ${localPath}`)
  91. }
  92. if (!fileStat.isFile()) {
  93. throw new Error(`local path is not a file: ${localPath}`)
  94. }
  95. const rawDestPath = opts.destPath ?? Path.basename(localPath)
  96. let targetPath
  97. try {
  98. targetPath = normalizeTargetPath(rawDestPath)
  99. } catch (error) {
  100. const invalidValue =
  101. opts.destPath !== undefined
  102. ? `--dest=${JSON.stringify(opts.destPath)}`
  103. : `derived basename ${rawDestPath} from FILE ${localPath}`
  104. throw new Error(
  105. `provide a non-empty destination project path; invalid value ${invalidValue}`
  106. )
  107. }
  108. if (!SafePath.isCleanPath(targetPath)) {
  109. throw new Errors.InvalidNameError('invalid --dest value')
  110. }
  111. const fileName = Path.posix.basename(targetPath)
  112. if (!fileName || fileName === '.' || fileName === '..') {
  113. throw new Error('destination path must include a file name')
  114. }
  115. return { ...opts, targetPath }
  116. }
  117. async function confirmUpload(projectId, localPath, targetPath, assumeYes) {
  118. if (assumeYes) {
  119. return true
  120. }
  121. const rl = readline.createInterface({ input, output })
  122. try {
  123. const answer = await rl.question(
  124. `Upload ${localPath} to ${targetPath} in project ${projectId}? [y/N] `
  125. )
  126. return /^y(es)?$/i.test(answer.trim())
  127. } finally {
  128. rl.close()
  129. }
  130. }
  131. async function getExistingEntity(projectId, targetPath) {
  132. try {
  133. return await ProjectLocator.promises.findElementByPath({
  134. project_id: projectId,
  135. path: targetPath,
  136. exactCaseMatch: true,
  137. })
  138. } catch (error) {
  139. if (error instanceof Errors.NotFoundError) {
  140. return null
  141. }
  142. throw error
  143. }
  144. }
  145. async function main(trackProgress) {
  146. let opts = parseArgs()
  147. if (opts.help) {
  148. usage()
  149. return
  150. }
  151. opts = await validateInputs(opts)
  152. const {
  153. projectId,
  154. userId,
  155. targetPath,
  156. localPath,
  157. source,
  158. force,
  159. dryRun,
  160. assumeYes,
  161. } = opts
  162. await trackProgress(
  163. `Starting upload for project=${projectId} path=${targetPath} dryRun=${dryRun} force=${force}`
  164. )
  165. const existing = await getExistingEntity(projectId, targetPath)
  166. if (existing?.type === 'folder') {
  167. throw new Error(
  168. `destination is a folder at ${targetPath}. Choose a file path within that folder.`
  169. )
  170. }
  171. if (existing && !force) {
  172. throw new Error(
  173. `destination already exists at ${targetPath} (type=${existing.type}). Re-run with --force to overwrite.`
  174. )
  175. }
  176. const intendedAction = existing ? 'overwrite' : 'create'
  177. console.log(
  178. `${dryRun ? 'DRY RUN: would' : 'Applying: will'} ${intendedAction} file at ${targetPath}`
  179. )
  180. if (dryRun) {
  181. return
  182. }
  183. const confirmed = await confirmUpload(
  184. projectId,
  185. localPath,
  186. targetPath,
  187. assumeYes
  188. )
  189. if (!confirmed) {
  190. console.log('Upload cancelled.')
  191. return
  192. }
  193. const { fileRef, isNew } =
  194. await ProjectEntityUpdateHandler.promises.upsertFileWithPath(
  195. projectId,
  196. targetPath,
  197. localPath,
  198. null,
  199. userId,
  200. source
  201. )
  202. const outcome = existing
  203. ? `overwrote existing ${existing.type}`
  204. : isNew
  205. ? 'created file'
  206. : 'updated existing file'
  207. console.log(
  208. `Success: ${outcome}. fileId=${fileRef?._id} fileName=${fileRef?.name} projectId=${projectId} path=${targetPath}`
  209. )
  210. }
  211. try {
  212. await scriptRunner(main)
  213. console.log('Done.')
  214. process.exit(0)
  215. } catch (error) {
  216. if (error instanceof Errors.InvalidNameError) {
  217. console.error(`Invalid name/path: ${error.message}`)
  218. } else {
  219. console.error(error)
  220. }
  221. usage()
  222. process.exit(1)
  223. }