]> bicyclesonthemoon.info Git - ott/enhance/blob - npb-cgi.c
27049dd425df6d29e236372b58cd3b16e7317b24
[ott/enhance] / npb-cgi.c
1 /*
2 npb-cgi.c
3 Download images and add Newpixbots to them! (or Megans)
4 05.12.2022
5
6 Copyright (C) 2013, 2014, 2022  Balthasar SzczepaƄski
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Affero General Public License as
10 published by the Free Software Foundation, either version 3 of the
11 License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU Affero General Public License for more details.
17
18 You should have received a copy of the GNU Affero General Public License
19 along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 Requires cgilib (http://www.infodrom.org/projects/cgilib/)
22 */
23
24 #include <stdio.h>
25 #include <stdint.h>
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <sys/wait.h>
29 #include <stdint.h>
30 #include <inttypes.h>
31
32 #include <cgi.h>
33
34 #include "online-core.h"
35 #include "mpb.h"
36 #include "mpb_503.h"
37
38
39 int npb (
40         char *inpix, char *outpix,
41         uint_fast8_t remove_border, uint_fast32_t remove_border_width,
42         uint_fast8_t new_border, uint_fast32_t new_border_width,
43         uint_fast8_t external_border, uint_fast32_t external_border_width,
44         uint_fast8_t corners, uint_fast8_t ong1, uint8_t t403
45 );
46
47 int main (int argc, char *argv[])
48 {
49         s_cgi *cgi;
50         char in_tmp[256];
51         char out_tmp[256];
52         char *in_path;
53         char *t;
54         uint_fast8_t remove_border, new_border, external_border, corners, ong1;
55         uint_fast32_t remove_border_width, new_border_width, external_border_width;
56         uint_fast8_t t403 = 0;
57         
58         int r=0;
59         int r1=0;
60         int r2=0;
61         int r3=0;
62         
63         do {
64                 make_tmp_path(in_tmp, 256, 0, "");
65                 make_tmp_path(out_tmp, 256, 1, ".png");
66                 
67                 cgi=cgiInit();
68                 
69                 r = get_file(cgi,"inpix2", in_tmp, &in_path);
70                 if (r)
71                         r = get_file(cgi,"inpix", in_tmp, &in_path);
72                 if (r)
73                         break;
74                 
75                 remove_border   = (cgiGetValue(cgi, "r")!=NULL) ? 1 : 0;
76                 external_border = (cgiGetValue(cgi, "e")!=NULL) ? 1 : 0;
77                 new_border      = (cgiGetValue(cgi, "b")!=NULL) ? 1 : 0;
78                 corners         = (cgiGetValue(cgi, "c")!=NULL) ? 1 : 0;
79                 ong1            = (cgiGetValue(cgi, "o")!=NULL) ? 1 : 0;
80                 t403            = (cgiGetValue(cgi, "4")!=NULL) ? 1 : 0;
81                 
82                 if (remove_border)
83                 {
84                         t = cgiGetValue(cgi, "rw");
85                         if (t != NULL)
86                                 sscanf(t,"%"SCNuFAST32, &remove_border_width);
87                         else
88                                 remove_border_width = 0;
89                 }
90                 
91                 if (external_border)
92                 {
93                         t = cgiGetValue(cgi, "ew");
94                         if (t != NULL)
95                                 sscanf(t,"%"SCNuFAST32, &external_border_width);
96                         else
97                                 external_border_width = 0;
98                 }
99                 
100                 if (new_border)
101                 {
102                         t = cgiGetValue(cgi, "bw");
103                         if (t != NULL)
104                                 sscanf(t,"%"SCNuFAST32, &new_border_width);
105                         else
106                                 new_border_width = 0;
107                 }
108                 
109                 r = npb(
110                         in_path, out_tmp,
111                         remove_border, remove_border_width,
112                         new_border, new_border_width,
113                         external_border, external_border_width,
114                         corners, ong1, t403
115                 );
116                 if (r)
117                         break;
118                 
119                 r1 = send_file(out_tmp, "image/png", 0);
120         } while (0);
121         if (r)
122         {
123                 r1 = send_data(
124                         t403 ? mpb_503 : mpb,
125                         t403 ? mpb_503_size : mpb_size,
126                         "image/png",
127                         500
128                 );
129         }
130         r2 = rm(in_tmp);
131         r3 = rm(out_tmp);
132         if (r)
133                 return r;
134         if (r1)
135                 return r1;
136         if (r2)
137                 return r2;
138         if (r3)
139                 return r3;
140         return 0;
141 }
142
143 int npb (
144         char *inpix, char *outpix,
145         uint_fast8_t remove_border, uint_fast32_t remove_border_width,
146         uint_fast8_t new_border, uint_fast32_t new_border_width,
147         uint_fast8_t external_border, uint_fast32_t external_border_width,
148         uint_fast8_t corners, uint_fast8_t ong1, uint8_t t403
149 )
150 {
151         char s_rw[13] = "-0";
152         char s_bw[13] = "-0";
153         char s_ew[13] = "-0";
154         
155         pid_t sub;
156         int r;
157         
158         if (remove_border)
159                 snprintf(s_rw, 13, "-r%"PRIuFAST32, remove_border_width);
160         if (new_border)
161                 snprintf(s_bw, 13, "-b%"PRIuFAST32, new_border_width);
162         if (external_border)
163                 snprintf(s_ew, 13, "-e%"PRIuFAST32, external_border_width);
164         
165         sub = fork();
166         if (sub == 0)
167         {
168                 r = execl(
169                         NPB_PATH, NPB_PATH,
170                         s_rw, s_ew, s_bw,
171                         corners ? "-c" : "-0",
172                         ong1 ? "-o" : "-0",
173                         t403 ? "-4" : "-0",
174                         inpix, outpix,
175                         (char *)0
176                 );
177                 exit(r);
178         }
179         waitpid(sub, &r, 0);
180         return r;
181 }