aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org
diff options
context:
space:
mode:
authorAnton Tananaev <anton@traccar.org>2022-07-04 09:26:43 -0700
committerAnton Tananaev <anton@traccar.org>2022-07-04 09:26:43 -0700
commit0112af2cf93743b2bcec0e16cd0d95bcc9713f3f (patch)
treeee10c2572f926da6ffc04d08ee0dee494c41a610 /src/main/java/org
parentaa032baf7349d103cbb3592cfe08a5a0b45a1ff3 (diff)
downloadtrackermap-server-0112af2cf93743b2bcec0e16cd0d95bcc9713f3f.tar.gz
trackermap-server-0112af2cf93743b2bcec0e16cd0d95bcc9713f3f.tar.bz2
trackermap-server-0112af2cf93743b2bcec0e16cd0d95bcc9713f3f.zip
Add device image upload
Diffstat (limited to 'src/main/java/org')
-rw-r--r--src/main/java/org/traccar/api/resource/DeviceResource.java36
-rw-r--r--src/main/java/org/traccar/database/MediaManager.java5
2 files changed, 40 insertions, 1 deletions
diff --git a/src/main/java/org/traccar/api/resource/DeviceResource.java b/src/main/java/org/traccar/api/resource/DeviceResource.java
index 8791a82f9..1d9bc20ec 100644
--- a/src/main/java/org/traccar/api/resource/DeviceResource.java
+++ b/src/main/java/org/traccar/api/resource/DeviceResource.java
@@ -17,6 +17,7 @@ package org.traccar.api.resource;
import org.traccar.api.BaseObjectResource;
import org.traccar.broadcast.BroadcastService;
+import org.traccar.database.MediaManager;
import org.traccar.helper.LogAction;
import org.traccar.model.Device;
import org.traccar.model.DeviceAccumulators;
@@ -32,12 +33,18 @@ import org.traccar.storage.query.Request;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
+import javax.ws.rs.HeaderParam;
+import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
+import java.io.File;
+import java.io.FileInputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.LinkedList;
@@ -57,6 +64,9 @@ public class DeviceResource extends BaseObjectResource<Device> {
@Inject
private BroadcastService broadcastService;
+ @Inject
+ private MediaManager mediaManager;
+
public DeviceResource() {
super(Device.class);
}
@@ -110,7 +120,7 @@ public class DeviceResource extends BaseObjectResource<Device> {
@Path("{id}/accumulators")
@PUT
- public Response updateAccumulators(DeviceAccumulators entity) throws StorageException, IOException {
+ public Response updateAccumulators(DeviceAccumulators entity) throws StorageException {
if (permissionsService.notAdmin(getUserId())) {
permissionsService.checkManager(getUserId());
permissionsService.checkPermission(Device.class, getUserId(), entity.getDeviceId());
@@ -149,4 +159,28 @@ public class DeviceResource extends BaseObjectResource<Device> {
return Response.noContent().build();
}
+ @Path("{id}/image")
+ @POST
+ @Consumes("image/*")
+ public Response uploadImage(
+ @PathParam("id") long deviceId, File file,
+ @HeaderParam(HttpHeaders.CONTENT_TYPE) String type) throws StorageException, IOException {
+
+ Device device = storage.getObject(Device.class, new Request(
+ new Columns.All(),
+ new Condition.And(
+ new Condition.Equals("id", "id", deviceId),
+ new Condition.Permission(User.class, getUserId(), Device.class))));
+ if (device != null) {
+ String name = "device";
+ String extension = type.substring("image/".length());
+ try (var input = new FileInputStream(file);
+ var output = mediaManager.createFileStream(device.getUniqueId(), name, extension)) {
+ input.transferTo(output);
+ }
+ return Response.ok(name + "." + extension).build();
+ }
+ return Response.status(Response.Status.NOT_FOUND).build();
+ }
+
}
diff --git a/src/main/java/org/traccar/database/MediaManager.java b/src/main/java/org/traccar/database/MediaManager.java
index 5f3fdcdf7..2b3e3e1ee 100644
--- a/src/main/java/org/traccar/database/MediaManager.java
+++ b/src/main/java/org/traccar/database/MediaManager.java
@@ -25,6 +25,7 @@ import javax.inject.Inject;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
@@ -53,6 +54,10 @@ public class MediaManager {
return filePath.toFile();
}
+ public OutputStream createFileStream(String uniqueId, String name, String extension) throws IOException {
+ return new FileOutputStream(createFile(uniqueId, name + "." + extension));
+ }
+
public String writeFile(String uniqueId, ByteBuf buf, String extension) {
if (path != null) {
int size = buf.readableBytes();