diff --git a/.gitignore b/.gitignore index 6ad65d7..3d583a1 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ /dist/ /docs/ .idea/* +/upload/ .DS_Store coverage diff --git a/package-lock.json b/package-lock.json index bccaebc..ed0163f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19,7 +19,9 @@ "got": "13.0.0", "http-status-codes": "2.3.0", "inversify": "6.0.1", + "mime-types": "2.1.35", "mongoose": "7.5.3", + "multer": "1.4.5-lts.1", "pino": "8.15.1", "reflect-metadata": "0.1.13" }, @@ -27,6 +29,8 @@ "@types/convict": "6.1.4", "@types/convict-format-with-validator": "6.0.3", "@types/express": "4.17.18", + "@types/mime-types": "2.1.2", + "@types/multer": "1.4.8", "@types/node": "18.17.15", "@typescript-eslint/eslint-plugin": "6.7.0", "@typescript-eslint/parser": "6.7.0", @@ -921,6 +925,23 @@ "dev": true, "license": "MIT" }, + "node_modules/@types/mime-types": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/@types/mime-types/-/mime-types-2.1.2.tgz", + "integrity": "sha512-q9QGHMGCiBJCHEvd4ZLdasdqXv570agPsUW0CeIm/B8DzhxsYMerD0l3IlI+EQ1A2RWHY2mmM9x1YIuuWxisCg==", + "dev": true, + "license": "MIT" + }, + "node_modules/@types/multer": { + "version": "1.4.8", + "resolved": "https://registry.npmjs.org/@types/multer/-/multer-1.4.8.tgz", + "integrity": "sha512-VMZOW6mnmMMhA5m3fsCdXBwFwC+a+27/8gctNMuQC4f7UtWcF79KAFGoIfKZ4iqrElgWIa3j5vhMJDp0iikQ1g==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/express": "*" + } + }, "node_modules/@types/node": { "version": "18.17.15", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.17.15.tgz", @@ -1296,6 +1317,12 @@ "node": ">= 8" } }, + "node_modules/append-field": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/append-field/-/append-field-1.0.0.tgz", + "integrity": "sha512-klpgFSWLW1ZEs8svjfb7g4qWY0YS5imI82dTg+QahUvJ8YqAY0P10Uk8tTyh9ZGuYEZEMaeJYCF5BFuX552hsw==", + "license": "MIT" + }, "node_modules/arg": { "version": "4.1.3", "resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz", @@ -1630,6 +1657,12 @@ "ieee754": "^1.2.1" } }, + "node_modules/buffer-from": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", + "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==", + "license": "MIT" + }, "node_modules/builtin-modules": { "version": "3.3.0", "resolved": "https://registry.npmjs.org/builtin-modules/-/builtin-modules-3.3.0.tgz", @@ -1652,6 +1685,17 @@ "semver": "^7.0.0" } }, + "node_modules/busboy": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/busboy/-/busboy-1.6.0.tgz", + "integrity": "sha512-8SFQbg/0hQ9xy3UNTB0YEnsNBbWfhf7RtnzpL7TkBiTBRfrQ9Fxcnz7VJsleJpyp6rVLvXiuORqjlHi5q+PYuA==", + "dependencies": { + "streamsearch": "^1.1.0" + }, + "engines": { + "node": ">=10.16.0" + } + }, "node_modules/bytes": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz", @@ -1968,6 +2012,51 @@ "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", "dev": true }, + "node_modules/concat-stream": { + "version": "1.6.2", + "resolved": "https://registry.npmjs.org/concat-stream/-/concat-stream-1.6.2.tgz", + "integrity": "sha512-27HBghJxjiZtIk3Ycvn/4kbJk/1uZuJFfuPEns6LaEvpvG1f0hTea8lilrouyo9mVc2GWdcEZ8OLoGmSADlrCw==", + "engines": [ + "node >= 0.8" + ], + "license": "MIT", + "dependencies": { + "buffer-from": "^1.0.0", + "inherits": "^2.0.3", + "readable-stream": "^2.2.2", + "typedarray": "^0.0.6" + } + }, + "node_modules/concat-stream/node_modules/isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==", + "license": "MIT" + }, + "node_modules/concat-stream/node_modules/readable-stream": { + "version": "2.3.8", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.8.tgz", + "integrity": "sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA==", + "license": "MIT", + "dependencies": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, + "node_modules/concat-stream/node_modules/string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "license": "MIT", + "dependencies": { + "safe-buffer": "~5.1.0" + } + }, "node_modules/connect-pause": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/connect-pause/-/connect-pause-0.1.1.tgz", @@ -2068,6 +2157,12 @@ "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==", "license": "MIT" }, + "node_modules/core-util-is": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz", + "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==", + "license": "MIT" + }, "node_modules/cors": { "version": "2.8.5", "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", @@ -5167,7 +5262,6 @@ "version": "1.2.8", "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz", "integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==", - "dev": true, "license": "MIT", "funding": { "url": "https://github.com/sponsors/ljharb" @@ -5182,6 +5276,18 @@ "node": ">=16 || 14 >=14.17" } }, + "node_modules/mkdirp": { + "version": "0.5.6", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.6.tgz", + "integrity": "sha512-FP+p8RB8OWpF3YZBCrP5gtADmtXApB5AMLn+vdyA+PyxCjrCs00mjyUozssO33cwDeT3wNGdLxJ5M//YqtHAJw==", + "license": "MIT", + "dependencies": { + "minimist": "^1.2.6" + }, + "bin": { + "mkdirp": "bin/cmd.js" + } + }, "node_modules/mongodb": { "version": "5.8.1", "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-5.8.1.tgz", @@ -5334,6 +5440,24 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, + "node_modules/multer": { + "version": "1.4.5-lts.1", + "resolved": "https://registry.npmjs.org/multer/-/multer-1.4.5-lts.1.tgz", + "integrity": "sha512-ywPWvcDMeH+z9gQq5qYHCCy+ethsk4goepZ45GLD63fOu0YcNecQxi64nDs3qluZB+murG3/D4dJ7+dGctcCQQ==", + "license": "MIT", + "dependencies": { + "append-field": "^1.0.0", + "busboy": "^1.0.0", + "concat-stream": "^1.5.2", + "mkdirp": "^0.5.4", + "object-assign": "^4.1.1", + "type-is": "^1.6.4", + "xtend": "^4.0.0" + }, + "engines": { + "node": ">= 6.0.0" + } + }, "node_modules/nanoid": { "version": "3.3.7", "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.7.tgz", @@ -5477,7 +5601,6 @@ "version": "4.1.1", "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", "integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==", - "dev": true, "engines": { "node": ">=0.10.0" } @@ -6031,6 +6154,12 @@ "node": ">= 0.6.0" } }, + "node_modules/process-nextick-args": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", + "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==", + "license": "MIT" + }, "node_modules/process-warning": { "version": "2.3.2", "resolved": "https://registry.npmjs.org/process-warning/-/process-warning-2.3.2.tgz", @@ -6519,7 +6648,6 @@ "version": "5.1.2", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", - "dev": true, "license": "MIT" }, "node_modules/safe-regex": { @@ -6949,6 +7077,14 @@ "graceful-fs": "^4.1.3" } }, + "node_modules/streamsearch": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-1.1.0.tgz", + "integrity": "sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==", + "engines": { + "node": ">=10.0.0" + } + }, "node_modules/string_decoder": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", @@ -7462,6 +7598,12 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/typedarray": { + "version": "0.0.6", + "resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz", + "integrity": "sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==", + "license": "MIT" + }, "node_modules/typescript": { "version": "5.2.2", "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.2.2.tgz", @@ -7519,7 +7661,6 @@ "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==", - "dev": true, "license": "MIT" }, "node_modules/utils-merge": { @@ -7758,6 +7899,15 @@ "node": ">=8" } }, + "node_modules/xtend": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", + "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==", + "license": "MIT", + "engines": { + "node": ">=0.4" + } + }, "node_modules/y18n": { "version": "5.0.8", "resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz", @@ -8402,6 +8552,21 @@ "integrity": "sha512-/pyBZWSLD2n0dcHE3hq8s8ZvcETHtEuF+3E7XVt0Ig2nvsVQXdghHVcEkIWjy9A0wKfTn97a/PSDYohKIlnP/w==", "dev": true }, + "@types/mime-types": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/@types/mime-types/-/mime-types-2.1.2.tgz", + "integrity": "sha512-q9QGHMGCiBJCHEvd4ZLdasdqXv570agPsUW0CeIm/B8DzhxsYMerD0l3IlI+EQ1A2RWHY2mmM9x1YIuuWxisCg==", + "dev": true + }, + "@types/multer": { + "version": "1.4.8", + "resolved": "https://registry.npmjs.org/@types/multer/-/multer-1.4.8.tgz", + "integrity": "sha512-VMZOW6mnmMMhA5m3fsCdXBwFwC+a+27/8gctNMuQC4f7UtWcF79KAFGoIfKZ4iqrElgWIa3j5vhMJDp0iikQ1g==", + "dev": true, + "requires": { + "@types/express": "*" + } + }, "@types/node": { "version": "18.17.15", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.17.15.tgz", @@ -8644,6 +8809,11 @@ "picomatch": "^2.0.4" } }, + "append-field": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/append-field/-/append-field-1.0.0.tgz", + "integrity": "sha512-klpgFSWLW1ZEs8svjfb7g4qWY0YS5imI82dTg+QahUvJ8YqAY0P10Uk8tTyh9ZGuYEZEMaeJYCF5BFuX552hsw==" + }, "arg": { "version": "4.1.3", "resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz", @@ -8868,6 +9038,11 @@ "ieee754": "^1.2.1" } }, + "buffer-from": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", + "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==" + }, "builtin-modules": { "version": "3.3.0", "resolved": "https://registry.npmjs.org/builtin-modules/-/builtin-modules-3.3.0.tgz", @@ -8883,6 +9058,14 @@ "semver": "^7.0.0" } }, + "busboy": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/busboy/-/busboy-1.6.0.tgz", + "integrity": "sha512-8SFQbg/0hQ9xy3UNTB0YEnsNBbWfhf7RtnzpL7TkBiTBRfrQ9Fxcnz7VJsleJpyp6rVLvXiuORqjlHi5q+PYuA==", + "requires": { + "streamsearch": "^1.1.0" + } + }, "bytes": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz", @@ -9115,6 +9298,46 @@ "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", "dev": true }, + "concat-stream": { + "version": "1.6.2", + "resolved": "https://registry.npmjs.org/concat-stream/-/concat-stream-1.6.2.tgz", + "integrity": "sha512-27HBghJxjiZtIk3Ycvn/4kbJk/1uZuJFfuPEns6LaEvpvG1f0hTea8lilrouyo9mVc2GWdcEZ8OLoGmSADlrCw==", + "requires": { + "buffer-from": "^1.0.0", + "inherits": "^2.0.3", + "readable-stream": "^2.2.2", + "typedarray": "^0.0.6" + }, + "dependencies": { + "isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==" + }, + "readable-stream": { + "version": "2.3.8", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.8.tgz", + "integrity": "sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA==", + "requires": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, + "string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "requires": { + "safe-buffer": "~5.1.0" + } + } + } + }, "connect-pause": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/connect-pause/-/connect-pause-0.1.1.tgz", @@ -9175,6 +9398,11 @@ "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", "integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==" }, + "core-util-is": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz", + "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==" + }, "cors": { "version": "2.8.5", "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", @@ -11333,8 +11561,7 @@ "minimist": { "version": "1.2.8", "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz", - "integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==", - "dev": true + "integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==" }, "minipass": { "version": "7.0.3", @@ -11342,6 +11569,14 @@ "integrity": "sha512-LhbbwCfz3vsb12j/WkWQPZfKTsgqIe1Nf/ti1pKjYESGLHIVjWU96G9/ljLH4F9mWNVhlQOm0VySdAWzf05dpg==", "dev": true }, + "mkdirp": { + "version": "0.5.6", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.6.tgz", + "integrity": "sha512-FP+p8RB8OWpF3YZBCrP5gtADmtXApB5AMLn+vdyA+PyxCjrCs00mjyUozssO33cwDeT3wNGdLxJ5M//YqtHAJw==", + "requires": { + "minimist": "^1.2.6" + } + }, "mongodb": { "version": "5.8.1", "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-5.8.1.tgz", @@ -11440,6 +11675,20 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, + "multer": { + "version": "1.4.5-lts.1", + "resolved": "https://registry.npmjs.org/multer/-/multer-1.4.5-lts.1.tgz", + "integrity": "sha512-ywPWvcDMeH+z9gQq5qYHCCy+ethsk4goepZ45GLD63fOu0YcNecQxi64nDs3qluZB+murG3/D4dJ7+dGctcCQQ==", + "requires": { + "append-field": "^1.0.0", + "busboy": "^1.0.0", + "concat-stream": "^1.5.2", + "mkdirp": "^0.5.4", + "object-assign": "^4.1.1", + "type-is": "^1.6.4", + "xtend": "^4.0.0" + } + }, "nanoid": { "version": "3.3.7", "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.7.tgz", @@ -11535,8 +11784,7 @@ "object-assign": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", - "integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==", - "dev": true + "integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==" }, "object-inspect": { "version": "1.13.2", @@ -11919,6 +12167,11 @@ "resolved": "https://registry.npmjs.org/process/-/process-0.11.10.tgz", "integrity": "sha512-cdGef/drWFoydD1JsMzuFf8100nZl+GT+yacc2bEced5f9Rjk4z+WtFUTBu9PhOi9j/jfmBPu0mMEY4wIdAF8A==" }, + "process-nextick-args": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz", + "integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==" + }, "process-warning": { "version": "2.3.2", "resolved": "https://registry.npmjs.org/process-warning/-/process-warning-2.3.2.tgz", @@ -12241,8 +12494,7 @@ "safe-buffer": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", - "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", - "dev": true + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" }, "safe-regex": { "version": "2.1.1", @@ -12550,6 +12802,11 @@ "graceful-fs": "^4.1.3" } }, + "streamsearch": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-1.1.0.tgz", + "integrity": "sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==" + }, "string_decoder": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", @@ -12890,6 +13147,11 @@ "possible-typed-array-names": "^1.0.0" } }, + "typedarray": { + "version": "0.0.6", + "resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz", + "integrity": "sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==" + }, "typescript": { "version": "5.2.2", "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.2.2.tgz", @@ -12931,8 +13193,7 @@ "util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", - "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==", - "dev": true + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==" }, "utils-merge": { "version": "1.0.1", @@ -13096,6 +13357,11 @@ "integrity": "sha512-PSNhEJDejZYV7h50BohL09Er9VaIefr2LMAf3OEmpCkjOi34eYyQYAXUTjEQtZJTKcF0E2UKTh+osDLsgNim9Q==", "dev": true }, + "xtend": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", + "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==" + }, "y18n": { "version": "5.0.8", "resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz", diff --git a/package.json b/package.json index 265d8d2..e65fd88 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,8 @@ "@types/convict": "6.1.4", "@types/convict-format-with-validator": "6.0.3", "@types/express": "4.17.18", + "@types/mime-types": "2.1.2", + "@types/multer": "1.4.8", "@types/node": "18.17.15", "@typescript-eslint/eslint-plugin": "6.7.0", "@typescript-eslint/parser": "6.7.0", @@ -54,7 +56,9 @@ "got": "13.0.0", "http-status-codes": "2.3.0", "inversify": "6.0.1", + "mime-types": "2.1.35", "mongoose": "7.5.3", + "multer": "1.4.5-lts.1", "pino": "8.15.1", "reflect-metadata": "0.1.13" } diff --git a/src/rest/rest.application.ts b/src/rest/rest.application.ts index b31a987..45a0ac4 100644 --- a/src/rest/rest.application.ts +++ b/src/rest/rest.application.ts @@ -51,6 +51,10 @@ export class RestApplication { private async _initMiddleware() { this.server.use(express.json()); + this.server.use( + '/upload', + express.static(this.config.get('UPLOAD_DIRECTORY')) + ); } private async _initExceptionFilters() { diff --git a/src/shared/libs/config/rest.schema.ts b/src/shared/libs/config/rest.schema.ts index f95cd1a..e2687fa 100644 --- a/src/shared/libs/config/rest.schema.ts +++ b/src/shared/libs/config/rest.schema.ts @@ -11,6 +11,7 @@ export type RestSchema = { DB_PASSWORD: string; DB_PORT: string; DB_NAME: string; + UPLOAD_DIRECTORY: string; } export const configRestSchema = convict({ @@ -56,4 +57,12 @@ export const configRestSchema = convict({ env: 'DB_NAME', default: 'six-cities_data' }, + UPLOAD_DIRECTORY: { + doc: 'Directory for upload files', + format: String, + env: 'UPLOAD_DIRECTORY', + default: 'upload' + }, }); + + diff --git a/src/shared/libs/rest/index.ts b/src/shared/libs/rest/index.ts index 3cc9f35..44e62be 100644 --- a/src/shared/libs/rest/index.ts +++ b/src/shared/libs/rest/index.ts @@ -10,3 +10,5 @@ export * from './errors/index.js'; export * from './middleware/middleware.interface.js'; export * from './middleware/validate-objectid.middleware.js'; export * from './middleware/validate-dto.middleware.js'; +export * from './middleware/document-exista.middleware.js'; +export * from './middleware/upload-file.middleware.js'; diff --git a/src/shared/libs/rest/middleware/document-exista.middleware.ts b/src/shared/libs/rest/middleware/document-exista.middleware.ts new file mode 100644 index 0000000..9645c72 --- /dev/null +++ b/src/shared/libs/rest/middleware/document-exista.middleware.ts @@ -0,0 +1,26 @@ +import {Middleware} from './middleware.interface.js'; +import {NextFunction, Request, Response} from 'express'; +import {DocumentExists} from '../../../types/index.js'; +import {HttpError} from '../errors/index.js'; +import {StatusCodes} from 'http-status-codes'; + +export class DocumentExistsMiddleware implements Middleware { + constructor( + private readonly service: DocumentExists, + private readonly entityName: string, + private readonly paramName: string, + ) { + } + + public async execute({params}: Request, _res: Response, next: NextFunction): Promise { + const documentId = params[this.paramName]; + if (!await this.service.exists(documentId)) { + throw new HttpError( + StatusCodes.NOT_FOUND, + `${this.entityName} with ${documentId} not found.`, + 'DocumentExistsMiddleware' + ); + } + next(); + } +} diff --git a/src/shared/libs/rest/middleware/upload-file.middleware.ts b/src/shared/libs/rest/middleware/upload-file.middleware.ts new file mode 100644 index 0000000..3fbd834 --- /dev/null +++ b/src/shared/libs/rest/middleware/upload-file.middleware.ts @@ -0,0 +1,28 @@ +import {NextFunction, Request, Response} from 'express'; +import multer, {diskStorage} from 'multer'; +import {extension} from 'mime-types'; +import * as crypto from 'node:crypto'; +import {Middleware} from './middleware.interface.js'; + +export class UploadFileMiddleware implements Middleware { + constructor( + private uploadDirectory: string, + private fieldName: string, + ) { + } + + public async execute(req: Request, res: Response, next: NextFunction): Promise { + const storage = diskStorage({ + destination: this.uploadDirectory, + filename: (_req, file, callback) => { + const fileExtension = extension(file.mimetype); + console.log(file); + const filename = crypto.randomUUID(); + callback(null, `${filename}.${fileExtension}`); + } + }); + const uploadSingleFileMiddleware = multer({storage}) + .single(this.fieldName); + uploadSingleFileMiddleware(req, res, next); + } +} diff --git a/src/shared/modules/comment/comment-service.interface.ts b/src/shared/modules/comment/comment-service.interface.ts index 119beef..b2f242a 100644 --- a/src/shared/modules/comment/comment-service.interface.ts +++ b/src/shared/modules/comment/comment-service.interface.ts @@ -1,8 +1,9 @@ import {CreateCommentDto} from './dto/create-comment.dto.js'; import {DocumentType} from '@typegoose/typegoose'; import {CommentEntity} from './comment.entity.js'; +import {DocumentExists} from '../../types/index.js'; -export interface CommentService { +export interface CommentService extends DocumentExists { create(dto: CreateCommentDto): Promise | null>; findByOfferId(offerId: string): Promise[]>; } diff --git a/src/shared/modules/comment/comment.controller.ts b/src/shared/modules/comment/comment.controller.ts index 02c6579..a1338e3 100644 --- a/src/shared/modules/comment/comment.controller.ts +++ b/src/shared/modules/comment/comment.controller.ts @@ -1,19 +1,26 @@ import {inject, injectable} from 'inversify'; import {Request, Response} from 'express'; -import {BaseController, HttpMethod, ValidateDtoMiddleware, ValidateObjectIdMiddleware} from '../../libs/rest/index.js'; +import { + BaseController, + DocumentExistsMiddleware, + HttpMethod, + ValidateDtoMiddleware, + ValidateObjectIdMiddleware +} from '../../libs/rest/index.js'; import {Logger} from '../../libs/logger/index.js'; import {Component} from '../../types/index.js'; import {CommentService} from './comment-service.interface.js'; import {CommentRdo} from './rdo/comment.rdo.js'; import {fillDTO} from '../../helpers/index.js'; import {CreateCommentDto} from './dto/create-comment.dto.js'; -import {CreateOfferDto} from '../offer/index.js'; +import {CreateOfferDto, OfferServiceInterface} from '../offer/index.js'; @injectable() export class CommentController extends BaseController { constructor( @inject(Component.Logger) protected readonly logger: Logger, @inject(Component.CommentService) private readonly commentService: CommentService, + @inject(Component.OfferService) private readonly offerService: OfferServiceInterface, ) { super(logger); @@ -23,13 +30,13 @@ export class CommentController extends BaseController { path: '/:offerId', method: HttpMethod.Get, handler: this.index, - middlewares: [new ValidateObjectIdMiddleware('offerId')] + middlewares: [new ValidateObjectIdMiddleware('offerId'), new DocumentExistsMiddleware(this.offerService, 'Offer', 'offerId'),] }); this.addRoute({ path: '/:offerId', method: HttpMethod.Post, handler: this.create, - middlewares: [new ValidateObjectIdMiddleware('offerId'), new ValidateDtoMiddleware(CreateOfferDto)] + middlewares: [new ValidateObjectIdMiddleware('offerId'), new ValidateDtoMiddleware(CreateOfferDto), new DocumentExistsMiddleware(this.offerService, 'Offer', 'offerId')] }); } diff --git a/src/shared/modules/offer/offer-service.interface.ts b/src/shared/modules/offer/offer-service.interface.ts index 8698a7a..c123bf6 100644 --- a/src/shared/modules/offer/offer-service.interface.ts +++ b/src/shared/modules/offer/offer-service.interface.ts @@ -2,8 +2,9 @@ import {DocumentType} from '@typegoose/typegoose'; import {OfferEntity} from './offer.entity.js'; import {CreateOfferDto} from './dto/create-offer.dto.js'; import {UpdateOfferDto} from './dto/update-offer.dto.js'; +import {DocumentExists} from '../../types/index.js'; -export interface OfferServiceInterface { +export interface OfferServiceInterface extends DocumentExists { create(dto: CreateOfferDto): Promise>; findAll(): Promise[]>; findById(offerId: string): Promise | null>; diff --git a/src/shared/modules/offer/offer.controller.ts b/src/shared/modules/offer/offer.controller.ts index 831e505..74da2ca 100644 --- a/src/shared/modules/offer/offer.controller.ts +++ b/src/shared/modules/offer/offer.controller.ts @@ -1,5 +1,11 @@ import {inject, injectable} from 'inversify'; -import {BaseController, HttpMethod, ValidateDtoMiddleware, ValidateObjectIdMiddleware} from '../../libs/rest/index.js'; +import { + BaseController, + DocumentExistsMiddleware, + HttpMethod, + ValidateDtoMiddleware, + ValidateObjectIdMiddleware +} from '../../libs/rest/index.js'; import {Component} from '../../types/index.js'; import {Logger} from '../../libs/logger/index.js'; import {Request, Response} from 'express'; @@ -51,19 +57,19 @@ export class OfferController extends BaseController { path: '/:id', method: HttpMethod.Get, handler: this.show, - middlewares: [new ValidateObjectIdMiddleware('id')] + middlewares: [new ValidateObjectIdMiddleware('id'), new DocumentExistsMiddleware(this.offerService, 'Offer', 'id'),] }); this.addRoute({ path: '/:id', method: HttpMethod.Patch, handler: this.edit, - middlewares: [new ValidateObjectIdMiddleware('id'), new ValidateDtoMiddleware(UpdateOfferDto)] + middlewares: [new ValidateObjectIdMiddleware('id'), new ValidateDtoMiddleware(UpdateOfferDto), new DocumentExistsMiddleware(this.offerService, 'Offer', 'id')] }); this.addRoute({ path: '/:id', method: HttpMethod.Delete, handler: this.delete, - middlewares: [new ValidateObjectIdMiddleware('id')] + middlewares: [new ValidateObjectIdMiddleware('id'), new DocumentExistsMiddleware(this.offerService, 'Offer', 'id'),] }); } diff --git a/src/shared/modules/user/user.controller.ts b/src/shared/modules/user/user.controller.ts index d1887b8..a430918 100644 --- a/src/shared/modules/user/user.controller.ts +++ b/src/shared/modules/user/user.controller.ts @@ -1,6 +1,12 @@ import {inject, injectable} from 'inversify'; -import {Response} from 'express'; -import {BaseController, HttpError, HttpMethod, ValidateDtoMiddleware} from '../../libs/rest/index.js'; +import {Request, Response} from 'express'; +import { + BaseController, + HttpError, + HttpMethod, UploadFileMiddleware, + ValidateDtoMiddleware, + ValidateObjectIdMiddleware +} from '../../libs/rest/index.js'; import {Logger} from '../../libs/logger/index.js'; import {Component} from '../../types/index.js'; import {CreateUserRequest} from './create-user-request.type.js'; @@ -35,6 +41,15 @@ export class UserController extends BaseController { handler: this.login, middlewares: [new ValidateDtoMiddleware(LoginUserDto)] }); + this.addRoute({ + path: '/:userId/avatar', + method: HttpMethod.Post, + handler: this.uploadAvatar, + middlewares: [ + new ValidateObjectIdMiddleware('userId'), + new UploadFileMiddleware(this.configService.get('UPLOAD_DIRECTORY'), 'avatar'), + ] + }); } public async create( @@ -76,4 +91,10 @@ export class UserController extends BaseController { 'UserController', ); } + + public async uploadAvatar(req: Request, res: Response) { + this.created(res, { + filepath: req.file?.path + }); + } } diff --git a/src/shared/modules/user/user.http b/src/shared/modules/user/user.http index 6ff7a62..c6d5085 100644 --- a/src/shared/modules/user/user.http +++ b/src/shared/modules/user/user.http @@ -21,3 +21,16 @@ Content-Type: application/json } ### + +## Отправить изображение + +POST http://localhost:4000/users/6717e369c934030b401bf05c/avatar HTTP/1.1 +Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW + +------WebKitFormBoundary7MA4YWxkTrZu0gW +Content-Disposition: form-data; name="avatar"; filename="2150932060.jpg" +Content-Type: image/jpg + +< /Users/sergeiragozin/Pictures/Screenshots/2150932060.jpg +------WebKitFormBoundary7MA4YWxkTrZu0gW-- +## diff --git a/src/shared/types/document-exists.interface.ts b/src/shared/types/document-exists.interface.ts new file mode 100644 index 0000000..8bf1bd5 --- /dev/null +++ b/src/shared/types/document-exists.interface.ts @@ -0,0 +1,3 @@ +export interface DocumentExists { + exists(documentId: string): Promise; +} diff --git a/src/shared/types/index.ts b/src/shared/types/index.ts index 81f5d11..6cc4ef4 100644 --- a/src/shared/types/index.ts +++ b/src/shared/types/index.ts @@ -7,3 +7,4 @@ export * from './offer-good.enum.js'; export * from './mock-server-data.type.js'; export * from './good.union-type.js'; export * from './component.enum.js'; +export * from './document-exists.interface.js';