Debian Codegraph @

src/ImageContainer.cpp

raw
1 // For all support, instructions and copyright go to:
2 // http://e2guardian.org/
3 // Released under the GPL v2, with the OpenSSL exception described in the README file.
4
5 // INCLUDES
6
7 #ifdef HAVE_CONFIG_H
8 #include "e2config.h"
9 #endif
10 #include "ImageContainer.hpp"
11 #include "Logger.hpp"
12
13 #include <iostream>
14 #include <fstream>
15 #include <stdexcept>
16 #include <cerrno>
17 #include <cstring>
18 #include <limits.h>
19
20 // GLOBALS
21
22
23 // IMPLEMENTATION
24
25 ImageContainer::ImageContainer()
26 {
27 image = NULL;
28 imagelength = 0;
29 }
30
31 ImageContainer::~ImageContainer()
32 {
33 delete[] image;
34 }
35
36 // wipe the loaded image
37 void ImageContainer::reset()
38 {
39 delete[] image;
40 image = NULL;
41 mimetype = "";
42 imagelength = 0;
43 }
44
45 // send image to client
46 //void ImageContainer::display(Socket *s)
47 bool ImageContainer::display(Socket *s)
48 {
49 DEBUG_debug("Displaying custom image file mimetype: ", mimetype);
50 s->writeString("Content-type: ");
51 s->writeString(mimetype.toCharArray());
52 s->writeString("\n\n");
53
54 if (!s->writeToSocket(image, imagelength, 0, s->getTimeout()))
55 // throw std::runtime_error(std::string("Can't write to socket: ") + strerror(errno));
56 return false;
57 return true;
58 }
59
60
61 bool ImageContainer::display_hb(String &eheader, String &ebody) {
62 eheader += "Content-type: " ;
63 eheader += mimetype.toCharArray();
64 eheader += "\n\n";
65 ebody = image;
66 return true;
67 }
68
69 // read image from file
70 bool ImageContainer::read(const char *filename)
71 {
72 String temp(filename);
73 temp.toLower();
74
75 if (temp.endsWith(".jpg") || temp.endsWith(".jpeg") || temp.endsWith(".jpe")) {
76 mimetype = "image/jpg";
77 } else if (temp.endsWith("png"))
78 mimetype = "image/png";
79 else if (temp.endsWith("swf"))
80 mimetype = "application/x-shockwave-flash";
81 else {
82 mimetype = "image/gif";
83 }
84
85 std::ifstream imagefile;
86 imagefile.open(filename, std::ifstream::binary);
87 imagefile.seekg(0, std::ios::end);
88 imagelength = imagefile.tellg();
89 imagefile.seekg(0, std::ios::beg);
90
91 if (imagelength) {
92 if (image != NULL)
93 delete[] image;
94 image = new char[imagelength + 1];
95 imagefile.read(image, imagelength);
96 if (!imagefile.good()) {
97 E2LOGGER_error("Error reading custom image file: ", filename);
98 return false;
99 }
100 } else {
101 E2LOGGER_error("Error reading custom image file: ", filename);
102 return false;
103 }
104 imagefile.close();
105 return true;
106 }