1
0
mirror of https://git.ngram.ca/OpenJam/rc-servers synced 2026-08-23 23:08:52 +00:00

Implementing upload functionality for ArcAdapter and improving CRF search. (#62)

### Description

This PR implements upload functionality for ArcAdapter and improves CRF search.

adapter.rs

First, regarding the CRF search feature, when there is no search query (i.e., on the default page), there was a bug where sorting by added date and other sort options did not work correctly, so the default query was removed. Also, the added-date sort order was reversed, so that was fixed as well.

Next, about the upload feature.

First, in order to create cube_amounts, which stores in JSON the count of each part of the robot contained at the end of the ROBOT_CUBES table, we count—by type—the number of all byte sequences (part IDs) excluding the first 4 bytes (total part count) and excluding the last 4 bytes (coordinates) out of each following 8 bytes, and then create a JSON string where the keys are the part IDs converted to decimal and the values are the counts.

After that, we insert Base64-encoded data into cube_data and colour_data in ROBOT_CUBES, and insert the above JSON string into cube_amounts.

Next, regarding ROBOT_METADATA, the thumbnail URL is set to the internal CDN, and the actual thumbnail data is saved under data/robocraft/thumbnails.

The added date and expiration date are converted to match the same format as the other data before insertion, and the other data is inserted as-is.

arc.rs

If the requested file is not present in the ZIP file, it was changed to scan the JPG files existing in data/robocraft/thumbnails.

package_release.py

Made it so that data/robocraft/thumbnails is created.

### Game

Robocraft

### Please confirm

- [x] I am the legal owner or represent the owner of all work submitted
- [x] I consent to my submission being added to this FOSS project
- [x] This PR used LLMs to generate some or all of the code changes

Reviewed-on: https://git.ngram.ca/OpenJam/rc-servers/pulls/62
Co-authored-by: MaxSignal <kastera58@gmail.com>
Co-committed-by: MaxSignal <kastera58@gmail.com>
This commit is contained in:
MaxSignal
2025-12-13 19:23:07 +00:00
committed by NGnius
parent 0f9f67e5a9
commit aedec62b8d
7 changed files with 165 additions and 82 deletions

View File

@@ -8,10 +8,11 @@ static ZIP_FILE: std::sync::Mutex<Option<zip::read::ZipArchive<std::io::BufReade
pub async fn get(cli: Data<crate::cli::CliArgs>, id: Path<u32>) -> HttpResponse {
let id: u32 = *id;
let zip_path = std::path::PathBuf::from(&cli.data_robocraft).join("rc_archive_thumbnails.zip");
try_find_file(zip_path, id).await
let thumb_dir_path = std::path::PathBuf::from(&cli.data_robocraft).join("factorythumbnails");
try_find_file(zip_path, thumb_dir_path, id).await
}
async fn try_find_file(zip_path: std::path::PathBuf, id: u32) -> HttpResponse {
async fn try_find_file(zip_path: std::path::PathBuf, thumb_dir: std::path::PathBuf, id: u32) -> HttpResponse {
let result = tokio::task::spawn_blocking(move || get_file_in_zip(zip_path, id)).await.unwrap();
match result {
Ok(bytes) => {
@@ -20,6 +21,19 @@ async fn try_find_file(zip_path: std::path::PathBuf, id: u32) -> HttpResponse {
.append_header(("Content-Type", "image/jpeg"))
.body(bytes)
},
Err(zip::result::ZipError::FileNotFound) => {
let result = tokio::task::spawn_blocking(move || get_file_in_thumbnails(thumb_dir, id)).await.unwrap();
match result {
Ok(bytes) => {
log::debug!("Found id {} in thumbnails dir", id);
actix_web::HttpResponseBuilder::new(actix_web::http::StatusCode::OK)
.append_header(("Content-Type", "image/jpeg"))
.body(bytes)
},
Err(_) => actix_web::HttpResponseBuilder::new(actix_web::http::StatusCode::NOT_FOUND)
.body("file not found in zip archive or thumbnails dir".to_string()),
}
},
Err(e) => {
log::debug!("Failed to find id {} in factory arc: {}", id, e);
match e {
@@ -35,10 +49,6 @@ async fn try_find_file(zip_path: std::path::PathBuf, id: u32) -> HttpResponse {
actix_web::HttpResponseBuilder::new(actix_web::http::StatusCode::INTERNAL_SERVER_ERROR)
.body(format!("unsupported zip file: {}", e))
},
zip::result::ZipError::FileNotFound => {
actix_web::HttpResponseBuilder::new(actix_web::http::StatusCode::NOT_FOUND)
.body("file not found in zip archive".to_string())
},
zip::result::ZipError::InvalidPassword => {
actix_web::HttpResponseBuilder::new(actix_web::http::StatusCode::INTERNAL_SERVER_ERROR)
.body("invalid zip password".to_string())
@@ -68,6 +78,19 @@ fn get_file_in_zip(zip_path: std::path::PathBuf, id: u32) -> zip::result::ZipRes
}
fn get_file_in_thumbnails(dir: std::path::PathBuf, id: u32) -> std::io::Result<Vec<u8>> {
let prefix = format!("{} - ", id);
for ent in std::fs::read_dir(&dir)? {
let ent = ent?;
let name = ent.file_name().to_string_lossy().into_owned();
if name.starts_with(&prefix) && name.ends_with(".jpg") {
return std::fs::read(ent.path());
}
}
Err(std::io::Error::new(std::io::ErrorKind::NotFound, "thumbnail not found"))
}
fn read_file_with_prefix(prefix: &str, archive: &mut zip::read::ZipArchive<std::io::BufReader<std::fs::File>>) -> zip::result::ZipResult<Vec<u8>> {
let index = if let Some((index, _)) = archive.file_names().enumerate().find(|(_, name)| name.starts_with(prefix)) {
index