File size: 1,783 Bytes
5fbccc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
FROM node:18-alpine

WORKDIR /app

RUN cat <<EOF > package.json
{
  "name": "image-border-api-hf",
  "version": "1.0.0",
  "description": "API to add white borders to an image.",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "axios": "^1.6.8",
    "express": "^4.19.2",
    "sharp": "^0.33.3"
  }
}
EOF

RUN cat <<EOF > server.js
const express = require('express');
const sharp = require('sharp');
const axios = require('axios');

const app = express();
const PORT = process.env.PORT || 7860;

app.get('/add-border', async (req, res) => {
  const { url } = req.query;
  
  if (!url) {
    return res.status(400).json({ error: 'Параметр "url" обязателен.' });
  }

  const top = parseInt(req.query.top, 10) || 0;
  const bottom = parseInt(req.query.bottom, 10) || 0;
  const left = parseInt(req.query.left, 10) || 0;
  const right = parseInt(req.query.right, 10) || 0;

  try {
    const imageResponse = await axios({
      url,
      responseType: 'arraybuffer'
    });
    const imageBuffer = imageResponse.data;

    const processedImage = await sharp(imageBuffer)
      .extend({
        top: top,
        bottom: bottom,
        left: left,
        right: right,
        background: { r: 255, g: 255, b: 255, alpha: 1 }
      })
      .png()
      .toBuffer();

    res.set('Content-Type', 'image/png');
    res.send(processedImage);

  } catch (error) {
    console.error('Ошибка обработки изображения:', error);
    res.status(500).json({ error: 'Не удалось обработать изображение.' });
  }
});

app.listen(PORT, () => {
  console.log(`Сервер запущен на порту ${PORT}`);
});
EOF

RUN npm install

EXPOSE 7860

CMD [ "npm", "start" ]