diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2018-02-03 08:07:02 +1300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-03 08:07:02 +1300 |
commit | c8f3ddf412dc98b9430f697a0e02d24a3f15627d (patch) | |
tree | 185d255ed2b6d1069c48cc39f5ef0545e0574626 /src | |
parent | c70fa517c467472e96c1f06729da338ac7a39c9e (diff) | |
parent | bec12783f99849131f80175731d00b3f7929e661 (diff) | |
download | trackermap-server-c8f3ddf412dc98b9430f697a0e02d24a3f15627d.tar.gz trackermap-server-c8f3ddf412dc98b9430f697a0e02d24a3f15627d.tar.bz2 trackermap-server-c8f3ddf412dc98b9430f697a0e02d24a3f15627d.zip |
Merge pull request #3752 from Abyss777/switch_media
Add switch to enable media web access
Diffstat (limited to 'src')
-rw-r--r-- | src/org/traccar/Context.java | 4 | ||||
-rw-r--r-- | src/org/traccar/database/MediaManager.java | 33 | ||||
-rw-r--r-- | src/org/traccar/web/WebServer.java | 14 |
3 files changed, 26 insertions, 25 deletions
diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index 3681cc2cc..09e3c619b 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -337,9 +337,7 @@ public final class Context { ldapProvider = new LdapProvider(config); } - if (config.hasKey("media.path")) { - mediaManager = new MediaManager(config); - } + mediaManager = new MediaManager(config.getString("media.path")); if (dataManager != null) { usersManager = new UsersManager(dataManager); diff --git a/src/org/traccar/database/MediaManager.java b/src/org/traccar/database/MediaManager.java index 2c448a20c..482442735 100644 --- a/src/org/traccar/database/MediaManager.java +++ b/src/org/traccar/database/MediaManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 - 2018 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +16,6 @@ package org.traccar.database; import org.jboss.netty.buffer.ChannelBuffer; -import org.traccar.Config; import org.traccar.helper.Log; import java.io.File; @@ -34,8 +33,8 @@ public class MediaManager { private String path; - public MediaManager(Config config) { - path = config.getString("media.path"); + public MediaManager(String path) { + this.path = path; } private File createFile(String uniqueId, String name) throws IOException { @@ -48,19 +47,21 @@ public class MediaManager { } 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); + if (path != null) { + 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); } - fileChannel.force(false); - return name; - } catch (IOException e) { - Log.warning(e); } return null; } diff --git a/src/org/traccar/web/WebServer.java b/src/org/traccar/web/WebServer.java index 88b38c1ab..06894552d 100644 --- a/src/org/traccar/web/WebServer.java +++ b/src/org/traccar/web/WebServer.java @@ -167,12 +167,14 @@ public class WebServer { servletHandler.addServlet(new ServletHolder(new AsyncSocketServlet()), "/socket"); - ServletHolder servletHolder = new ServletHolder("media", DefaultServlet.class); - servletHolder.setInitParameter("resourceBase", config.getString("media.path")); - servletHolder.setInitParameter("dirAllowed", config.getString("media.directoryAllow", "false")); - servletHolder.setInitParameter("pathInfoOnly", "true"); - servletHandler.addServlet(servletHolder, "/media/*"); - servletHandler.addFilter(MediaFilter.class, "/media/*", EnumSet.allOf(DispatcherType.class)); + if (config.hasKey("media.path")) { + ServletHolder servletHolder = new ServletHolder("media", DefaultServlet.class); + servletHolder.setInitParameter("resourceBase", config.getString("media.path")); + servletHolder.setInitParameter("dirAllowed", config.getString("media.directoryAllow", "false")); + servletHolder.setInitParameter("pathInfoOnly", "true"); + servletHandler.addServlet(servletHolder, "/media/*"); + servletHandler.addFilter(MediaFilter.class, "/media/*", EnumSet.allOf(DispatcherType.class)); + } ResourceConfig resourceConfig = new ResourceConfig(); resourceConfig.registerClasses(JacksonFeature.class, ObjectMapperProvider.class, ResourceErrorHandler.class); |