aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/database
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/traccar/database')
-rw-r--r--src/org/traccar/database/MediaManager.java37
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;
+ }
+
}