Skip to content

Commit

Permalink
Add command to publish/unpublish archives
Browse files Browse the repository at this point in the history
  • Loading branch information
LetrixZ committed Jun 24, 2024
1 parent f3c4cdb commit a5e429f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docker-compose.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ services:
- server
environment:
PUBLIC_CDN_URL: ${PUBLIC_CDN_URL}
API_KEY: ${API_KEY}
ports:
- '${WEB_HTTP_PORT}:5000'
db:
container_name: faccina-db
image: postgres
restart: unless-stopped
environment:
POSTGRES_DB: '${DB_NAME:-faccina}'
POSTGRES_USER: '${DB_USER:-faccina}'
Expand Down
7 changes: 7 additions & 0 deletions server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ It will replace any data already present in the archive. Only sources are merged
- `--id <ID_RANGE>`: Indicate one or multiple archive IDs to scrape metadata for.
- `--sleep <MS>`: Indicate how much milliseconds to wait between archives to avoid rate limits. Default is 1000.

### Publish and Unpublish archives

Run `./server publish <ID>` or `./server unpublish <ID>`.
`<ID>` can be a range. Example: `1-10,14,230-400`

This will change the visiblity of the given archives.

### Start server

Run the `./server` binary to run the server.
Expand Down
8 changes: 6 additions & 2 deletions server/src/archive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,15 @@ pub async fn index(
pool: &PgPool,
mp: &MultiProgress,
) -> anyhow::Result<bool> {
if !opts.reindex && sqlx::query_scalar!(
if !opts.reindex
&& sqlx::query_scalar!(
r#"SELECT id FROM archives WHERE path = $1 AND deleted_at IS NULL"#,
path.to_string()
)
.fetch_optional(pool)
.await?
.is_some() {
.is_some()
{
return Ok(false);
}

Expand Down Expand Up @@ -249,5 +251,7 @@ pub async fn index(
}
}

mp.suspend(|| info!("Indexed '{}' with ID {}", path.to_string(), archive_id));

Ok(true)
}
31 changes: 31 additions & 0 deletions server/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ pub enum Commands {
CalculateDimensions(CalculateDimensionsArgs),
#[command(about="Scrape metadata for archives.", long_about = None)]
Scrape(ScrapeArgs),
#[command(about="Show given archives from the search results.", long_about = None)]
Publish(PublishArgs),
#[command(about="Hide given archives from the search results.", long_about = None)]
Unpublish(PublishArgs),
}

#[derive(Args, Clone)]
Expand Down Expand Up @@ -138,6 +142,12 @@ pub struct ScrapeArgs {
pub sleep: u64,
}

#[derive(Args, Clone)]
pub struct PublishArgs {
#[arg(help = "List of archive IDs or range (ex: 1-10,14,230-400)")]
pub id: String,
}

async fn fetch_archives(
pool: &PgPool,
id_ranges: &Option<String>,
Expand Down Expand Up @@ -459,3 +469,24 @@ pub async fn scrape(args: ScrapeArgs) -> anyhow::Result<()> {

Ok(())
}

pub async fn pusblish(args: PublishArgs, publish: bool) -> anyhow::Result<()> {
let pool = db::get_pool().await?;

let mut qb = QueryBuilder::new("UPDATE archives SET ");

if publish {
qb.push("deleted_at = NULL");
} else {
qb.push("deleted_at = NOW()");
}

qb.push(" WHERE");

utils::add_id_ranges(&mut qb, &args.id);
let affected = qb.build().execute(&pool).await?;

info!("{} archives updated", affected.rows_affected());

Ok(())
}
2 changes: 2 additions & 0 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ async fn run() -> anyhow::Result<()> {
Commands::GenerateThumbnails(args) => cmd::generate_thumbnails(args.clone()).await?,
Commands::CalculateDimensions(args) => cmd::calculate_dimensions(args.clone()).await?,
Commands::Scrape(args) => cmd::scrape(args.clone()).await?,
Commands::Publish(args) => cmd::pusblish(args.clone(), true).await?,
Commands::Unpublish(args) => cmd::pusblish(args.clone(), false).await?,
}
}
None => api::start_server().await?,
Expand Down

0 comments on commit a5e429f

Please sign in to comment.