This commit is contained in:
@@ -322,13 +322,14 @@ public class FileUploadServiceImpl implements FileUploadService {
|
||||
|
||||
File file = new File(finalFilePath);
|
||||
|
||||
if (session.getFileType().startsWith("audio") && !isWavFile(file)) {
|
||||
if (session.getFileType().startsWith("audio") && !isValidWav(file)) {
|
||||
session.setStatus(UploadStatus.FAILED);
|
||||
session.setLastError("wav file not have RIFF header");
|
||||
|
||||
sessionRepository.save(session);
|
||||
|
||||
throw new FileFormatException("Failed to upload chunk: .wav file not have RIFF header");
|
||||
throw new FileFormatException("Failed to upload chunk: .wav file not have RIFF header or " +
|
||||
"another problems with .wav file");
|
||||
}
|
||||
|
||||
FileProtector.Type type = "document".equals(session.getFileType()) ?
|
||||
@@ -431,6 +432,67 @@ public class FileUploadServiceImpl implements FileUploadService {
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isValidWav(File file) {
|
||||
if (file == null || !file.exists() || file.length() < 44) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try (FileInputStream fis = new FileInputStream(file)) {
|
||||
byte[] header = new byte[44];
|
||||
if (fis.read(header) < 44) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (header[0] != 'R' || header[1] != 'I' ||
|
||||
header[2] != 'F' || header[3] != 'F') {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (header[8] != 'W' || header[9] != 'A' ||
|
||||
header[10] != 'V' || header[11] != 'E') {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (header[12] != 'f' || header[13] != 'm' ||
|
||||
header[14] != 't' || header[15] != ' ') {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (header[36] != 'd' || header[37] != 'a' ||
|
||||
header[38] != 't' || header[39] != 'a') {
|
||||
return false;
|
||||
}
|
||||
|
||||
int audioFormat = (header[20] & 0xFF) | (header[21] & 0xFF) << 8;
|
||||
if (audioFormat != 1 && audioFormat != 3) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int numChannels = (header[22] & 0xFF) | (header[23] & 0xFF) << 8;
|
||||
if (numChannels < 1 || numChannels > 8) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int sampleRate = (header[24] & 0xFF) | (header[25] & 0xFF) << 8 |
|
||||
(header[26] & 0xFF) << 16 | (header[27] & 0xFF) << 24;
|
||||
if (sampleRate < 8000 || sampleRate > 192000) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int bitsPerSample = (header[34] & 0xFF) | (header[35] & 0xFF) << 8;
|
||||
if (bitsPerSample < 8 || bitsPerSample > 32) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private int protectCost(String fileType) {
|
||||
int cost = 0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user