mirror of https://gitee.com/bigwinds/arangodb
less pointer handling
This commit is contained in:
parent
fd9134e22b
commit
ba07532950
|
@ -42,7 +42,7 @@ RestUploadHandler::RestUploadHandler(GeneralRequest* request,
|
||||||
RestUploadHandler::~RestUploadHandler() {}
|
RestUploadHandler::~RestUploadHandler() {}
|
||||||
|
|
||||||
RestStatus RestUploadHandler::execute() {
|
RestStatus RestUploadHandler::execute() {
|
||||||
// cast is ok because http requst is required
|
// cast is ok because http request is required
|
||||||
HttpRequest* request = dynamic_cast<HttpRequest*>(_request.get());
|
HttpRequest* request = dynamic_cast<HttpRequest*>(_request.get());
|
||||||
|
|
||||||
if (request == nullptr) {
|
if (request == nullptr) {
|
||||||
|
@ -59,6 +59,8 @@ RestStatus RestUploadHandler::execute() {
|
||||||
return RestStatus::DONE;
|
return RestStatus::DONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string filenameString;
|
||||||
|
{
|
||||||
char* filename = nullptr;
|
char* filename = nullptr;
|
||||||
std::string errorMessage;
|
std::string errorMessage;
|
||||||
long systemError;
|
long systemError;
|
||||||
|
@ -71,14 +73,32 @@ RestStatus RestUploadHandler::execute() {
|
||||||
return RestStatus::FAIL;
|
return RestStatus::FAIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* relative = TRI_GetFilename(filename);
|
if (filename == nullptr) {
|
||||||
|
THROW_ARANGO_EXCEPTION(TRI_ERROR_OUT_OF_MEMORY);
|
||||||
|
}
|
||||||
|
|
||||||
|
filenameString.append(filename);
|
||||||
|
TRI_Free(TRI_CORE_MEM_ZONE, filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string relativeString;
|
||||||
|
{
|
||||||
|
char* relative = TRI_GetFilename(filenameString.c_str());
|
||||||
|
|
||||||
|
if (relative == nullptr) {
|
||||||
|
THROW_ARANGO_EXCEPTION(TRI_ERROR_OUT_OF_MEMORY);
|
||||||
|
}
|
||||||
|
|
||||||
|
relativeString.append(relative);
|
||||||
|
TRI_Free(TRI_CORE_MEM_ZONE, relative);
|
||||||
|
}
|
||||||
|
|
||||||
std::string const& bodyStr = request->body();
|
std::string const& bodyStr = request->body();
|
||||||
char const* body = bodyStr.c_str();
|
char const* body = bodyStr.c_str();
|
||||||
size_t bodySize = bodyStr.size();
|
size_t bodySize = bodyStr.size();
|
||||||
|
|
||||||
LOG(TRACE) << "saving uploaded file of length " << bodySize << " in file '"
|
LOG(TRACE) << "saving uploaded file of length " << bodySize << " in file '"
|
||||||
<< filename << "', relative '" << relative << "'";
|
<< filenameString << "', relative '" << relativeString << "'";
|
||||||
|
|
||||||
bool found;
|
bool found;
|
||||||
std::string const& value = request->value("multipart", found);
|
std::string const& value = request->value("multipart", found);
|
||||||
|
@ -88,8 +108,6 @@ RestStatus RestUploadHandler::execute() {
|
||||||
|
|
||||||
if (multiPart) {
|
if (multiPart) {
|
||||||
if (!parseMultiPart(body, bodySize)) {
|
if (!parseMultiPart(body, bodySize)) {
|
||||||
TRI_Free(TRI_CORE_MEM_ZONE, relative);
|
|
||||||
TRI_Free(TRI_CORE_MEM_ZONE, filename);
|
|
||||||
generateError(rest::ResponseCode::SERVER_ERROR,
|
generateError(rest::ResponseCode::SERVER_ERROR,
|
||||||
TRI_ERROR_INTERNAL, "invalid multipart request");
|
TRI_ERROR_INTERNAL, "invalid multipart request");
|
||||||
return RestStatus::FAIL;
|
return RestStatus::FAIL;
|
||||||
|
@ -98,18 +116,14 @@ RestStatus RestUploadHandler::execute() {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
FileUtils::spit(std::string(filename), body, bodySize);
|
FileUtils::spit(filenameString, body, bodySize);
|
||||||
TRI_Free(TRI_CORE_MEM_ZONE, filename);
|
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
TRI_Free(TRI_CORE_MEM_ZONE, relative);
|
|
||||||
TRI_Free(TRI_CORE_MEM_ZONE, filename);
|
|
||||||
generateError(rest::ResponseCode::SERVER_ERROR,
|
generateError(rest::ResponseCode::SERVER_ERROR,
|
||||||
TRI_ERROR_INTERNAL, "could not save file");
|
TRI_ERROR_INTERNAL, "could not save file");
|
||||||
return RestStatus::FAIL;
|
return RestStatus::FAIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* fullName = TRI_Concatenate2File("uploads", relative);
|
std::string fullName = basics::FileUtils::buildFilename("uploads", relativeString);
|
||||||
TRI_Free(TRI_CORE_MEM_ZONE, relative);
|
|
||||||
|
|
||||||
// create the response
|
// create the response
|
||||||
resetResponse(rest::ResponseCode::CREATED);
|
resetResponse(rest::ResponseCode::CREATED);
|
||||||
|
@ -118,7 +132,6 @@ RestStatus RestUploadHandler::execute() {
|
||||||
|
|
||||||
b.add(VPackValue(VPackValueType::Object));
|
b.add(VPackValue(VPackValueType::Object));
|
||||||
b.add("filename", VPackValue(fullName));
|
b.add("filename", VPackValue(fullName));
|
||||||
TRI_Free(TRI_CORE_MEM_ZONE, fullName);
|
|
||||||
b.close();
|
b.close();
|
||||||
|
|
||||||
VPackSlice s = b.slice();
|
VPackSlice s = b.slice();
|
||||||
|
|
Loading…
Reference in New Issue