281 lines
10 KiB
C++
281 lines
10 KiB
C++
|
|
// Copyright 2017-2019 David Romanski (Socke). All Rights Reserved.
|
||
|
|
|
||
|
|
#include "FileFunctionsWebCom.h"
|
||
|
|
|
||
|
|
|
||
|
|
UFileFunctionsWebCom::UFileFunctionsWebCom(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer) {
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
FString UFileFunctionsWebCom::getCleanDirectory(EFileFunctionsWebComDirectoryType directoryType, FString filePath) {
|
||
|
|
if (directoryType == EFileFunctionsWebComDirectoryType::E_ad) {
|
||
|
|
return FPaths::ConvertRelativePathToFull(filePath);
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
FString ProjectDir = FPaths::ProjectDir();
|
||
|
|
return FPaths::ConvertRelativePathToFull(ProjectDir + filePath);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void UFileFunctionsWebCom::writeBytesToFile(EFileFunctionsWebComDirectoryType directoryType, FString filePath, TArray<uint8> bytes, bool& success) {
|
||
|
|
success = FFileHelper::SaveArrayToFile(bytes, *getCleanDirectory(directoryType, filePath));
|
||
|
|
}
|
||
|
|
|
||
|
|
void UFileFunctionsWebCom::addBytesToFile(EFileFunctionsWebComDirectoryType directoryType, FString filePath, TArray<uint8> bytes, bool& success) {
|
||
|
|
FArchive* writer = IFileManager::Get().CreateFileWriter(*getCleanDirectory(directoryType, filePath), EFileWrite::FILEWRITE_Append);
|
||
|
|
if (!writer) {
|
||
|
|
success = false;
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
writer->Seek(writer->TotalSize());
|
||
|
|
writer->Serialize(bytes.GetData(), bytes.Num());
|
||
|
|
writer->Close();
|
||
|
|
delete writer;
|
||
|
|
|
||
|
|
success = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
void UFileFunctionsWebCom::splitFile(bool& success, TArray<FString>& absolutePathsOfTheParts, TArray<int64>& sizeOfTheParts, EFileFunctionsWebComDirectoryType directoryType, FString filePath, int32 parts){
|
||
|
|
|
||
|
|
if (parts <= 0)
|
||
|
|
parts = 1;
|
||
|
|
|
||
|
|
|
||
|
|
FString originalFilePath = getCleanDirectory(directoryType, filePath);
|
||
|
|
FString originalFileNameWithoutExtension = FPaths::GetBaseFilename(originalFilePath);
|
||
|
|
FString extension = FPaths::GetExtension(originalFilePath);
|
||
|
|
|
||
|
|
FArchive* reader = IFileManager::Get().CreateFileReader(*originalFilePath);
|
||
|
|
if (!reader) {
|
||
|
|
success = false;
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (reader->TotalSize() <= ((int64)parts)) {
|
||
|
|
success = false;
|
||
|
|
|
||
|
|
reader->Close();
|
||
|
|
delete reader;
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
int64 partSize = reader->TotalSize()/ ((int64)parts);
|
||
|
|
int64 savedBytes = 0;
|
||
|
|
TArray<uint8> bytes;
|
||
|
|
|
||
|
|
int32 iterator = 0;
|
||
|
|
while (iterator < parts-1){
|
||
|
|
reader->Seek(savedBytes);
|
||
|
|
bytes.AddUninitialized(partSize);
|
||
|
|
reader->Serialize(bytes.GetData(), partSize);
|
||
|
|
FString path = originalFilePath.Replace(*originalFileNameWithoutExtension, *FString(originalFileNameWithoutExtension+"_"+FString::FromInt(iterator)));
|
||
|
|
|
||
|
|
absolutePathsOfTheParts.Add(path);
|
||
|
|
sizeOfTheParts.Add(partSize);
|
||
|
|
|
||
|
|
if (FFileHelper::SaveArrayToFile(bytes, *path) == false) {
|
||
|
|
success = false;
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
bytes.Empty();
|
||
|
|
savedBytes += partSize;
|
||
|
|
iterator++;
|
||
|
|
}
|
||
|
|
//last part
|
||
|
|
reader->Seek(savedBytes);
|
||
|
|
bytes.AddUninitialized(reader->TotalSize() - savedBytes);
|
||
|
|
reader->Serialize(bytes.GetData(), reader->TotalSize() - savedBytes);
|
||
|
|
FString path = originalFilePath.Replace(*originalFileNameWithoutExtension, *FString(originalFileNameWithoutExtension + "_" + FString::FromInt(iterator)));
|
||
|
|
absolutePathsOfTheParts.Add(path);
|
||
|
|
if (FFileHelper::SaveArrayToFile(bytes, *path) == false) {
|
||
|
|
success = false;
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
bytes.Empty();
|
||
|
|
|
||
|
|
reader->Close();
|
||
|
|
delete reader;
|
||
|
|
success = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
TArray<uint8> UFileFunctionsWebCom::readBytesFromFile(EFileFunctionsWebComDirectoryType directoryType, FString filePath, int64 seekBytes, bool& success) {
|
||
|
|
TArray<uint8> result;
|
||
|
|
FArchive* reader = IFileManager::Get().CreateFileReader(*getCleanDirectory(directoryType, filePath));
|
||
|
|
if (!reader) {
|
||
|
|
success = false;
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
int64 totalSize = reader->TotalSize();
|
||
|
|
if (seekBytes > totalSize)
|
||
|
|
seekBytes = totalSize;
|
||
|
|
|
||
|
|
reader->Seek(seekBytes);
|
||
|
|
result.AddUninitialized((totalSize - seekBytes));
|
||
|
|
reader->Serialize(result.GetData(), (totalSize - seekBytes));
|
||
|
|
|
||
|
|
reader->Close();
|
||
|
|
delete reader;
|
||
|
|
success = true;
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
void UFileFunctionsWebCom::readStringFromFile(EFileFunctionsWebComDirectoryType directoryType, FString filePath, bool& success, FString& data) {
|
||
|
|
data.Empty();
|
||
|
|
success = FFileHelper::LoadFileToString(data, *getCleanDirectory(directoryType, filePath));
|
||
|
|
}
|
||
|
|
|
||
|
|
void UFileFunctionsWebCom::writeStringToFile(EFileFunctionsWebComDirectoryType directoryType, FString data, FString filePath, bool& success) {
|
||
|
|
success = FFileHelper::SaveStringToFile(data, *getCleanDirectory(directoryType, filePath));
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
void UFileFunctionsWebCom::getMD5FromFile(EFileFunctionsWebComDirectoryType directoryType, FString filePath, bool& success, FString& MD5) {
|
||
|
|
MD5.Empty();
|
||
|
|
FArchive* reader = IFileManager::Get().CreateFileReader(*getCleanDirectory(directoryType, filePath));
|
||
|
|
if (!reader) {
|
||
|
|
success = false;
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
TArray<uint8> byteArrayTmp;
|
||
|
|
int64 totalSize = reader->TotalSize();
|
||
|
|
int64 loadedBytes = 0;
|
||
|
|
int64 leftUploadBytes = 1024;
|
||
|
|
|
||
|
|
|
||
|
|
if (totalSize < leftUploadBytes)
|
||
|
|
leftUploadBytes = totalSize;
|
||
|
|
|
||
|
|
|
||
|
|
uint8 Digest[16];
|
||
|
|
FMD5 Md5Gen;
|
||
|
|
|
||
|
|
while ((loadedBytes + leftUploadBytes) <= totalSize) {
|
||
|
|
byteArrayTmp.Reset(leftUploadBytes);
|
||
|
|
byteArrayTmp.AddUninitialized(leftUploadBytes);
|
||
|
|
reader->Serialize(byteArrayTmp.GetData(), byteArrayTmp.Num());
|
||
|
|
loadedBytes += leftUploadBytes;
|
||
|
|
reader->Seek(loadedBytes);
|
||
|
|
|
||
|
|
Md5Gen.Update(byteArrayTmp.GetData(), byteArrayTmp.Num());
|
||
|
|
}
|
||
|
|
|
||
|
|
leftUploadBytes = totalSize - loadedBytes;
|
||
|
|
if (leftUploadBytes > 0) {
|
||
|
|
byteArrayTmp.Reset(leftUploadBytes);
|
||
|
|
byteArrayTmp.AddUninitialized(leftUploadBytes);
|
||
|
|
reader->Serialize(byteArrayTmp.GetData(), byteArrayTmp.Num());
|
||
|
|
loadedBytes += leftUploadBytes;
|
||
|
|
Md5Gen.Update(byteArrayTmp.GetData(), byteArrayTmp.Num());
|
||
|
|
}
|
||
|
|
|
||
|
|
if (reader != nullptr) {
|
||
|
|
reader->Close();
|
||
|
|
delete reader;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (totalSize != loadedBytes) {
|
||
|
|
success = false;
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
Md5Gen.Final(Digest);
|
||
|
|
for (int32 i = 0; i < 16; i++) {
|
||
|
|
MD5 += FString::Printf(TEXT("%02x"), Digest[i]);
|
||
|
|
}
|
||
|
|
|
||
|
|
success = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
void UFileFunctionsWebCom::stringToBase64String(FString string, FString& base64String) {
|
||
|
|
base64String.Empty();
|
||
|
|
FTCHARToUTF8 Convert(*string);
|
||
|
|
TArray<uint8> bytes;
|
||
|
|
bytes.Append((uint8*)Convert.Get(), Convert.Length());
|
||
|
|
base64String = FBase64::Encode(bytes);
|
||
|
|
}
|
||
|
|
|
||
|
|
void UFileFunctionsWebCom::base64StringToString(FString& string, FString base64String) {
|
||
|
|
string.Empty();
|
||
|
|
TArray<uint8> bytes;
|
||
|
|
if (FBase64::Decode(*base64String, bytes)) {
|
||
|
|
bytes.Add(0x00);// null-terminator
|
||
|
|
char* Data = (char*)bytes.GetData();
|
||
|
|
string = FString(UTF8_TO_TCHAR(Data));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void UFileFunctionsWebCom::bytesToBase64String(TArray<uint8> bytes, FString& base64String) {
|
||
|
|
base64String = FBase64::Encode(bytes);
|
||
|
|
}
|
||
|
|
|
||
|
|
TArray<uint8> UFileFunctionsWebCom::base64StringToBytes(EFileFunctionsWebComDirectoryType directoryType, FString base64String, bool& success) {
|
||
|
|
TArray<uint8> fileData;
|
||
|
|
success = FBase64::Decode(*base64String, fileData);
|
||
|
|
return fileData;
|
||
|
|
}
|
||
|
|
|
||
|
|
void UFileFunctionsWebCom::fileToBase64String(EFileFunctionsWebComDirectoryType directoryType, FString filePath, bool& success, FString& base64String, FString& fileName) {
|
||
|
|
base64String.Empty();
|
||
|
|
TArray<uint8> fileData;
|
||
|
|
FString dir = getCleanDirectory(directoryType, filePath);
|
||
|
|
if (!FFileHelper::LoadFileToArray(fileData, *dir)) {
|
||
|
|
success = false;
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
fileName = FPaths::GetCleanFilename(dir);
|
||
|
|
base64String = FBase64::Encode(fileData);
|
||
|
|
success = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool UFileFunctionsWebCom::fileExists(EFileFunctionsWebComDirectoryType directoryType, FString filePath) {
|
||
|
|
return FPaths::FileExists(*getCleanDirectory(directoryType, filePath));
|
||
|
|
}
|
||
|
|
|
||
|
|
bool UFileFunctionsWebCom::directoryExists(EFileFunctionsWebComDirectoryType directoryType, FString path) {
|
||
|
|
return FPaths::DirectoryExists(*getCleanDirectory(directoryType, path));
|
||
|
|
}
|
||
|
|
|
||
|
|
int64 UFileFunctionsWebCom::fileSize(EFileFunctionsWebComDirectoryType directoryType, FString filePath) {
|
||
|
|
return FPlatformFileManager::Get().GetPlatformFile().FileSize(*getCleanDirectory(directoryType, filePath));
|
||
|
|
}
|
||
|
|
|
||
|
|
bool UFileFunctionsWebCom::deleteFile(EFileFunctionsWebComDirectoryType directoryType, FString filePath) {
|
||
|
|
return FPlatformFileManager::Get().GetPlatformFile().DeleteFile(*getCleanDirectory(directoryType, filePath));
|
||
|
|
}
|
||
|
|
|
||
|
|
bool UFileFunctionsWebCom::deleteDirectory(EFileFunctionsWebComDirectoryType directoryType, FString filePath) {
|
||
|
|
return FPlatformFileManager::Get().GetPlatformFile().DeleteDirectory(*getCleanDirectory(directoryType, filePath));
|
||
|
|
}
|
||
|
|
|
||
|
|
bool UFileFunctionsWebCom::isReadOnly(EFileFunctionsWebComDirectoryType directoryType, FString filePath) {
|
||
|
|
return FPlatformFileManager::Get().GetPlatformFile().IsReadOnly(*getCleanDirectory(directoryType, filePath));
|
||
|
|
}
|
||
|
|
|
||
|
|
bool UFileFunctionsWebCom::moveFile(EFileFunctionsWebComDirectoryType directoryTypeTo, FString filePathTo, EFileFunctionsWebComDirectoryType directoryTypeFrom, FString filePathFrom) {
|
||
|
|
return FPlatformFileManager::Get().GetPlatformFile().MoveFile(*getCleanDirectory(directoryTypeTo, filePathTo), *getCleanDirectory(directoryTypeFrom, filePathFrom));
|
||
|
|
}
|
||
|
|
|
||
|
|
bool UFileFunctionsWebCom::setReadOnly(EFileFunctionsWebComDirectoryType directoryType, FString filePath, bool bNewReadOnlyValue) {
|
||
|
|
return FPlatformFileManager::Get().GetPlatformFile().SetReadOnly(*getCleanDirectory(directoryType, filePath), bNewReadOnlyValue);
|
||
|
|
}
|
||
|
|
|
||
|
|
FDateTime UFileFunctionsWebCom::getTimeStamp(EFileFunctionsWebComDirectoryType directoryType, FString filePath) {
|
||
|
|
return FPlatformFileManager::Get().GetPlatformFile().GetTimeStamp(*getCleanDirectory(directoryType, filePath));
|
||
|
|
}
|
||
|
|
|
||
|
|
void UFileFunctionsWebCom::setTimeStamp(EFileFunctionsWebComDirectoryType directoryType, FString filePath, FDateTime DateTime) {
|
||
|
|
FPlatformFileManager::Get().GetPlatformFile().SetTimeStamp(*getCleanDirectory(directoryType, filePath), DateTime);
|
||
|
|
}
|
||
|
|
|
||
|
|
FDateTime UFileFunctionsWebCom::getAccessTimeStamp(EFileFunctionsWebComDirectoryType directoryType, FString filePath) {
|
||
|
|
return FPlatformFileManager::Get().GetPlatformFile().GetAccessTimeStamp(*getCleanDirectory(directoryType, filePath));
|
||
|
|
}
|
||
|
|
|
||
|
|
FString UFileFunctionsWebCom::getFilenameOnDisk(EFileFunctionsWebComDirectoryType directoryType, FString filePath) {
|
||
|
|
return FPlatformFileManager::Get().GetPlatformFile().GetFilenameOnDisk(*getCleanDirectory(directoryType, filePath));
|
||
|
|
}
|
||
|
|
|
||
|
|
bool UFileFunctionsWebCom::createDirectory(EFileFunctionsWebComDirectoryType directoryType, FString path) {
|
||
|
|
return FPlatformFileManager::Get().GetPlatformFile().CreateDirectory(*getCleanDirectory(directoryType, path));
|
||
|
|
}
|