'use strict';

// NODE MODULES
const fs = require('fs');
const path = require('path');

// NPM MODULES
const del = require('del');
const AWS = require('aws-sdk');
const multer = require('multer');
const createError = require('http-errors');
const debug = require('debug')('AnyDrive: photo-aws-router');

//APP MODULES
const Photo = require('../model/photo');
const Gallery = require('../model/gallery');
const s3UploadPromise = require('../lib/aws-promisify').s3UploadPromise;
const bearerAuth = require('../lib/bearer-auth-middleware');

// module config
AWS.config = new AWS.Config();
AWS.config.setPromisesDependency(require('bluebird'));
new AWS.Credentials({
  accessKeyId: process.env.AWS_ACCESS_KEY_ID,
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
});

// MODULE CONSTANTS
const s3 = new AWS.S3();
const dataDir =`${__dirname}/../data`;
const upload = multer({dest: dataDir});

const photoAwsRouter = module.exports = require('express').Router();

photoAwsRouter.post('/api/gallery/:id/photo', bearerAuth, upload.single('image'), function(req, res, next) {
  if (!req.file) return next(createError(400, 'no image found'));
  if (!req.file.path) return next(createError(500, 'image not saved'));

  let ext = path.extname(req.file.originalname); // ex: .jpg / .jpeg
  let params = {
    ACL: 'public-read',
    Bucket: process.env.AWS_BUCKET,
    Key: `${req.file.filename}${ext}`,
    Body: fs.createReadStream(req.file.path),
  };

  Gallery.findById(req.params.id)
  .then(() => s3UploadPromise(params))
  .catch(err => err.status ? Promise.reject(err) : Promise.reject(createError(500, err.message)))
  .then( s3data => {
    console.log('s3data', s3data);
    del([`${dataDir}/*`]);
    let picData = {
      name: req.body.name,
      desc: req.body.desc,
      objectKey: s3data.Key,
      imageURI: s3data.Location,
      userID: req.user._id,
      galleryID: req.params.id,
    };
    return new Photo(picData).save();
  })
  .then( pic => res.json(pic))
  .catch( err => next(err));
});

photoAwsRouter.delete('/api/photo/:bucket/:id', function(req, res, next) {
  debug('hit DELETE /api/photo/:bucket/:id');
  Photo.findById(req.params.id)
  .catch(err => Promise.reject(createError(404, err.message)))
  .then(photo => {
    if(photo.userID.toString() !== req.user._id.toString())
      return Promise.reject(createError(401, 'User not authorized to delete this photo'));

    let params = {
      Bucket: req.params.bucket,
      Key: photo.objectKey,
    };
    return s3.deleteObject(params).promise();
  })
 .then(() => {
   return Photo.findByIdAndRemove(req.params.photoID);
 })
 .then(() => res.sendStatus(204));
});

@@ -1638,43 +1638,8 @@
 %3E %7B%0A
-    console.log('s3data', s3data);%0A
     
@@ -1661,16 +1661,16 @@
 %7D/*%60%5D);%0A
+
     let 
@@ -1968,24 +1968,27 @@
 err));%0A%7D);%0A%0A
+// 
 photoAwsRout
@@ -2050,16 +2050,19 @@
 next) %7B%0A
+// 
   debug(
@@ -2099,16 +2099,19 @@
 /:id');%0A
+// 
   Photo.
@@ -2134,16 +2134,19 @@
 ams.id)%0A
+// 
   .catch
@@ -2196,26 +2196,29 @@
 .message)))%0A
+//
   
+ 
 .then(photo 
@@ -2218,24 +2218,27 @@
 (photo =%3E %7B%0A
+// 
     if(photo
@@ -2285,16 +2285,19 @@
 ring())%0A
+// 
       re
@@ -2379,17 +2379,22 @@
 oto'));%0A
-%0A
+//%0A// 
     let 
@@ -2404,22 +2404,25 @@
 ams = %7B%0A
+//
       
+ 
 Bucket: 
@@ -2440,16 +2440,19 @@
 bucket,%0A
+// 
       Ke
@@ -2471,23 +2471,29 @@
 ectKey,%0A
+// 
     %7D;%0A
+// 
     retu
@@ -2530,21 +2530,27 @@
 mise();%0A
+// 
   %7D)%0A
+// 
  .then((
@@ -2556,16 +2556,19 @@
 () =%3E %7B%0A
+// 
    retur
@@ -2614,20 +2614,26 @@
 otoID);%0A
+// 
  %7D)%0A
+// 
  .then((
@@ -2655,16 +2655,19 @@
 atus(204));%0A
+// 
 %7D);%0A

fix eeror in test