Ivan Asmer hace 4 años
padre
commit
0163ff4c30
Se han modificado 2 ficheros con 26 adiciones y 19 borrados
  1. 7 18
      index.js
  2. 19 1
      models.js

+ 7 - 18
index.js

@@ -44,7 +44,7 @@ const upload  = require('multer')({ dest: `${__dirname}/public/images` })
 
         input LikeInput {
             _id: ID,
-            post: Post,
+            post: PostInput,
             comment: CommentInput,
             direct: DirectInput,
             user: UserInput,
@@ -83,9 +83,10 @@ const upload  = require('multer')({ dest: `${__dirname}/public/images` })
 
         input ImageInput {
             _id: ID,
-            userAvatar: User,
-            posts: [Post],
-            directs: [Direct]
+            text: String,
+            userAvatar: UserInput,
+            posts: [PostInput],
+            directs: [DirectInput]
         }
 
         type Comment {
@@ -134,7 +135,7 @@ const upload  = require('multer')({ dest: `${__dirname}/public/images` })
         input CollectionInput {
             _id: ID,
             text: String,
-            posts: [Post]
+            posts: [PostInput]
         }
     `);
 
@@ -151,22 +152,10 @@ const upload  = require('multer')({ dest: `${__dirname}/public/images` })
         let decoded;
         if (decoded = jwtCheck(req, jwtSecret)){
             console.log('SOME UPLOAD', decoded, req.file)
-            //let fileName = Math.random().toString('36')
-            //let fileStream = fs.createWriteStream(uploadPath + fileName);
 
             let {models: {Image }} = await getModels(decoded.sub)
-
-            let image  = new Image({})
-            image.fileData = req.file
-            image.url      = `images/${req.file.filename}`
-            image.originalFileName = req.file.originalname
-            await image.save()
+            let image = await Image.fromFileData(req.file)
             res.end(JSON.stringify({_id: image._id, url: image.url}))
-
-            //req.pipe(fileStream)
-            //req.on('end', () =>{
-                //res.end(fileName)
-            //})
         }
         else {
             res.status(503).send('permission denied')

+ 19 - 1
models.js

@@ -61,7 +61,25 @@ module.exports = async (dbName='hipstagram') => {
         class Image extends SlicedSavable {
             constructor(...params){
                 super(...params)
-                //TODO: multer, file data and so
+            }
+
+            static async fromFileData(fileData){
+                let image  = new Image({})
+                image.fileData = fileData
+                image.url      = `images/${fileData.filename}`
+                image.originalFileName = fileData.originalname
+                await image.save()
+                return image;
+            }
+
+            async save(...params){
+                if (this.userAvatar){
+                    if (this.userAvatar._id.toString() !== id){
+                        throw new ReferenceError(`You can't set ava for other user`)
+                    }
+                }
+
+                return await super.save(...params)
             }
 
             static get relations(){