1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
use std::collections::HashMap;
use std::fs::File;
use std::io::Read;
use std::str::FromStr;
use std::sync::Arc;
use product_os_utilities::ProductOSUtilities;
use product_os_content::{ProductOSContentSite, ProductOSContentTemplate, ProductOSContentAsset, ProductOSContentAssetType, ProductOSContentMediaType, ProductOSContentFormat, ProductOSContentWorkflowState, ProductOSContentOverrideType};
use product_os_store::{ProductOSRelationalStore, ProductOSRelationalObject, Action, ProductOSStoreError, Target};
use product_os_store::types::chrono::Utc;
use mime_sniffer::MimeTypeSniffer;
use product_os_security::RandomGenerator;
#[tokio::main]
pub async fn setup_site_content(config: product_os_configuration::Configuration, store: Arc<ProductOSRelationalStore>) {
match internal_setup_site_content(config, store).await {
Ok(_) => tracing::info!("Content for site setup complete"),
Err(e) => tracing::error!("Error encountered setting up site content: {:?}", e)
};
}
async fn internal_setup_site_content(config: product_os_configuration::Configuration, store: Arc<ProductOSRelationalStore>) -> Result<(), ProductOSStoreError> {
let mut random_generator = product_os_security::RandomGenerator::new();
let default_site = ProductOSContentSite {
identifier: uuid::Uuid::from_str("2fc44c00-2979-48d1-8057-77ccb8248195").unwrap(),
domain: "localhost".to_string(),
base_path: "".to_string()
};
match store.upsert(default_site.relational_upsert(Target::Column("identifier".to_string()), Action::DoNothingIfExists)).await {
Ok(_) => tracing::info!("Add / update site to database"),
Err(e) => tracing::error!("Failed to add site: {:?}", e)
}
let content_setup_config = config.get_content_setup();
let root_path = content_setup_config.base_path.to_owned();
let add_templates = content_setup_config.add_templates;
let add_javascript = content_setup_config.add_javascript;
let add_stylesheet = content_setup_config.add_stylesheet;
let add_fonts = content_setup_config.add_fonts;
let add_images = content_setup_config.add_images;
let add_files = content_setup_config.add_files;
let add_error = content_setup_config.add_error;
let add_views = content_setup_config.add_views;
let add_applets = content_setup_config.add_applets;
let add_cms = content_setup_config.add_cms;
// Add templates content
match add_templates_to_database(store.clone(), add_templates).await {
Ok(_) => {},
Err(e) => { println!("Error adding content to database: {:?}", e); }
}
// Add javascript content
let mut javascript_base_path = String::from(root_path.clone());
javascript_base_path.push_str("/assets/js/");
match add_files_to_database(store.clone(), &mut random_generator,ProductOSContentAssetType::Source, "/js/".to_string(), javascript_base_path, true, add_javascript).await {
Ok(_) => {}
Err(e) => { println!("Error adding content to database: {:?}", e); }
}
// Add stylesheet content
let mut css_base_path = String::from(root_path.clone());
css_base_path.push_str("/assets/css/");
match add_files_to_database(store.clone(), &mut random_generator, ProductOSContentAssetType::Source, "/css/".to_string(), css_base_path, true, add_stylesheet).await {
Ok(_) => {}
Err(e) => { println!("Error adding content to database: {:?}", e); }
}
// Add fonts content
let mut fonts_base_path = String::from(root_path.clone());
fonts_base_path.push_str("/assets/fonts/");
match add_files_to_database(store.clone(), &mut random_generator, ProductOSContentAssetType::Media(ProductOSContentMediaType::Font), "/fonts/".to_string(), fonts_base_path, true, add_fonts).await {
Ok(_) => {}
Err(e) => { println!("Error adding content to database: {:?}", e); }
}
// Add images content
let mut images_base_path = String::from(root_path.clone());
images_base_path.push_str("/assets/images/");
match add_files_to_database(store.clone(), &mut random_generator, ProductOSContentAssetType::Media(ProductOSContentMediaType::Image), "/images/".to_string(), images_base_path, true, add_images).await {
Ok(_) => {}
Err(e) => { println!("Error adding content to database: {:?}", e); }
}
// Add general file content
let mut files_base_path = String::from(root_path.clone());
files_base_path.push_str("/assets/files/");
match add_files_to_database(store.clone(), &mut random_generator, ProductOSContentAssetType::Media(ProductOSContentMediaType::Unknown), "/".to_string(), files_base_path, true, add_files).await {
Ok(_) => {}
Err(e) => { println!("Error adding content to database: {:?}", e); }
}
// Add error content
let mut errors_base_path = String::from(root_path.clone());
errors_base_path.push_str("/error/");
match add_content_to_database(store.clone(), &mut random_generator,ProductOSContentAssetType::Error(0), "/error/".to_string(), errors_base_path, true, add_error, false).await {
Ok(_) => {}
Err(e) => { println!("Error adding content to database: {:?}", e); }
}
// Add application content
let mut views_base_path = String::from(root_path.clone());
views_base_path.push_str("/views/");
match add_content_to_database(store.clone(), &mut random_generator,ProductOSContentAssetType::View, "/".to_string(), views_base_path, true, add_views, false).await {
Ok(_) => {}
Err(e) => { println!("Error adding content to database: {:?}", e); }
}
// Add applet content
let mut applets_base_path = String::from(root_path.clone());
applets_base_path.push_str("/applets/");
match add_content_to_database(store.clone(), &mut random_generator,ProductOSContentAssetType::Applet, "/".to_string(), applets_base_path, true, add_applets, false).await {
Ok(_) => {}
Err(e) => { println!("Error adding content to database: {:?}", e); }
}
// Add CMS content
let mut cms_content_base_path = String::from(root_path.clone());
cms_content_base_path.push_str("/cms-content/");
match add_content_to_database(store.clone(), &mut random_generator,ProductOSContentAssetType::Content, "/".to_string(), cms_content_base_path, true, add_cms, true).await {
Ok(_) => {}
Err(e) => { println!("Error adding content to database: {:?}", e); }
}
Ok(())
}
async fn add_templates_to_database(store: Arc<ProductOSRelationalStore>, add_templates: bool) -> Result<(), ProductOSStoreError> {
if add_templates {
let site_layout = r###"
<!DOCTYPE html>
<html prefix="og: http://ogp.me/ns#">
<head>
<title> #{title} </title>
#{meta}
#{links}
#{sources}
<style>
#{styles}
</style>
#{tracking}
</head>
<body id="body">
<div id="product-os-header" class="product-os-header">
<div id="navigation">
<div id="navigation-left">
#{navigation}
</div>
<div id="navigation-right">
#{menu}
</div>
<div class="navigation-image-container">
<div class="lds-circle"><div></div></div></div>
</div>
</div>
<div id="product-os-content" class="product-os-content">
#{main_content}
</div>
<div id="product-os-footer" class="product-os-footer">
#{footer}
</div>
</body>
</html>
"###.to_string();
let mut default_content_settings = HashMap::new();
let mut default_content_elements = serde_json::Map::new();
let current_working_directory = ProductOSUtilities::get_current_working_dir();
let mut title_path = current_working_directory.clone();
title_path.push_str("/../../content/templates/title.html");
default_content_settings.insert("title".to_string(), title_path);
let mut meta_path = current_working_directory.clone();
meta_path.push_str("/../../content/templates/meta.html");
default_content_settings.insert("meta".to_string(), meta_path);
let mut links_path = current_working_directory.clone();
links_path.push_str("/../../content/templates/links.html");
default_content_settings.insert("links".to_string(), links_path);
let mut styles_path = current_working_directory.clone();
styles_path.push_str("/../../content/templates/styles.css");
default_content_settings.insert("styles".to_string(), styles_path);
let mut sources_path = current_working_directory.clone();
sources_path.push_str("/../../content/templates/sources.html");
default_content_settings.insert("sources".to_string(), sources_path);
let mut scripts_path = current_working_directory.clone();
scripts_path.push_str("/../../content/templates/scripts.js");
default_content_settings.insert("scripts".to_string(), scripts_path);
let mut tracking_path = current_working_directory.clone();
tracking_path.push_str("/../../content/templates/tracking.html");
default_content_settings.insert("tracking".to_string(), tracking_path);
let mut navigation_path = current_working_directory.clone();
navigation_path.push_str("/../../content/templates/navigation.html");
default_content_settings.insert("navigation".to_string(), navigation_path);
let mut menu_path = current_working_directory.clone();
menu_path.push_str("/../../content/templates/menu.html");
default_content_settings.insert("menu".to_string(), menu_path);
let mut footer_path = current_working_directory.clone();
footer_path.push_str("/../../content/templates/footer.html");
default_content_settings.insert("footer".to_string(), footer_path);
for (key, path) in default_content_settings {
match File::open(path.clone()) {
Ok(mut file) => {
let mut content = String::new();
match file.read_to_string(&mut content) {
Ok(_) => default_content_elements.insert(key, serde_json::Value::String(content)),
Err(_) => return Err(ProductOSStoreError::ConnectionError("Error reading file for content".to_string()))
}
}
Err(_) => return Err(ProductOSStoreError::ConnectionError(format!("Error getting path for content: {:?}", path)))
};
}
// default_content_elements.insert("main_content".to_string(), serde_json::Value::String("".to_string()));
let default_site_template = ProductOSContentTemplate {
identifier: uuid::Uuid::from_str("49ec21a5-ebb4-4a61-bcab-531d972dd466").unwrap(),
content_type: "text/html".to_string(),
layout: serde_json::Value::String(site_layout),
default_content_elements,
format: ProductOSContentFormat::Text
};
match store.upsert(default_site_template.relational_upsert(Target::Column("identifier".to_string()), Action::DoUpdateIfExists)).await {
Ok(_) => tracing::info!("Add / update template to database"),
Err(e) => return Err(ProductOSStoreError::ConnectionError(format!("Failed to add template: {:?}", e)))
}
}
Ok(())
}
async fn add_files_to_database(store: Arc<ProductOSRelationalStore>, random_generator: &mut RandomGenerator, mut content_kind: ProductOSContentAssetType, content_path: String, base_path: String, base_relative: bool, add_files: bool) -> Result<(), ProductOSStoreError> {
if add_files {
let mut files_base_path = String::new();
if base_relative {
files_base_path.push_str(ProductOSUtilities::get_current_working_dir().as_str());
}
files_base_path.push_str(base_path.as_str());
println!("Base Path for {:?} is: {:?}", content_path, files_base_path);
/*
let entries = walkdir::WalkDir::new(files_base_path.clone())
.min_depth(1)
.max_depth(5)
.follow_links(true)
.into_iter()
.filter_entry(|e| is_not_hidden(e));
//.filter_map(|v| v.ok());
//.for_each(|entry| );
for entry in entries {
println!("Entry for files: {:?}", entry);
}
*/
let entries = walkdir::WalkDir::new(files_base_path.clone())
.min_depth(1)
.max_depth(5)
.follow_links(true)
.into_iter()
.filter_entry(|e| is_not_hidden(e));
//.filter_map(|v| v.ok());
//.for_each(|entry| );
for entry in entries {
match entry {
Ok(entry) => {
let path = entry.path();
match path.to_str() {
None => {}
Some(s) => {
if entry.metadata().unwrap().is_file() {
let filename = s.split('/').collect::<Vec<&str>>().last().unwrap().to_owned();
let mut files_path = content_path.clone();
files_path.push_str(s.to_string().replace(files_base_path.clone().as_str(), "").as_str());
let mut files_data_path = String::from(files_base_path.clone());
files_data_path.push_str(s.to_string().replace(files_base_path.clone().as_str(), "").as_str());
match File::open(files_data_path.clone()) {
Ok(mut file) => {
let mut data = Vec::new();
match file.read_to_end(&mut data) {
Ok(_) => {
let mut content_type = match data.sniff_mime_type() {
None => None,
Some(ct) => Some(ct.to_string())
};
if content_type == None {
let guess = mime_guess::from_path(filename);
content_type = match guess.first_raw() {
None => None,
Some(ct) => Some(ct.to_string())
};
}
content_type = match content_type {
None => content_type,
Some(ct) => match ct.as_str() {
"application/javascript" => Some("text/javascript".to_string()),
_ => Some(ct)
}
};
match content_kind {
ProductOSContentAssetType::Media(ref media_type) => {
match media_type {
ProductOSContentMediaType::Unknown => {
if content_type.is_some() {
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types
if content_type.as_ref().unwrap().starts_with("image") { content_kind = ProductOSContentAssetType::Media(ProductOSContentMediaType::Image); }
else if content_type.as_ref().unwrap().starts_with("video") { content_kind = ProductOSContentAssetType::Media(ProductOSContentMediaType::Video); }
else if content_type.as_ref().unwrap().starts_with("audio") { content_kind = ProductOSContentAssetType::Media(ProductOSContentMediaType::Audio); }
else if content_type.as_ref().unwrap().starts_with("font") { content_kind = ProductOSContentAssetType::Media(ProductOSContentMediaType::Font); }
else if content_type.as_ref().unwrap().starts_with("text") { content_kind = ProductOSContentAssetType::Media(ProductOSContentMediaType::Text); }
else { content_kind = ProductOSContentAssetType::Media(ProductOSContentMediaType::Binary); }
}
}
_ => {}
}
}
ProductOSContentAssetType::Content | ProductOSContentAssetType::Error(_) => {}
_ => {}
}
let mut reference = random_generator.get_random_alphanumeric(10).to_uppercase();
while match store.get_one::<ProductOSContentAsset>(ProductOSContentAsset::relational_query_basic(product_os_store::Fields::All,
Some(product_os_store::Expression::EqualTo("reference".to_string(), reference.clone())))).await {
Ok(res) => {
match res {
None => Ok(()),
Some(_) => Err(())
}
}
Err(e) => {
tracing::error!("Reference check query error: {:?}", e);
Err(())
}
}.is_err() { reference = random_generator.get_random_alphanumeric(10).to_uppercase(); };
println!("Path and File details - file {:?}, reference {:?} content type {:?}, path: {:?}", filename, reference, content_type, files_data_path);
let files_page = ProductOSContentAsset {
identifier: uuid::Uuid::new_v4(),
site: uuid::Uuid::from_str("2fc44c00-2979-48d1-8057-77ccb8248195").unwrap(),
state: ProductOSContentWorkflowState::Live,
reference,
template: None,
data: Some(data),
format: ProductOSContentFormat::Binary,
path: files_path.clone(),
content_kind: Some(content_kind.clone()),
content_type,
markup: None,
auth_app_id: None,
scopes: None,
content_elements: Default::default(),
content_element_template_overrides: Default::default(),
created_at: Utc::now(),
updated_at: Utc::now()
};
match store.upsert(files_page.relational_upsert(Target::Constraint("site_content_asset".to_string()), Action::DoUpdateIfExists)).await {
Ok(_) => tracing::info!("Add / update files page to database"),
Err(e) => tracing::error!("Failed to add files page: {:?}", e)
}
}
Err(_) => return Err(ProductOSStoreError::ConnectionError("Error reading file for content".to_string()))
}
}
Err(_) => return Err(ProductOSStoreError::ConnectionError(format!("Error getting path for content: {:?}", path)))
};
}
}
}
}
Err(e) => return Err(ProductOSStoreError::ConnectionError(format!("Failed to read files folder entry: {:?}", e)))
}
}
}
Ok(())
}
async fn add_content_to_database(store: Arc<ProductOSRelationalStore>, random_generator: &mut RandomGenerator, asset_type: ProductOSContentAssetType, content_path: String, base_path: String, base_relative: bool, add_content: bool, include_root: bool) -> Result<(), ProductOSStoreError> {
if add_content {
let mut data_base_path = String::new();
if base_relative {
data_base_path.push_str(ProductOSUtilities::get_current_working_dir().as_str());
}
data_base_path.push_str(base_path.as_str());
let min_depth = if include_root { 0 } else { 1 };
let entries = walkdir::WalkDir::new(data_base_path.clone())
.min_depth(min_depth)
.max_depth(5)
.follow_links(true)
.into_iter()
.filter_entry(|e| is_directory(e));
//.filter_map(|v| v.ok());
//.for_each(|entry| );
for entry in entries {
match entry {
Ok(entry) => {
let path = entry.path();
match path.to_str() {
None => {}
Some(s) => {
let last_dir = s.split('/').collect::<Vec<&str>>().last().unwrap().to_owned();
if last_dir == "_files" {
let data_path = s.replace(data_base_path.as_str(), "");
let mut data_full_path = content_path.clone();
data_full_path.push_str(data_path.as_str());
let mut data_content_path = String::from(s);
data_content_path.push_str("/");
let mut data_full_path = content_path.clone();
data_full_path.push_str(data_path.as_str());
data_full_path.push_str("/assets/");
data_full_path = data_full_path.replace("/_files", "");
println!("Data full path dir is: {:?} and path {:?}", data_full_path, data_content_path);
match add_files_to_database(store.clone(), random_generator, ProductOSContentAssetType::Media(ProductOSContentMediaType::Unknown), data_full_path, data_content_path, false, true).await {
Ok(_) => {}
Err(_) => {}
};
}
else {
let data_path = s.replace(data_base_path.as_str(), "");
let mut data_full_path = content_path.clone();
data_full_path.push_str(data_path.as_str());
let mut data_content_path = String::from(data_base_path.clone());
data_content_path.push_str(data_path.as_str());
println!("Path details - content {:?}, path: {:?}", data_path, data_content_path);
/*
let content_entries = walkdir::WalkDir::new(data_content_path.clone())
.min_depth(0)
.max_depth(1)
.follow_links(true)
.into_iter();
for content_entry in content_entries {
if content_entry.as_ref().unwrap().metadata().unwrap().is_file() {
println!("Content Entry: {:?}", content_entry.unwrap().metadata());
}
}
*/
let content_entries = walkdir::WalkDir::new(data_content_path.clone())
.min_depth(0)
.max_depth(1)
.follow_links(true)
.into_iter()
.filter_entry(|e| is_not_hidden(e));
let mut content_elements = serde_json::Map::new();
let mut content_element_template_overrides = HashMap::new();
let mut content_type = Some("text/html".to_string());
let mut format = ProductOSContentFormat::Text;
for content_entry in content_entries {
match content_entry {
Ok(content_entry) => {
if content_entry.metadata().unwrap().is_file() {
let path = content_entry.path();
match path.to_str() {
None => {}
Some(content_path) => {
match File::open(content_path.clone()) {
Ok(mut file) => {
let mut content = String::new();
match file.read_to_string(&mut content) {
Ok(_) => {
let filename = content_path.split("/").collect::<Vec<&str>>().last().unwrap().split(".").collect::<Vec<&str>>();
let file_type = filename.last().unwrap().to_lowercase();
let content_name = filename.first().unwrap().to_owned().to_lowercase();
match file_type.as_str() {
"html" => {
content_type = Some("text/html".to_string());
format = ProductOSContentFormat::Text;
match content_name.as_str() {
"index" => { content_elements.insert("main_content".to_string(), serde_json::Value::String(content)); }
_ => { content_elements.insert(content_name, serde_json::Value::String(content)); }
}
}
"json" => {
match content_name.as_str() {
"config" => {
match serde_json::Value::from_str(content.as_str()) {
Ok(config) => {
match config.as_object() {
None => {}
Some(config_settings) => {
match config_settings.get("override") {
None => {}
Some(override_settings) => {
match override_settings.as_object() {
None => {}
Some(override_fields) => {
for (field, setting) in override_fields {
match setting.as_str() {
None => {}
Some(setting_value) => {
match setting_value {
"asset" => { content_element_template_overrides.insert(field.to_owned(), ProductOSContentOverrideType::Asset); },
"template" => { content_element_template_overrides.insert(field.to_owned(), ProductOSContentOverrideType::Template); },
"prepend" => { content_element_template_overrides.insert(field.to_owned(), ProductOSContentOverrideType::Prepend); },
"append" => { content_element_template_overrides.insert(field.to_owned(), ProductOSContentOverrideType::Append); },
_ => {}
}
}
}
}
}
}
}
}
}
}
}
Err(_) => tracing::info!("Config format invalid json - ignoring file: {:?}", content_path)
}
}
_ => {
content_type = Some("text/html".to_string());
format = ProductOSContentFormat::Block;
match serde_json::Value::from_str(content.as_str()) {
Ok(block) => {
match content_name.as_str() {
"index" => { content_elements.insert("main_content".to_string(), block); },
_ => { content_elements.insert(content_name, block); }
}
}
Err(_) => {}
}
}
}
}
_ => tracing::info!("Ignoring file: {:?}", content_path)
}
}
Err(_) => return Err(ProductOSStoreError::ConnectionError("Error reading file for content".to_string()))
}
}
Err(_) => tracing::error!("No content defined for path: {:?}", path)
};
}
}
}
}
Err(e) => return Err(ProductOSStoreError::ConnectionError(format!("Failed to read data folder entry: {:?}", e)))
}
}
let content_kind = match asset_type {
ProductOSContentAssetType::Content => Some(ProductOSContentAssetType::Content),
ProductOSContentAssetType::Source => Some(ProductOSContentAssetType::Source),
ProductOSContentAssetType::Media(ref mt) => Some(ProductOSContentAssetType::Media(mt.to_owned())),
ProductOSContentAssetType::View => Some(ProductOSContentAssetType::View),
ProductOSContentAssetType::Applet => Some(ProductOSContentAssetType::Applet),
ProductOSContentAssetType::Error(_) => Some(ProductOSContentAssetType::Error(data_path.to_string().parse::<i16>().unwrap())),
// _ => return Err(ProductOSStoreError::WriteError(format!("Illegal Content Kind specified: {:?}", data_content_path)))
};
let mut reference = random_generator.get_random_alphanumeric(10).to_uppercase();
while match store.get_one::<ProductOSContentAsset>(ProductOSContentAsset::relational_query_basic(product_os_store::Fields::All,
Some(product_os_store::Expression::EqualTo("reference".to_string(), reference.clone())))).await {
Ok(res) => {
match res {
None => Ok(()),
Some(_) => Err(())
}
}
Err(e) => {
tracing::error!("Reference check query error: {:?}", e);
Err(())
}
}.is_err() { reference = random_generator.get_random_alphanumeric(10).to_uppercase(); };
println!("Path and content element details - content {:?}, reference {:?} content kind {:?}, path: {:?}", data_full_path, reference, content_kind, content_path);
let data_page = ProductOSContentAsset {
identifier: uuid::Uuid::new_v4(),
site: uuid::Uuid::from_str("2fc44c00-2979-48d1-8057-77ccb8248195").unwrap(),
state: ProductOSContentWorkflowState::Live,
reference,
template: Some(uuid::Uuid::from_str("49ec21a5-ebb4-4a61-bcab-531d972dd466").unwrap()),
data: None,
path: data_full_path.clone(),
content_kind,
content_type,
markup: None,
auth_app_id: None,
scopes: None,
content_elements,
format,
content_element_template_overrides,
created_at: Utc::now(),
updated_at: Utc::now()
};
match store.upsert(data_page.relational_upsert(Target::Constraint("site_content_asset".to_string()), Action::DoUpdateIfExists)).await {
Ok(_) => tracing::info!("Add / update data page to database"),
Err(e) => tracing::error!("Failed to add data page: {:?}", e)
}
}
}
}
}
Err(e) => return Err(ProductOSStoreError::ConnectionError(format!("Failed to read data folder entry: {:?}", e)))
}
}
}
Ok(())
}
/*
fn is_not_directory_nor_hidden(entry: &walkdir::DirEntry) -> bool {
!entry
.metadata()
.unwrap()
.is_dir()
&&
entry
.file_name()
.to_str()
.map(|s| !s.starts_with("."))
.unwrap_or(false)
}
*/
fn is_not_hidden(entry: &walkdir::DirEntry) -> bool {
entry
.file_name()
.to_str()
.map(|s| !s.starts_with("."))
.unwrap_or(false)
}
fn is_directory(entry: &walkdir::DirEntry) -> bool {
entry
.metadata()
.unwrap()
.is_dir()
}
/*
fn is_file(entry: &walkdir::DirEntry) -> bool {
entry
.metadata()
.unwrap()
.is_file()
}
*/