Skip to content

Commit

Permalink
WIP: export admin setting iframe
Browse files Browse the repository at this point in the history
Signed-off-by: codewithvk <[email protected]>
Change-Id: Ic1ce7cb70a499b76b13c5c74424f6bf2cde9b8f9
  • Loading branch information
codewithvk committed Nov 27, 2024
1 parent cea23f5 commit 313ebef
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
83 changes: 83 additions & 0 deletions browser/html/admin-settings.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Admin Setting Iframe</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 600px;
margin: 0 auto;
padding: 20px;
background-color: #f9f9f9;
}
#fileControls {
margin-top: 20px;
padding: 20px;
background-color: #f0f0f0;
border-radius: 5px;
}
button {
margin: 10px 5px;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
}

input[type="file"] {
margin: 10px 0;
}
</style>
</head>
<body>
<h2>Admin Setting Iframe</h2>
<div id="fileControls">
<label for="fontFile">Select Font File (TTF, OTF, WOFF):</label><br>
<input type="file" id="fontFile" accept=".ttf, .otf, .woff"><br>
<button class="upload" onclick="uploadFile()">Upload</button>
<button class="delete" onclick="deleteFile()">Delete</button>
<p id="fileStatus"></p>
</div>

<script>
function uploadFile() {
const fileInput = document.getElementById('fontFile');
const fileStatus = document.getElementById('fileStatus');

if (!fileInput.files.length) {
fileStatus.textContent = "No file selected for upload.";
return;
}

const fileName = fileInput.files[0].name;
fileStatus.textContent = `Uploading ${fileName}...`;

setTimeout(() => {
fileStatus.textContent = `File "${fileName}" uploaded successfully!`;

fileInput.value = '';
}, 2000);
}

function deleteFile() {
const fileInput = document.getElementById('fontFile');
const fileStatus = document.getElementById('fileStatus');

if (!fileInput.files.length) {
fileStatus.textContent = "No file selected to delete.";
return;
}

const fileName = fileInput.files[0].name;
fileStatus.textContent = `Deleting ${fileName}...`;

setTimeout(() => {
fileStatus.textContent = `File "${fileName}" deleted successfully!`;

fileInput.value = '';
}, 2000);
}
</script>
</body>
</html>
48 changes: 48 additions & 0 deletions wsd/FileServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,54 @@ void FileServerRequestHandler::handleRequest(const HTTPRequest& request,
}
}

if (endPoint == "admin-settings.html")
{
try
{
std::string templatePath = COOLWSD::FileServerRoot + "/browser/dist/admin-settings.html";

std::string htmlContent = readFileToString(templatePath);
if (htmlContent.empty())
{
throw std::runtime_error("HTML file is empty or could not be read");
}

Poco::Net::HTTPResponse httpResponse(Poco::Net::HTTPResponse::HTTP_OK);
httpResponse.setContentType("text/html");
httpResponse.setContentLength(htmlContent.size());

std::ostringstream headerStream;
headerStream << "HTTP/1.1 " << httpResponse.getStatus() << " " << httpResponse.getReason()
<< "\r\nContent-Type: " << httpResponse.getContentType()
<< "\r\nContent-Length: " << httpResponse.getContentLength() << "\r\n\r\n";
std::string headers = headerStream.str();

socket->send(headers.data(), headers.size());

socket->send(htmlContent.data(), htmlContent.size());
}
catch (const std::exception& e)
{
Poco::Net::HTTPResponse errorResponse(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR);
errorResponse.setContentType("text/plain");

std::string errorMessage = "Error: " + std::string(e.what());
errorResponse.setContentLength(errorMessage.size());

std::ostringstream errorHeaderStream;
errorHeaderStream << "HTTP/1.1 " << errorResponse.getStatus() << " " << errorResponse.getReason()
<< "\r\nContent-Type: " << errorResponse.getContentType()
<< "\r\nContent-Length: " << errorResponse.getContentLength() << "\r\n\r\n";
std::string errorHeaders = errorHeaderStream.str();

socket->send(errorHeaders.data(), errorHeaders.size());

socket->send(errorMessage.data(), errorMessage.size());
}

return;
}

// Is this a file we read at startup - if not; it's not for serving.
if (FileHash.find(relPath) == FileHash.end() &&
FileHash.find(relPath + ".br") == FileHash.end())
Expand Down

0 comments on commit 313ebef

Please sign in to comment.