diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2017-05-31 22:30:45 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2017-05-31 22:30:45 +1200 |
commit | dc5f2151d9e74f525d74663d599320b8cf3d9c47 (patch) | |
tree | c1e4994d17586ff619608608aa3e29ecf77e51b5 /src/org/traccar/database | |
parent | 883d296cc80e11279c5fca2d3d7a737eff762479 (diff) | |
download | trackermap-server-dc5f2151d9e74f525d74663d599320b8cf3d9c47.tar.gz trackermap-server-dc5f2151d9e74f525d74663d599320b8cf3d9c47.tar.bz2 trackermap-server-dc5f2151d9e74f525d74663d599320b8cf3d9c47.zip |
Implement image saving
Diffstat (limited to 'src/org/traccar/database')
-rw-r--r-- | src/org/traccar/database/MediaManager.java | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/org/traccar/database/MediaManager.java b/src/org/traccar/database/MediaManager.java index a492e1e6f..4b5930642 100644 --- a/src/org/traccar/database/MediaManager.java +++ b/src/org/traccar/database/MediaManager.java @@ -15,7 +15,20 @@ */ package org.traccar.database; +import org.jboss.netty.buffer.ChannelBuffer; import org.traccar.Config; +import org.traccar.helper.Log; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.text.SimpleDateFormat; +import java.util.Date; public class MediaManager { @@ -25,4 +38,28 @@ public class MediaManager { path = config.getString("media.path"); } + private File createFile(String uniqueId, String name) throws IOException { + Path filePath = Paths.get(path, uniqueId, name); + Files.createDirectories(filePath.getParent()); + return filePath.toFile(); + } + + public String writeFile(String uniqueId, ChannelBuffer buf, String extension) { + int size = buf.readableBytes(); + String name = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()) + "." + extension; + try (FileOutputStream output = new FileOutputStream(createFile(uniqueId, name)); + FileChannel fileChannel = output.getChannel()) { + ByteBuffer byteBuffer = buf.toByteBuffer(); + int written = 0; + while (written < size) { + written += fileChannel.write(byteBuffer); + } + fileChannel.force(false); + return name; + } catch (IOException e) { + Log.warning(e); + } + return null; + } + } |