|
|
|
@ -15,7 +15,6 @@ |
|
|
|
*/ |
|
|
|
package org.thingsboard.server.controller; |
|
|
|
|
|
|
|
import com.google.common.hash.Hashing; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.springframework.core.io.ByteArrayResource; |
|
|
|
@ -35,6 +34,7 @@ import org.thingsboard.server.common.data.Firmware; |
|
|
|
import org.thingsboard.server.common.data.FirmwareInfo; |
|
|
|
import org.thingsboard.server.common.data.audit.ActionType; |
|
|
|
import org.thingsboard.server.common.data.exception.ThingsboardException; |
|
|
|
import org.thingsboard.server.common.data.firmware.ChecksumAlgorithm; |
|
|
|
import org.thingsboard.server.common.data.firmware.FirmwareType; |
|
|
|
import org.thingsboard.server.common.data.id.DeviceProfileId; |
|
|
|
import org.thingsboard.server.common.data.id.FirmwareId; |
|
|
|
@ -53,6 +53,7 @@ import java.nio.ByteBuffer; |
|
|
|
public class FirmwareController extends BaseController { |
|
|
|
|
|
|
|
public static final String FIRMWARE_ID = "firmwareId"; |
|
|
|
public static final String CHECKSUM_ALGORITHM = "checksumAlgorithm"; |
|
|
|
|
|
|
|
@PreAuthorize("hasAnyAuthority( 'TENANT_ADMIN')") |
|
|
|
@RequestMapping(value = "/firmware/{firmwareId}/download", method = RequestMethod.GET) |
|
|
|
@ -125,9 +126,10 @@ public class FirmwareController extends BaseController { |
|
|
|
@ResponseBody |
|
|
|
public Firmware saveFirmwareData(@PathVariable(FIRMWARE_ID) String strFirmwareId, |
|
|
|
@RequestParam(required = false) String checksum, |
|
|
|
@RequestParam(required = false) String checksumAlgorithm, |
|
|
|
@RequestParam(CHECKSUM_ALGORITHM) String checksumAlgorithmStr, |
|
|
|
@RequestBody MultipartFile file) throws ThingsboardException { |
|
|
|
checkParameter(FIRMWARE_ID, strFirmwareId); |
|
|
|
checkParameter(CHECKSUM_ALGORITHM, checksumAlgorithmStr); |
|
|
|
try { |
|
|
|
FirmwareId firmwareId = new FirmwareId(toUUID(strFirmwareId)); |
|
|
|
FirmwareInfo info = checkFirmwareInfoId(firmwareId, Operation.READ); |
|
|
|
@ -141,18 +143,19 @@ public class FirmwareController extends BaseController { |
|
|
|
firmware.setVersion(info.getVersion()); |
|
|
|
firmware.setAdditionalInfo(info.getAdditionalInfo()); |
|
|
|
|
|
|
|
byte[] data = file.getBytes(); |
|
|
|
if (StringUtils.isEmpty(checksumAlgorithm)) { |
|
|
|
checksumAlgorithm = "sha256"; |
|
|
|
checksum = Hashing.sha256().hashBytes(data).toString(); |
|
|
|
ChecksumAlgorithm checksumAlgorithm = ChecksumAlgorithm.valueOf(checksumAlgorithmStr.toUpperCase()); |
|
|
|
|
|
|
|
byte[] bytes = file.getBytes(); |
|
|
|
if (StringUtils.isEmpty(checksum)) { |
|
|
|
checksum = firmwareService.generateChecksum(checksumAlgorithm, ByteBuffer.wrap(bytes)); |
|
|
|
} |
|
|
|
|
|
|
|
firmware.setChecksumAlgorithm(checksumAlgorithm); |
|
|
|
firmware.setChecksum(checksum); |
|
|
|
firmware.setFileName(file.getOriginalFilename()); |
|
|
|
firmware.setContentType(file.getContentType()); |
|
|
|
firmware.setData(ByteBuffer.wrap(data)); |
|
|
|
firmware.setDataSize((long) data.length); |
|
|
|
firmware.setData(ByteBuffer.wrap(bytes)); |
|
|
|
firmware.setDataSize((long) bytes.length); |
|
|
|
Firmware savedFirmware = firmwareService.saveFirmware(firmware); |
|
|
|
logEntityAction(savedFirmware.getId(), savedFirmware, null, ActionType.UPDATED, null); |
|
|
|
return savedFirmware; |
|
|
|
|