{"action":"created","issue":{"url":"https://api.github.com/repos/strapi/strapi/issues/1753","repository_url":"https://api.github.com/repos/strapi/strapi","labels_url":"https://api.github.com/repos/strapi/strapi/issues/1753/labels{/name}","comments_url":"https://api.github.com/repos/strapi/strapi/issues/1753/comments","events_url":"https://api.github.com/repos/strapi/strapi/issues/1753/events","html_url":"https://github.com/strapi/strapi/issues/1753","id":350665322,"node_id":"MDU6SXNzdWUzNTA2NjUzMjI=","number":1753,"title":"Upload Bug or lacking knowledge","user":{"login":"johntom","id":1877376,"node_id":"MDQ6VXNlcjE4NzczNzY=","avatar_url":"https://avatars2.githubusercontent.com/u/1877376?v=4","gravatar_id":"","url":"https://api.github.com/users/johntom","html_url":"https://github.com/johntom","followers_url":"https://api.github.com/users/johntom/followers","following_url":"https://api.github.com/users/johntom/following{/other_user}","gists_url":"https://api.github.com/users/johntom/gists{/gist_id}","starred_url":"https://api.github.com/users/johntom/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/johntom/subscriptions","organizations_url":"https://api.github.com/users/johntom/orgs","repos_url":"https://api.github.com/users/johntom/repos","events_url":"https://api.github.com/users/johntom/events{/privacy}","received_events_url":"https://api.github.com/users/johntom/received_events","type":"User","site_admin":false},"labels":[{"id":758313798,"node_id":"MDU6TGFiZWw3NTgzMTM3OTg=","url":"https://api.github.com/repos/strapi/strapi/labels/type:%20help%20wanted%20%F0%9F%A4%B7%E2%80%8D%E2%99%82%EF%B8%8F","name":"type: help wanted 🤷‍♂️","color":"1ed856","default":false}],"state":"closed","locked":false,"assignee":{"login":"soupette","id":13311463,"node_id":"MDQ6VXNlcjEzMzExNDYz","avatar_url":"https://avatars2.githubusercontent.com/u/13311463?v=4","gravatar_id":"","url":"https://api.github.com/users/soupette","html_url":"https://github.com/soupette","followers_url":"https://api.github.com/users/soupette/followers","following_url":"https://api.github.com/users/soupette/following{/other_user}","gists_url":"https://api.github.com/users/soupette/gists{/gist_id}","starred_url":"https://api.github.com/users/soupette/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/soupette/subscriptions","organizations_url":"https://api.github.com/users/soupette/orgs","repos_url":"https://api.github.com/users/soupette/repos","events_url":"https://api.github.com/users/soupette/events{/privacy}","received_events_url":"https://api.github.com/users/soupette/received_events","type":"User","site_admin":false},"assignees":[{"login":"soupette","id":13311463,"node_id":"MDQ6VXNlcjEzMzExNDYz","avatar_url":"https://avatars2.githubusercontent.com/u/13311463?v=4","gravatar_id":"","url":"https://api.github.com/users/soupette","html_url":"https://github.com/soupette","followers_url":"https://api.github.com/users/soupette/followers","following_url":"https://api.github.com/users/soupette/following{/other_user}","gists_url":"https://api.github.com/users/soupette/gists{/gist_id}","starred_url":"https://api.github.com/users/soupette/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/soupette/subscriptions","organizations_url":"https://api.github.com/users/soupette/orgs","repos_url":"https://api.github.com/users/soupette/repos","events_url":"https://api.github.com/users/soupette/events{/privacy}","received_events_url":"https://api.github.com/users/soupette/received_events","type":"User","site_admin":false}],"milestone":null,"comments":4,"created_at":"2018-08-15T02:34:58Z","updated_at":"2018-09-17T09:18:06Z","closed_at":"2018-09-17T09:18:06Z","author_association":"NONE","body":"Node.js version: 9.11.1\r\nnpm version: 5.6.0\r\nStrapi version: 3.0.0-alpha.13.1\r\nDatabase: mongoDB v4.0.0\r\nOperating system: win10\r\nWhat is the current behavior?\r\nTrying to port a trails server that uses muter to upload files to specific directories.\r\nI'm testing wth 'koa-multer' and get the following\r\n\r\nWhat is the current behavior?\r\nClient: tried many variants but this seems like the best and putting headers in request caused more issues\r\n    //     headers: {\r\n    //  'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8',\r\n    //  'Accept': 'application/json',\r\n    // or   'Content-type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW',\r\n    //  },\r\n\r\n   let formData = new FormData()\r\n   //    console.log('fname ', image[0].name)\r\n    formData.append('file', image[0]);\r\n   let baseweb = 'http://localhost:1337/'\r\n   var url = baseweb + `gym/upload`\r\n    return fetch(url, {\r\n      mode: 'cors',\r\n      method: 'POST',\r\n    //  body: JSON.stringify(formData) // does not work with koa\r\n     body:formData\r\n    }).then((res) => res.json())\r\n      .then(data => console.log('data.message', data.message))\r\n      .catch(error => console.log(error));\r\n  },\r\n```\r\nSteps to reproduce the problem\r\nAfter I upload a csv file I get the following.\r\n```\r\n files:\r\n   { file:\r\n      File {\r\n        domain: null,\r\n        _events: {},\r\n        _eventsCount: 0,\r\n        _maxListeners: undefined,\r\n        size: 1574,\r\n        path: 'C:\\\\Users\\\\john\\\\AppData\\\\Local\\\\Temp\\\\upload_14d719cc6b8eaedf6d7f51f5ae303df5',\r\n        name: 'Gym Door Report0722.csv',\r\n        type: 'application/vnd.ms-excel',\r\n        hash: null,\r\n        lastModifiedDate: 2018-08-15T02:05:22.593Z,\r\n        _writeStream: [WriteStream] } } }\r\n[2018-08-15T02:05:22.610Z] debug POST /gym/upload (35 ms)\r\n(node:14516) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'headers' of undefined\r\n    at hasbody (D:\\Frameworks\\Strapi-Gatsby\\api\\node_modules\\type-is\\index.js:93:14)\r\n    at typeofrequest (D:\\Frameworks\\Strapi-Gatsby\\api\\node_modules\\type-is\\index.js:127:8)\r\n    at multerMiddleware (D:\\Frameworks\\Strapi-Gatsby\\api\\node_modules\\multer\\lib\\make-middleware.js:18:10)\r\n    at Promise (D:\\Frameworks\\Strapi-Gatsby\\api\\node_modules\\koa-multer\\index.js:38:9)\r\n    at new Promise (<anonymous>)\r\n    at D:\\Frameworks\\Strapi-Gatsby\\api\\node_modules\\koa-multer\\index.js:37:14\r\n    at upload (D:\\Frameworks\\Strapi-Gatsby\\api\\api\\gym\\controllers\\Gym.js:112:25)\r\n    at dispatch (D:\\Frameworks\\Strapi-Gatsby\\api\\node_modules\\koa-router\\node_modules\\koa-compose\\index.js:44:32)\r\n    at next (D:\\Frameworks\\Strapi-Gatsby\\api\\node_modules\\koa-router\\node_modules\\koa-compose\\index.js:45:18)\r\n    at dispatch (D:\\Frameworks\\Strapi-Gatsby\\api\\node_modules\\koa-compose\\index.js:42:32)\r\n    at policies.push (D:\\Frameworks\\Strapi-Gatsby\\api\\node_modules\\strapi\\lib\\middlewares\\router\\utils\\routerChecker.js:59:26)\r\n    at dispatch (D:\\Frameworks\\Strapi-Gatsby\\api\\node_modules\\koa-compose\\index.js:42:32)\r\n    at module.exports (D:\\Frameworks\\Strapi-Gatsby\\api\\plugins\\users-permissions\\config\\policies\\permissions.js:55:9)\r\n    at <anonymous>\r\n(node:14516) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)\r\n(node:14516) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.\r\n```\r\nWhat is the expected behavior?\r\nupload 1 or more files to directory, don't always need a file link\r\n\r\n\r\nSuggested solutions\r\nCurrent Code\r\n```\r\n'use strict';\r\n\r\n/**\r\n * Gym.js controller\r\n *\r\n * @description: A set of functions called \"actions\" for managing `Gym`.\r\n */\r\n```\r\nvar multer = require('koa-multer');\r\nconst path = require('path')\r\nconst fs = require('fs.extra')\r\n\r\n// const koaBody = require('koa-body')({ multipart: true });\r\n// const bodyParser = require('koa-bodyparser');\r\n\r\nconst storage = multer.diskStorage({\r\n  destination: function (req, file, cb) {\r\n\r\n    let dirtest = 'c:/DailyImport/import'\r\n    let fpath = dirtest + '/' + file.originalname\r\n    console.log('path ', fpath);\r\n\r\n    if (!fs.existsSync(dirtest)) {\r\n      //this.log.info(\"make dir\");\r\n      fs.mkdirp(dirtest, function (err) {\r\n        if (err) {\r\n          // this.log.info(err);\r\n        } else {\r\n          // this.log.info('pow!')\r\n          cb(null, dirtest)\r\n        }\r\n      })\r\n    } else cb(null, dirtest)\r\n\r\n  },\r\n  filename: function (req, file, cb) {\r\n    console.log(' file.originalname', file, file.originalname)\r\n    cb(null, file.originalname)\r\n  },\r\n\r\n})\r\n\r\nvar upload = multer({\r\n  storage: storage,\r\n  fileFilter: function (req, file, cb) {\r\n    console.log('upload file.mimetype', file.mimetype)\r\n\r\n    if (file.mimetype === 'application/pdf' || file.mimetype !== 'application/xls' || file.mimetype !== 'text/csv') {\r\n      cb(null, true);\r\n    } else {\r\n      req.fileValidationError = 'goes wrong on the mimetype';\r\n      return cb(null, false, new Error('goes wrong on the mimetype'));\r\n    }\r\n  }\r\n})\r\n\r\nmodule.exports = {\r\n  find: async (ctx) => {\r\n    // return strapi.services.gym.fetchAll(ctx.query);\r\n    const data = await strapi.services.gym.fetchAll(ctx.query);\r\n    console.log('data ', data.length)\r\n    return data // or  ctx.send(data);//or  // Send 200 `ok`\r\n  },\r\n  upload: async (ctx) => {\r\n    console.log('upload files ')\r\n    console.log('=======================')\r\n    // let rr = path.resolve(__dirname, '..') // api dir\r\n    let rr = path.resolve(__dirname, '../..') // 1 up  api dir\r\n    let mess = ' files uploaded'\r\n  \r\n    let body =ctx.request.body \r\n    // of below same error\r\n   //let body =JSON.stringify(ctx.request.body) //bodyParser(ctx.request.body)\r\n    console.log('======================sbody=', body)\r\n    upload.array('file')(body, ctx.response, err => {\r\n      if (err) {\r\n        console.log('err ', err)\r\n        this.log.info(err)\r\n      }\r\n      console.log('req.files[i req.files]', ctx.request.files)\r\n      // let mess = flen + ' files uploaded'\r\n      return { 'message': mess }// res.json({ 'message': mess })\r\n      ctx.send(mess)//'Hello World!');\r\n    })\r\n  },\r\n\r\n// below for mongo aggregates written to collection\r\n\r\n  aggregate: async (ctx) => {\r\n     const data = await strapi.services.gym.fetchAllaggregate(ctx.query);\r\n    console.log('Gym.js: aggregate data ', data.length)\r\n    let obj;\r\n    let objarray = []\r\n    data.forEach(function (rec) {\r\n      var userStr = JSON.stringify(rec);\r\n      obj = {};\r\n      JSON.parse(userStr, (key, value) => {\r\n        if (typeof value === 'string') {\r\n          obj[key] = value;\r\n        }\r\n        if (typeof value === 'number') {\r\n          obj[key] = value;\r\n          if (key === 'count') {\r\n            obj.CYM = obj.year + '-' + obj.month;\r\n            console.log('obj  value ', obj.tenant, obj.year, obj.month, obj.count);\r\n            objarray.push(obj)\r\n            obj = {};\r\n          }\r\n        }\r\n      });\r\n    })\r\n       console.log('objarray 1 ==========', objarray.length)\r\n    await strapi.services.gymaggregate.removeall()\r\n    await strapi.services.gymaggregate.bulkadd(objarray)\r\n    return objarray\r\n  },\r\n\r\n\r\n  findOne: async (ctx) => {\r\n    if (!ctx.params._id.match(/^[0-9a-fA-F]{24}$/)) {\r\n      return ctx.notFound();\r\n    }\r\n\r\n    return strapi.services.gym.fetch(ctx.params);\r\n  },\r\n\r\n\r\n\r\n  count: async (ctx) => {\r\n    return strapi.services.gym.count(ctx.query);\r\n  },\r\n\r\n\r\n  create: async (ctx) => {\r\n    return strapi.services.gym.add(ctx.request.body);\r\n  },\r\n\r\n  update: async (ctx, next) => {\r\n    return strapi.services.gym.edit(ctx.params, ctx.request.body);\r\n  },\r\n\r\n \r\n\r\n  destroy: async (ctx, next) => {\r\n    return strapi.services.gym.remove(ctx.params);\r\n  }\r\n};\r\n```"},"comment":{"url":"https://api.github.com/repos/strapi/strapi/issues/comments/421939748","html_url":"https://github.com/strapi/strapi/issues/1753#issuecomment-421939748","issue_url":"https://api.github.com/repos/strapi/strapi/issues/1753","id":421939748,"node_id":"MDEyOklzc3VlQ29tbWVudDQyMTkzOTc0OA==","user":{"login":"lauriejim","id":5716596,"node_id":"MDQ6VXNlcjU3MTY1OTY=","avatar_url":"https://avatars3.githubusercontent.com/u/5716596?v=4","gravatar_id":"","url":"https://api.github.com/users/lauriejim","html_url":"https://github.com/lauriejim","followers_url":"https://api.github.com/users/lauriejim/followers","following_url":"https://api.github.com/users/lauriejim/following{/other_user}","gists_url":"https://api.github.com/users/lauriejim/gists{/gist_id}","starred_url":"https://api.github.com/users/lauriejim/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lauriejim/subscriptions","organizations_url":"https://api.github.com/users/lauriejim/orgs","repos_url":"https://api.github.com/users/lauriejim/repos","events_url":"https://api.github.com/users/lauriejim/events{/privacy}","received_events_url":"https://api.github.com/users/lauriejim/received_events","type":"User","site_admin":false},"created_at":"2018-09-17T09:18:06Z","updated_at":"2018-09-17T09:18:06Z","author_association":"MEMBER","body":"You can check the Strapi SDK JS we have on our repo."}}
{"actor":{"display_login":"lauriejim"}}