-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For #85 - implemented pictures uploading
- Loading branch information
Showing
6 changed files
with
187 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
src/main/java/org/traccar/web/server/model/PicturesServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright 2015 Vitaly Litvak ([email protected]) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.traccar.web.server.model; | ||
|
||
import com.google.inject.persist.Transactional; | ||
import org.apache.commons.fileupload.FileItemIterator; | ||
import org.apache.commons.fileupload.FileItemStream; | ||
import org.apache.commons.fileupload.FileUploadException; | ||
import org.apache.commons.fileupload.servlet.ServletFileUpload; | ||
import org.apache.commons.io.FileUtils; | ||
import org.apache.commons.io.IOUtils; | ||
import org.traccar.web.shared.model.Picture; | ||
import org.traccar.web.shared.model.PictureType; | ||
|
||
import javax.imageio.ImageIO; | ||
import javax.inject.Inject; | ||
import javax.inject.Provider; | ||
import javax.inject.Singleton; | ||
import javax.persistence.EntityManager; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.awt.image.BufferedImage; | ||
import java.io.*; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
@Singleton | ||
public class PicturesServlet extends HttpServlet { | ||
@Inject | ||
private Provider<EntityManager> entityManager; | ||
@Inject | ||
protected Logger logger; | ||
|
||
@Transactional(rollbackOn = { IOException.class, RuntimeException.class }) | ||
@RequireUser | ||
@Override | ||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
PictureType pictureType; | ||
try { | ||
pictureType = PictureType.valueOf(req.getPathInfo().substring(1)); | ||
} catch (IllegalArgumentException iae) { | ||
logger.log(Level.WARNING, "Incorrect picture type: " + req.getPathInfo(), iae); | ||
resp.getWriter().write("Unsupported picture type: " + req.getPathInfo()); | ||
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST); | ||
return; | ||
} | ||
|
||
ServletFileUpload servletFileUpload = new ServletFileUpload(); | ||
|
||
OutputStream os = null; | ||
File file = null; | ||
try { | ||
FileItemIterator fileItemIterator = servletFileUpload.getItemIterator(req); | ||
while (fileItemIterator.hasNext()) { | ||
file = File.createTempFile("uploaded", ".image"); | ||
file.deleteOnExit(); | ||
os = new BufferedOutputStream(new FileOutputStream(file)); | ||
IOUtils.copy(fileItemIterator.next().openStream(), os); | ||
os.flush(); | ||
os.close(); | ||
if (file.length() > pictureType.getMaxFileSize()) { | ||
resp.getWriter().write("File is too big. Max size is " + pictureType.getMaxFileSize() + " bytes."); | ||
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST); | ||
return; | ||
} | ||
|
||
BufferedImage image = ImageIO.read(file); | ||
if (image == null) { | ||
resp.getWriter().write("This is not an image."); | ||
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST); | ||
return; | ||
} | ||
|
||
if (image.getWidth() > pictureType.getMaxWidth() || image.getHeight() > pictureType.getMaxHeight()) { | ||
resp.getWriter().write("Image dimesions are too big. Max dimensions are: " + pictureType.getMaxWidth() + "x" + pictureType.getMaxHeight()); | ||
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST); | ||
return; | ||
} | ||
|
||
Picture picture = new Picture(); | ||
picture.setType(pictureType); | ||
picture.setWidth(image.getWidth()); | ||
picture.setHeight(image.getHeight()); | ||
picture.setData(FileUtils.readFileToByteArray(file)); | ||
entityManager.get().persist(picture); | ||
} | ||
} catch (FileUploadException fue) { | ||
logger.log(Level.WARNING, fue.getLocalizedMessage(), fue); | ||
throw new IOException(fue); | ||
} catch (IOException ioex) { | ||
logger.log(Level.WARNING, ioex.getLocalizedMessage(), ioex); | ||
throw ioex; | ||
} finally { | ||
IOUtils.closeQuietly(os); | ||
FileUtils.deleteQuietly(file); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters