]> bicyclesonthemoon.info Git - ott/enhance/blob - nofading-cgi.c
5b4f065562aa98b2bb895db2527fa040e92d6305
[ott/enhance] / nofading-cgi.c
1 /*
2 nofading-cgi.c
3 The online interface for the fading enhancement tool
4 04.12.2022
5
6 Copyright (C) 2015, 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 "nf.h"
36
37 int nofading (
38         char *inpix, char *outpix,
39         uint_fast32_t border,
40         uint_fast8_t individual_channels,
41         uint_fast8_t enhance_alpha,
42         uint_fast8_t remove_alpha
43 );
44
45 int main (int argc, char *argv[])
46 {
47         s_cgi *cgi;
48         char in_tmp[256];
49         char out_tmp[256];
50         char *in_path;
51         char *t;
52         uint_fast32_t border;
53         uint_fast8_t individual_channels, enhance_alpha, remove_alpha;
54         
55         int r=0;
56         int r1=0;
57         int r2=0;
58         int r3=0;
59         
60         do {
61                 make_tmp_path(in_tmp, 256, 0, "");
62                 make_tmp_path(out_tmp, 256, 1, ".png");
63                 
64                 cgi=cgiInit();
65                 
66                 r = get_file(cgi,"inpix", in_tmp, &in_path);
67                 if (r)
68                         break;
69                 
70                 individual_channels = (cgiGetValue(cgi, "c")!=NULL) ? 0 : 1;
71                 enhance_alpha = (cgiGetValue(cgi, "a")!=NULL) ? 1 : 0;
72                 remove_alpha = (cgiGetValue(cgi, "n")!=NULL) ? 1 : 0;
73                 
74                 t = cgiGetValue(cgi, "f");
75                 if (t != NULL)
76                         sscanf(t,"%"SCNuFAST32, &border);
77                 else
78                         border = 0;
79                 
80                 r = nofading(
81                         in_path, out_tmp,
82                         border,
83                         individual_channels, enhance_alpha, remove_alpha
84                 );
85                 if (r)
86                         break;
87                 r1 = send_file(out_tmp, "image/png", 0);
88         } while (0);
89         if (r)
90         {
91                 r1 = send_data(nf, nf_size, "image/png", 500);
92         }
93         r2 = rm(in_tmp);
94         r3 = rm(out_tmp);
95         if (r)
96                 return r;
97         if (r1)
98                 return r1;
99         if (r2)
100                 return r2;
101         if (r3)
102                 return r3;
103         return 0;
104 }
105
106 int nofading (
107         char *inpix, char *outpix,
108         uint_fast32_t border,
109         uint_fast8_t individual_channels,
110         uint_fast8_t enhance_alpha,
111         uint_fast8_t remove_alpha
112 )
113 {
114         char s_border[11];
115         pid_t sub;
116         int r;
117         
118         snprintf(s_border, 11, "%"PRIuFAST32, border);
119         
120         sub = fork();
121         if (sub == 0)
122         {
123                 r = execl(
124                         NOFADING_PATH, NOFADING_PATH,
125                         individual_channels ? "-c" : "-0",
126                         enhance_alpha ? "-a" : "-0",
127                         remove_alpha ? "-n" : "-0",
128                         inpix, outpix,
129                         s_border,
130                         (char *)0
131                 );
132                 exit(r);
133         }
134         waitpid(sub, &r, 0);
135         return r;
136 }
137
138 /*
139
140
141 #include <cgi.h>
142 #include <stdio.h>
143 #include <stdlib.h>
144 #include <unistd.h>
145 #include <string.h>
146 #include <sys/wait.h>
147 #include <sys/stat.h>
148
149 #define WGET_PATH  "/usr/bin/wget"
150 #define ENH_PATH   "./nofading"
151 #define RM_PATH    "/bin/rm"
152 #define LOG_PATH   "/eizm/log/nofading/nofading.log"
153 #define USERAGENT  "No fading (1190.bicyclesonthemoon.dnsd.info/nofading/nofading.htm)"
154 #define MSTD_PATH  "/pro/nofading/nf.png"
155
156 int main (int argc, char *argv[]);
157 void mustard(int mustard);
158
159 FILE *pix;
160
161 char url[1024];
162 char path[256];
163 s_cgi *cgi;
164 int t;
165 pid_t sub;
166
167 int main (int argc, char *argv[])
168 {
169         unsigned char buf[1024];
170         struct stat st;
171         unsigned long p;
172         unsigned short q;
173         char **up;
174
175         cgi=cgiInit();
176         
177         up=cgiGetFiles(cgi);
178         if (up)
179         {
180                 sprintf(path,"%s",cgiGetFile(cgi,up[0])->tmpfile);
181                 sprintf(url,"%s",cgiGetFile(cgi,up[0])->filename);
182         }
183         else
184         {
185                 sprintf(url,"%s",(cgiGetValue(cgi,"inpix")!=NULL)?(cgiGetValue(cgi,"inpix")):"");
186                 sprintf(path,"/var/tmp/%lu",(unsigned long)getpid());
187                 
188                 //Why is  there no spawnl() in linux?
189                 sub=fork();
190                 if(sub==0)
191                 {
192                         t=execl(WGET_PATH,WGET_PATH,"-q","-t","2","-U",USERAGENT,"-O",path,url,(char *)0);
193                         exit(t);
194                 }
195                 waitpid(sub,&t,0);
196                 if(t)mustard(t);
197         }
198         
199         sub=fork();
200         if(sub==0)
201         {
202                 t=execl(ENH_PATH,ENH_PATH,path,path,(cgiGetValue(cgi,"f")!=NULL)?(cgiGetValue(cgi,"f")):"0","q",cgiGetValue(cgi,"a")!=NULL?"0":"a",(char *)0);
203                 exit(t);
204         }
205         waitpid(sub,&t,0);
206         if(t)mustard(t);
207         
208         pix=fopen(path,"rb");
209         if(pix==NULL)
210                 mustard(123);
211         fstat(fileno(pix),&st);
212         p=st.st_size/1024;
213         q=st.st_size%1024;
214         
215         printf("Content-Length: %lu\n",(unsigned long)(st.st_size));
216         printf("Content-type: image/png\n\n");
217         for(unsigned long i=0;i<p;++i)
218         {
219                 fread(buf,1,1024,pix);
220                 fwrite(buf,1,1024,stdout);
221         }
222         if(q!=0)
223         {
224                 fread(buf,1,q,pix);
225                 fwrite(buf,1,q,stdout);
226         }
227         fclose(pix);
228         fflush(stdout);
229         mustard(0);//no mustard
230 }
231
232 void mustard(int mustard)
233 {
234         if(mustard)
235         {
236                 cgiRedirect(MSTD_PATH);
237         }
238         sub=fork();
239         if(sub==0)
240         {
241                 t=execl(RM_PATH,RM_PATH,"-f",path,(char *)0);
242                 exit(t);
243         }
244         waitpid(sub,&t,0);
245         pix=fopen(LOG_PATH,"at");
246         if(pix!=NULL)
247         {
248                 fprintf(pix,"%x %s\n",mustard,url);
249                 fclose(pix);
250         }
251         exit(mustard);
252 }
253
254 */