458 {
459
460 if (mStudyToken.empty()) {
461 printf("Cannot create upload config: study token not set\n");
462 return false;
463 }
464
465 if (mUploadServerURL.empty()) {
466 printf("Cannot create upload config: upload server URL not set\n");
467 return false;
468 }
469
470
472 if (!test) {
473 printf("Cannot create upload config: test not found: %s\n", testName.c_str());
474 return false;
475 }
476
477
478 std::string host, page;
479 int port = 443;
480
481 std::string url = mUploadServerURL;
482
483
484 if (url.find("https://") == 0) {
485 url = url.substr(8);
486 port = 443;
487 } else if (url.find("http://") == 0) {
488 url = url.substr(7);
489 port = 80;
490 }
491
492
493 size_t slashPos = url.find('/');
494 if (slashPos != std::string::npos) {
495 host = url.substr(0, slashPos);
496 page = url.substr(slashPos);
497 } else {
498 host = url;
499 page = "/api/upload.php";
500 }
501
502
503 size_t colonPos = host.find(':');
504 if (colonPos != std::string::npos) {
505 std::string portStr = host.substr(colonPos + 1);
506 port = std::atoi(portStr.c_str());
507 host = host.substr(0, colonPos);
508 }
509
510
511 try {
513 j["host"] = host;
514 j["page"] = page;
515 j["subnumpage"] = "/api/getNewParticipantId.php";
516 j["port"] = port;
517 j["token"] = mStudyToken;
518 j["taskname"] = testName;
519 j["username"] = mStudyToken;
520 j["uploadpassword"] = mStudyToken;
521
522
524
525
526 std::string testDir = mPath + "/tests/" + testName;
527 try {
528 fs::create_directories(testDir);
529 } catch (const fs::filesystem_error& e) {
530 printf("Error creating test directory: %s\n", e.what());
531 return false;
532 }
533
534 std::ofstream file(uploadPath);
535 if (!file.is_open()) {
536 printf("Failed to create upload config file: %s\n", uploadPath.c_str());
537 return false;
538 }
539
540 file << j.dump(2);
541 file.close();
542
543 printf("Created upload config: %s\n", uploadPath.c_str());
544 return true;
545
546 } catch (const std::exception& e) {
547 printf("Error creating upload config: %s\n", e.what());
548 return false;
549 }
550}
Test * GetTest(const std::string &testName)
std::string GetUploadConfigPath(const std::string &testName) const