Skip to content

Commit

Permalink
replace timezone with utc offset
Browse files Browse the repository at this point in the history
  • Loading branch information
jb-alvarado committed Sep 4, 2022
1 parent 8f5691f commit 6ff34e0
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ curl -X GET http://127.0.0.1:8787/api/channel/1 -H "Authorization: Bearer <TOKEN
"preview_url": "http://localhost/live/preview.m3u8",
"config_path": "/etc/ffplayout/ffplayout.yml",
"extra_extensions": "jpg,jpeg,png",
"timezone": "UTC",
"service": "ffplayout.service"
"service": "ffplayout.service",
"utc_offset": "+120"
}
```

Expand All @@ -86,7 +86,7 @@ curl -X GET http://127.0.0.1:8787/api/channels -H "Authorization: Bearer <TOKEN>
```BASH
curl -X PATCH http://127.0.0.1:8787/api/channel/1 -H "Content-Type: application/json" \
-d '{ "id": 1, "name": "Channel 1", "preview_url": "http://localhost/live/stream.m3u8", \
"config_path": "/etc/ffplayout/ffplayout.yml", "extra_extensions": "jpg,jpeg,png", "timezone": "Europe/Berlin"}' \
"config_path": "/etc/ffplayout/ffplayout.yml", "extra_extensions": "jpg,jpeg,png"}' \
-H "Authorization: Bearer <TOKEN>"
```

Expand All @@ -96,7 +96,7 @@ curl -X PATCH http://127.0.0.1:8787/api/channel/1 -H "Content-Type: application/
curl -X POST http://127.0.0.1:8787/api/channel/ -H "Content-Type: application/json" \
-d '{ "name": "Channel 2", "preview_url": "http://localhost/live/channel2.m3u8", \
"config_path": "/etc/ffplayout/channel2.yml", "extra_extensions": "jpg,jpeg,png",
"timezone": "Europe/Berlin", "service": "ffplayout@channel2.service" }' \
"service": "ffplayout@channel2.service" }' \
-H "Authorization: Bearer <TOKEN>"
```

Expand Down
2 changes: 1 addition & 1 deletion ffplayout-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description = "Rest API for ffplayout"
license = "GPL-3.0"
authors = ["Jonathan Baecker jonbae77@gmail.com"]
readme = "README.md"
version = "0.5.4"
version = "0.6.0"
edition = "2021"

[dependencies]
Expand Down
25 changes: 14 additions & 11 deletions ffplayout-api/src/utils/handles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use simplelog::*;
use sqlx::{migrate::MigrateDatabase, sqlite::SqliteQueryResult, Pool, Sqlite, SqlitePool};

use crate::utils::{
db_path,
db_path, local_utc_offset,
models::{Channel, TextPreset, User},
GlobalSettings,
};
Expand Down Expand Up @@ -40,7 +40,6 @@ async fn create_schema() -> Result<SqliteQueryResult, sqlx::Error> {
preview_url TEXT NOT NULL,
config_path TEXT NOT NULL,
extra_extensions TEXT NOT NULL,
timezone TEXT NOT NULL,
service TEXT NOT NULL,
UNIQUE(name, service)
);
Expand Down Expand Up @@ -111,8 +110,8 @@ pub async fn db_init(domain: Option<String>) -> Result<&'static str, Box<dyn std
SELECT RAISE(FAIL, 'Database is already initialized!');
END;
INSERT INTO global(secret) VALUES($1);
INSERT INTO channels(name, preview_url, config_path, extra_extensions, timezone, service)
VALUES('Channel 1', $2, '/etc/ffplayout/ffplayout.yml', 'jpg,jpeg,png', 'UTC', 'ffplayout.service');
INSERT INTO channels(name, preview_url, config_path, extra_extensions, service)
VALUES('Channel 1', $2, '/etc/ffplayout/ffplayout.yml', 'jpg,jpeg,png', 'ffplayout.service');
INSERT INTO roles(name) VALUES('admin'), ('user'), ('guest');
INSERT INTO presets(name, text, x, y, fontsize, line_spacing, fontcolor, box, boxcolor, boxborderw, alpha, channel_id)
VALUES('Default', 'Wellcome to ffplayout messenger!', '(w-text_w)/2', '(h-text_h)/2', '24', '4', '#ffffff@0xff', '0', '#000000@0x80', '4', '1.0', '1'),
Expand Down Expand Up @@ -150,19 +149,25 @@ pub async fn db_global() -> Result<GlobalSettings, sqlx::Error> {
pub async fn db_get_channel(id: &i64) -> Result<Channel, sqlx::Error> {
let conn = db_connection().await?;
let query = "SELECT * FROM channels WHERE id = $1";
let result: Channel = sqlx::query_as(query).bind(id).fetch_one(&conn).await?;
let mut result: Channel = sqlx::query_as(query).bind(id).fetch_one(&conn).await?;
conn.close().await;

result.utc_offset = local_utc_offset();

Ok(result)
}

pub async fn db_get_all_channels() -> Result<Vec<Channel>, sqlx::Error> {
let conn = db_connection().await?;
let query = "SELECT * FROM channels";
let result: Vec<Channel> = sqlx::query_as(query).fetch_all(&conn).await?;
let mut results: Vec<Channel> = sqlx::query_as(query).fetch_all(&conn).await?;
conn.close().await;

Ok(result)
for result in results.iter_mut() {
result.utc_offset = local_utc_offset();
}

Ok(results)
}

pub async fn db_update_channel(
Expand All @@ -171,14 +176,13 @@ pub async fn db_update_channel(
) -> Result<SqliteQueryResult, sqlx::Error> {
let conn = db_connection().await?;

let query = "UPDATE channels SET name = $2, preview_url = $3, config_path = $4, extra_extensions = $5, timezone = $6 WHERE id = $1";
let query = "UPDATE channels SET name = $2, preview_url = $3, config_path = $4, extra_extensions = $5 WHERE id = $1";
let result: SqliteQueryResult = sqlx::query(query)
.bind(id)
.bind(channel.name)
.bind(channel.preview_url)
.bind(channel.config_path)
.bind(channel.extra_extensions)
.bind(channel.timezone)
.execute(&conn)
.await?;
conn.close().await;
Expand All @@ -189,13 +193,12 @@ pub async fn db_update_channel(
pub async fn db_add_channel(channel: Channel) -> Result<Channel, sqlx::Error> {
let conn = db_connection().await?;

let query = "INSERT INTO channels (name, preview_url, config_path, extra_extensions, timezone, service) VALUES($1, $2, $3, $4, $5, $6)";
let query = "INSERT INTO channels (name, preview_url, config_path, extra_extensions, service) VALUES($1, $2, $3, $4, $5)";
let result = sqlx::query(query)
.bind(channel.name)
.bind(channel.preview_url)
.bind(channel.config_path)
.bind(channel.extra_extensions)
.bind(channel.timezone)
.bind(channel.service)
.execute(&conn)
.await?;
Expand Down
17 changes: 17 additions & 0 deletions ffplayout-api/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
path::Path,
};

use chrono::prelude::*;
use faccess::PathExt;
use once_cell::sync::OnceCell;
use rpassword::read_password;
Expand Down Expand Up @@ -222,3 +223,19 @@ pub async fn read_log_file(channel_id: &i64, date: &str) -> Result<String, Servi
"Requested log file not exists, or not readable.".to_string(),
))
}

pub fn local_utc_offset() -> i32 {
let mut offset = Local::now().format("%:z").to_string();
let operator = offset.remove(0);
let mut utc_offset = 0;

if let Some((r, f)) = offset.split_once(':') {
utc_offset = r.parse::<i32>().unwrap_or(0) * 60 + f.parse::<i32>().unwrap_or(0);

if operator == '-' && utc_offset > 0 {
utc_offset = -utc_offset;
}
}

utc_offset
}
5 changes: 4 additions & 1 deletion ffplayout-api/src/utils/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ pub struct Channel {
pub preview_url: String,
pub config_path: String,
pub extra_extensions: String,
pub timezone: String,
pub service: String,

#[sqlx(default)]
#[serde(default)]
pub utc_offset: i32,
}
8 changes: 4 additions & 4 deletions ffplayout-api/src/utils/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ async fn add_user(data: web::Json<User>) -> Result<impl Responder, ServiceError>
/// "preview_url": "http://localhost/live/preview.m3u8",
/// "config_path": "/etc/ffplayout/ffplayout.yml",
/// "extra_extensions": "jpg,jpeg,png",
/// "timezone": "UTC",
/// "service": "ffplayout.service"
/// "service": "ffplayout.service",
/// "utc_offset": "+120"
/// }
/// ```
#[get("/channel/{id}")]
Expand Down Expand Up @@ -269,7 +269,7 @@ async fn get_all_channels() -> Result<impl Responder, ServiceError> {
/// ```BASH
/// curl -X PATCH http://127.0.0.1:8787/api/channel/1 -H "Content-Type: application/json" \
/// -d '{ "id": 1, "name": "Channel 1", "preview_url": "http://localhost/live/stream.m3u8", \
/// "config_path": "/etc/ffplayout/ffplayout.yml", "extra_extensions": "jpg,jpeg,png", "timezone": "Europe/Berlin"}' \
/// "config_path": "/etc/ffplayout/ffplayout.yml", "extra_extensions": "jpg,jpeg,png"}' \
/// -H "Authorization: Bearer <TOKEN>"
/// ```
#[patch("/channel/{id}")]
Expand All @@ -291,7 +291,7 @@ async fn patch_channel(
/// curl -X POST http://127.0.0.1:8787/api/channel/ -H "Content-Type: application/json" \
/// -d '{ "name": "Channel 2", "preview_url": "http://localhost/live/channel2.m3u8", \
/// "config_path": "/etc/ffplayout/channel2.yml", "extra_extensions": "jpg,jpeg,png",
/// "timezone": "Europe/Berlin", "service": "ffplayout@channel2.service" }' \
/// "service": "ffplayout@channel2.service" }' \
/// -H "Authorization: Bearer <TOKEN>"
/// ```
#[post("/channel/")]
Expand Down

0 comments on commit 6ff34e0

Please sign in to comment.