Example #1
0
File: app.js Project: argon/hackmd
 form.parse(req, function (err, fields, files) {
     if (err || !files.image || !files.image.path) {
         response.errorForbidden(res);
     } else {
         if (config.debug)
             logger.info('SERVER received uploadimage: ' + JSON.stringify(files.image));
         imgur.setClientId(config.imgur.clientID);
         try {
             imgur.uploadFile(files.image.path)
                 .then(function (json) {
                     if (config.debug)
                         logger.info('SERVER uploadimage success: ' + JSON.stringify(json));
                     res.send({
                         link: json.data.link.replace(/^http:\/\//i, 'https://')
                     });
                 })
                 .catch(function (err) {
                     logger.error(err);
                     return res.send('upload image error');
                 });
         } catch (err) {
             logger.error(err);
             return res.send('upload image error');
         }
     }
 });
Example #2
0
function uploadFromFromPath(path) {

  return imgur.uploadFile(path).then(function(json) {
    return json.data.link;
  });

}
Example #3
0
 return new Promise(function(resolve, reject) {
   imgur.uploadFile(file.path)
     .then(function(json) {
       if(!json || !json.data || !json.data.link) return reject();
       resolve(json.data.link);
     })
     .catch(reject);
 });
        files.forEach(function(file){
          imgur.uploadFile(testConfig.comparisonPath + file)
            .then(function (json) {
                $.util.log($.util.colors.red(file) + $.util.colors.cyan(' -> ') + json.data.link);

            })
            .catch(function (err) {
                console.error(err.message);
            });
        });
Example #5
0
 function (row, callback) {
     if (row) {
         var imgPath = path.join('.', 'data', row.ImagePath);
         imgur.uploadFile(imgPath).then(function (json) {
             callback(null, json.data, row)
         }, function () {
             callback({ error: 'failed to upload' });
         });
     }
 },
Example #6
0
function imgurUpload (source, done) {
  imgur.setClientId(imgurClientId);
  imgur
    .uploadFile(source.path)
    .then(function (res) {
      done(null, {
        alt: source.originalname,
        url: res.data.link.replace(/^http:/, 'https:')
      });
    })
    .catch(done);
}
function uploadImage(cb, file) {
	file = file || 'graph.png';
	if (fs.existsSync(file)) {
		imgur.uploadFile(file).then(function (json) {
			cb(json.data.link);
		}).catch(function () {
			console.err('Error upload graph.png make sure you run the madge command from the wiki');
			process.exit(1);
		});
	} else {
		cb(null);
	}
}
Example #8
0
		exec_cmd(cmd, function(err, data){	

		console.log(err);
		console.log(data.toString());                       
		console.log('after exec');
		var albumId = 'fGZi1';
		imgur.uploadFile('public/img/composite.png', albumId)	//upload to imgur
			.then(function (json) {
				photourl = json.data.link;
				console.log(photourl);
				//res.redirect('/share');
				res.json(json);
			})
			.catch(function (err) {
				console.error(err.message);
			});

		});
Example #9
0
        data.stickers.map(async (item) => {
            const key = `telegramstickerpack${item.file_id}`;
            const cache = await ctx.cache.get(key);
            if (cache) {
                return Promise.resolve(cache);
            } else {
                const pathResponse = await axios({
                    method: 'get',
                    url: `https://api.telegram.org/bot${config.telegram.token}/getFile?file_id=${item.file_id}`,
                });
                const path = pathResponse.data.result.file_path;

                const fileResponse = await axios({
                    method: 'get',
                    url: `https://api.telegram.org/file/bot${config.telegram.token}/${path}`,
                    responseType: 'arraybuffer',
                });

                const filePath = `tmp/${item.file_id}.png`;

                await sharp(fileResponse.data)
                    .png()
                    .toFile(filePath);

                let imgurResponse;
                try {
                    imgurResponse = await imgur.uploadFile(filePath);
                } catch (e) {
                    logger.error(`Error in imgur upload ${filePath}`, e);
                }
                const link = imgurResponse.data.link;

                fs.unlink(filePath, (e) => {
                    logger.error(`Error in remove ${filePath}`, e);
                });

                ctx.cache.set(key, link, cacheDays * 24 * 60 * 60);
                return Promise.resolve(link);
            }
        })
Example #10
0
  form.parse(req, function (err, fields, files) {
    if (err || !files.image || !files.image.path) {
      response.errorForbidden(res)
    } else {
      if (config.debug) { logger.info('SERVER received uploadimage: ' + JSON.stringify(files.image)) }

      try {
        switch (config.imageUploadType) {
          case 'filesystem':
            res.send({
              link: url.resolve(config.serverurl + '/', files.image.path.match(/^public\/(.+$)/)[1])
            })

            break

          case 's3':
            var AWS = require('aws-sdk')
            var awsConfig = new AWS.Config(config.s3)
            var s3 = new AWS.S3(awsConfig)
            const {getImageMimeType} = require('../utils')
            fs.readFile(files.image.path, function (err, buffer) {
              if (err) {
                logger.error(err)
                res.status(500).end('upload image error')
                return
              }
              var params = {
                Bucket: config.s3bucket,
                Key: path.join('uploads', path.basename(files.image.path)),
                Body: buffer
              }

              var mimeType = getImageMimeType(files.image.path)
              if (mimeType) { params.ContentType = mimeType }

              s3.putObject(params, function (err, data) {
                if (err) {
                  logger.error(err)
                  res.status(500).end('upload image error')
                  return
                }

                var s3Endpoint = 's3.amazonaws.com'
                if (config.s3.region && config.s3.region !== 'us-east-1') { s3Endpoint = `s3-${config.s3.region}.amazonaws.com` }
                res.send({
                  link: `https://${s3Endpoint}/${config.s3bucket}/${params.Key}`
                })
              })
            })
            break
          case 'imgur':
          default:
            imgur.setClientId(config.imgur.clientID)
            imgur.uploadFile(files.image.path)
              .then(function (json) {
                if (config.debug) { logger.info('SERVER uploadimage success: ' + JSON.stringify(json)) }
                res.send({
                  link: json.data.link.replace(/^http:\/\//i, 'https://')
                })
              })
              .catch(function (err) {
                logger.error(err)
                return res.status(500).end('upload image error')
              })
            break
        }
      } catch (err) {
        logger.error(err)
        return res.status(500).end('upload image error')
      }
    }
  })