Compare commits
41 Commits
v0.1.0-dev
...
main
Author | SHA1 | Date | |
---|---|---|---|
94d28ee047 | |||
bd01dac544 | |||
f748d93b3f | |||
80762b81ae | |||
af4f1acb87 | |||
d5e24f9114 | |||
27e4b30d21 | |||
99bb72ffb2 | |||
111d16515f | |||
47bf24180b | |||
c16ad062d4 | |||
a779e13a77 | |||
fe61fe3efb | |||
f9f3dbda36 | |||
59ce1c294c | |||
cd1be017e5 | |||
2f05c20d4e | |||
e138bf7a2e | |||
29b806fd02 | |||
557264482f | |||
4e07ee5d0f | |||
eaa3c6f40e | |||
79eea81e28 | |||
006fa3a2d8 | |||
2f11ff9e89 | |||
5ced62ec7a | |||
817ea9fed2 | |||
ce1d522814 | |||
c04c6cdd6c | |||
f1375f5639 | |||
b41933e05e | |||
d1610d1331 | |||
83eafd0005 | |||
8bc49e781b | |||
f68d01d50f | |||
e3895b6d1a | |||
41001ec926 | |||
775a62d9cc | |||
a24dd6aba0 | |||
522395f0ad | |||
9bfc0a10ea |
18
.dockerignore.yaml
Normal file
18
.dockerignore.yaml
Normal file
@@ -0,0 +1,18 @@
|
||||
# Ignore build artifacts
|
||||
target/
|
||||
pkg/
|
||||
|
||||
# Ignore git directory
|
||||
.git/
|
||||
|
||||
.gitea/
|
||||
|
||||
# Ignore environment files (configure via docker-compose instead)
|
||||
.env*
|
||||
|
||||
# Ignore IDE/editor specific files
|
||||
.idea/
|
||||
.vscode/
|
||||
|
||||
# Ignore OS specific files
|
||||
*.DS_Store
|
4
.env.docker.sample
Normal file
4
.env.docker.sample
Normal file
@@ -0,0 +1,4 @@
|
||||
ROOT_DIRECTORY=/usr/local/bin
|
||||
ICARUS_BASE_API_URL=http://api:3000
|
||||
ICARUS_AUTH_BASE_API_URL=http://auth_api:3000
|
||||
SERVICE_PASSPHRASE=iUOo1fxshf3y1tUGn1yU8l9raPApHCdinW0VdCHdRFEjqhR3Bf02aZzsKbLtaDFH
|
4
.env.sample
Normal file
4
.env.sample
Normal file
@@ -0,0 +1,4 @@
|
||||
ROOT_DIRECTORY=/home/songparser/mydata
|
||||
ICARUS_BASE_API_URL=http://localhost:3000
|
||||
ICARUS_AUTH_BASE_API_URL=http://localhost:3001
|
||||
SERVICE_PASSPHRASE=iUOo1fxshf3y1tUGn1yU8l9raPApHCdinW0VdCHdRFEjqhR3Bf02aZzsKbLtaDFH
|
@@ -3,10 +3,7 @@ name: Release Tagging
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
- devel
|
||||
tags:
|
||||
- 'v*' # Trigger on tags matching v*
|
||||
|
||||
jobs:
|
||||
release:
|
||||
@@ -20,7 +17,7 @@ jobs:
|
||||
- name: Install Rust
|
||||
uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
toolchain: 1.85.0
|
||||
toolchain: 1.86.0
|
||||
components: cargo
|
||||
|
||||
- name: Extract Version from Cargo.toml
|
||||
@@ -31,7 +28,7 @@ jobs:
|
||||
BRANCH_REF="${GITHUB_REF}"
|
||||
BRANCH_NAME=$(echo "$BRANCH_REF" | cut -d '/' -f 3)
|
||||
PROJECT_TAG_RELEASE="v$VERSION-$BRANCH_NAME-$PROJECT_COMMIT_HASH"
|
||||
echo "::set-output name=project_tag_release::$PROJECT_TAG_RELEASE-111"
|
||||
echo "::set-output name=project_tag_release::$PROJECT_TAG_RELEASE-372"
|
||||
echo "Version: $VERSION"
|
||||
echo "Hash: $PROJECT_COMMIT_HASH"
|
||||
echo "Branch: $BRANCH_NAME"
|
||||
@@ -52,6 +49,3 @@ jobs:
|
||||
release_name: Release ${{ steps.version.outputs.project_tag_release }}
|
||||
body: |
|
||||
Release of version ${{ steps.version.outputs.project_tag_release }}
|
||||
# draft: false
|
||||
# prerelease: ${{ startsWith(github.ref, 'v') == false }} # prerelease if not a valid release tag
|
||||
|
||||
|
@@ -18,10 +18,10 @@ jobs:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||
with:
|
||||
toolchain: 1.85.0
|
||||
toolchain: 1.88.0
|
||||
- run: |
|
||||
mkdir -p ~/.ssh
|
||||
echo "${{ secreats.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key
|
||||
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key
|
||||
chmod 600 ~/.ssh/gitlab_deploy_key
|
||||
ssh-keyscan ${{ vars.MYHOST }} >> ~/.ssh/known_hosts
|
||||
eval $(ssh-agent -s)
|
||||
@@ -36,12 +36,12 @@ jobs:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||
with:
|
||||
toolchain: 1.85.0
|
||||
toolchain: 1.88.0
|
||||
- run: |
|
||||
mkdir -p ~/.ssh
|
||||
echo "${{ secreats.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key
|
||||
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key
|
||||
chmod 600 ~/.ssh/gitlab_deploy_key
|
||||
ssh-keyscan git.kundeng.us ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBLl/OZiKVDxwnyvMxa+rjKvDpKqTxH1GWuGuDPLmENGQMbTVulajZWr9x8Q1cotoJiHZkt7DA5vczcjB/4lwgWA= >> ~/.ssh/known_hosts
|
||||
ssh-keyscan ${{ vars.MYHOST }} >> ~/.ssh/known_hosts
|
||||
eval $(ssh-agent -s)
|
||||
ssh-add -v ~/.ssh/gitlab_deploy_key
|
||||
cargo test
|
||||
@@ -54,13 +54,13 @@ jobs:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||
with:
|
||||
toolchain: 1.85.0
|
||||
toolchain: 1.88.0
|
||||
- run: rustup component add rustfmt
|
||||
- run: |
|
||||
mkdir -p ~/.ssh
|
||||
echo "${{ secreats.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key
|
||||
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key
|
||||
chmod 600 ~/.ssh/gitlab_deploy_key
|
||||
ssh-keyscan git.kundeng.us ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBLl/OZiKVDxwnyvMxa+rjKvDpKqTxH1GWuGuDPLmENGQMbTVulajZWr9x8Q1cotoJiHZkt7DA5vczcjB/4lwgWA= >> ~/.ssh/known_hosts
|
||||
ssh-keyscan ${{ vars.MYHOST }} >> ~/.ssh/known_hosts
|
||||
eval $(ssh-agent -s)
|
||||
ssh-add -v ~/.ssh/gitlab_deploy_key
|
||||
cargo fmt --all -- --check
|
||||
@@ -73,13 +73,13 @@ jobs:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||
with:
|
||||
toolchain: 1.85.0
|
||||
toolchain: 1.88.0
|
||||
- run: rustup component add clippy
|
||||
- run: |
|
||||
mkdir -p ~/.ssh
|
||||
echo "${{ secreats.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key
|
||||
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key
|
||||
chmod 600 ~/.ssh/gitlab_deploy_key
|
||||
ssh-keyscan git.kundeng.us ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBLl/OZiKVDxwnyvMxa+rjKvDpKqTxH1GWuGuDPLmENGQMbTVulajZWr9x8Q1cotoJiHZkt7DA5vczcjB/4lwgWA= >> ~/.ssh/known_hosts
|
||||
ssh-keyscan ${{ vars.MYHOST }} >> ~/.ssh/known_hosts
|
||||
eval $(ssh-agent -s)
|
||||
ssh-add -v ~/.ssh/gitlab_deploy_key
|
||||
cargo clippy -- -D warnings
|
||||
@@ -92,12 +92,12 @@ jobs:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||
with:
|
||||
toolchain: 1.85.0
|
||||
toolchain: 1.88.0
|
||||
- run: |
|
||||
mkdir -p ~/.ssh
|
||||
echo "${{ secreats.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key
|
||||
echo "${{ secrets.MYREPO_TOKEN }}" > ~/.ssh/gitlab_deploy_key
|
||||
chmod 600 ~/.ssh/gitlab_deploy_key
|
||||
ssh-keyscan git.kundeng.us ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBLl/OZiKVDxwnyvMxa+rjKvDpKqTxH1GWuGuDPLmENGQMbTVulajZWr9x8Q1cotoJiHZkt7DA5vczcjB/4lwgWA= >> ~/.ssh/known_hosts
|
||||
ssh-keyscan ${{ vars.MYHOST }} >> ~/.ssh/known_hosts
|
||||
eval $(ssh-agent -s)
|
||||
ssh-add -v ~/.ssh/gitlab_deploy_key
|
||||
cargo build --release
|
||||
|
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,2 +1,4 @@
|
||||
/target
|
||||
Cargo.lock
|
||||
.env
|
||||
.env.local
|
||||
.env.docker
|
||||
|
2107
Cargo.lock
generated
Normal file
2107
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
15
Cargo.toml
15
Cargo.toml
@@ -1,7 +1,18 @@
|
||||
[package]
|
||||
name = "songparser"
|
||||
version = "0.1.0"
|
||||
version = "0.3.0"
|
||||
edition = "2024"
|
||||
rust-version = "1.88"
|
||||
|
||||
[dependencies]
|
||||
icarus-models = { git = "ssh://git@git.kundeng.us/phoenix/icarus-models.git", tag = "v0.1.14" }
|
||||
tokio = { version = "1.45.1", features = ["full"] }
|
||||
futures = { version = "0.3.31" }
|
||||
reqwest = { version = "0.12.20", features = ["json", "stream", "multipart"] }
|
||||
serde = { version = "1.0.219", features = ["derive"] }
|
||||
serde_json = { version = "1.0.140" }
|
||||
time = { version = "0.3.41", features = ["macros", "serde"] }
|
||||
uuid = { version = "1.17.0", features = ["v4", "serde"] }
|
||||
rand = { version = "0.9.1" }
|
||||
icarus_meta = { git = "ssh://git@git.kundeng.us/phoenix/icarus_meta.git", tag = "v0.3.0" }
|
||||
icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", tag = "v0.5.6" }
|
||||
icarus_envy = { git = "ssh://git@git.kundeng.us/phoenix/icarus_envy.git", tag = "v0.3.2" }
|
||||
|
63
Dockerfile
Normal file
63
Dockerfile
Normal file
@@ -0,0 +1,63 @@
|
||||
# Stage 1: Build the application
|
||||
# Use a specific Rust version for reproducibility. Choose one that matches your development environment.
|
||||
# Using slim variant for smaller base image
|
||||
FROM rust:1.88 as builder
|
||||
|
||||
# Set the working directory inside the container
|
||||
WORKDIR /usr/src/app
|
||||
|
||||
# Install build dependencies if needed (e.g., git for cloning)
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
pkg-config libssl3 \
|
||||
ca-certificates \
|
||||
openssh-client git \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# << --- ADD HOST KEY HERE --- >>
|
||||
# Replace 'yourgithost.com' with the actual hostname (e.g., github.com)
|
||||
RUN mkdir -p -m 0700 ~/.ssh && \
|
||||
ssh-keyscan git.kundeng.us >> ~/.ssh/known_hosts
|
||||
|
||||
# Copy Cargo manifests
|
||||
COPY Cargo.toml Cargo.lock ./
|
||||
|
||||
# Build *only* dependencies to leverage Docker cache
|
||||
# This dummy build caches dependencies as a separate layer
|
||||
RUN --mount=type=ssh mkdir src && \
|
||||
echo "fn main() {println!(\"if you see this, the build broke\")}" > src/main.rs && \
|
||||
cargo build --release --quiet && \
|
||||
rm -rf src target/release/deps/songparser* # Clean up dummy build artifacts (replace songparser)
|
||||
|
||||
# Copy the actual source code
|
||||
COPY src ./src
|
||||
# If you have other directories like `templates` or `static`, copy them too
|
||||
COPY .env ./.env
|
||||
|
||||
# << --- SSH MOUNT ADDED HERE --- >>
|
||||
# Build *only* dependencies to leverage Docker cache
|
||||
# This dummy build caches dependencies as a separate layer
|
||||
# Mount the SSH agent socket for this command
|
||||
RUN --mount=type=ssh \
|
||||
cargo build --release --quiet
|
||||
|
||||
# Stage 2: Create the final, smaller runtime image
|
||||
# Use a minimal base image like debian-slim or even distroless for security/size
|
||||
FROM ubuntu:24.04
|
||||
|
||||
# Install runtime dependencies if needed (e.g., SSL certificates)
|
||||
RUN apt-get update && apt-get install -y ca-certificates libssl-dev libssl3 && rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# Set the working directory
|
||||
WORKDIR /usr/local/bin
|
||||
|
||||
# Copy the compiled binary from the builder stage
|
||||
# Replace 'songparser' with the actual name of your binary (usually the crate name)
|
||||
COPY --from=builder /usr/src/app/target/release/songparser .
|
||||
|
||||
# Copy other necessary files like .env (if used for runtime config) or static assets
|
||||
# It's generally better to configure via environment variables in Docker though
|
||||
COPY --from=builder /usr/src/app/.env .
|
||||
|
||||
# Set the command to run your application
|
||||
# Ensure this matches the binary name copied above
|
||||
CMD ["./songparser"]
|
99
README.md
99
README.md
@@ -1,93 +1,20 @@
|
||||
# Songparser
|
||||
|
||||
A service that edits the metadata of a queued song and populates it with data.
|
||||
|
||||
|
||||
## Getting started
|
||||
This service can run as a regular service or a docker image. The easiest way to get quickly
|
||||
started is to run it with docker. Copy `.env.docker.sample` as `.env`. Ensure that
|
||||
`ROOT_DIRECTORY` is pointing to a directory that exists, not for the local filesystem, but
|
||||
for the filesystem on the docker image. The current directory that is listed will work, but
|
||||
it can be changed.
|
||||
|
||||
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
|
||||
The `SERVICE_PASSPHRASE` env variable should not be changed, but it could be changed. The
|
||||
value for this variable should match the value in the `passphrase` table. This would be
|
||||
found in the `icarus_auth` project.
|
||||
|
||||
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
|
||||
Ensure that the URLs for the two APIs are correctly set for the respective env variables
|
||||
`ICARUS_BASE_API_URL` for Icarus API and `ICARUS_AUTH_BASE_API_URL` for `icarus_auth`.
|
||||
|
||||
## Add your files
|
||||
|
||||
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
|
||||
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
|
||||
|
||||
```
|
||||
cd existing_repo
|
||||
git remote add origin https://gitlab.com/kdeng00/songparser.git
|
||||
git branch -M main
|
||||
git push -uf origin main
|
||||
```
|
||||
|
||||
## Integrate with your tools
|
||||
|
||||
- [ ] [Set up project integrations](https://gitlab.com/kdeng00/songparser/-/settings/integrations)
|
||||
|
||||
## Collaborate with your team
|
||||
|
||||
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
|
||||
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
|
||||
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
|
||||
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
|
||||
- [ ] [Set auto-merge](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
|
||||
|
||||
## Test and Deploy
|
||||
|
||||
Use the built-in continuous integration in GitLab.
|
||||
|
||||
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/)
|
||||
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing (SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
|
||||
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
|
||||
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
|
||||
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
|
||||
|
||||
***
|
||||
|
||||
# Editing this README
|
||||
|
||||
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thanks to [makeareadme.com](https://www.makeareadme.com/) for this template.
|
||||
|
||||
## Suggestions for a good README
|
||||
|
||||
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
|
||||
|
||||
## Name
|
||||
Choose a self-explaining name for your project.
|
||||
|
||||
## Description
|
||||
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
|
||||
|
||||
## Badges
|
||||
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
|
||||
|
||||
## Visuals
|
||||
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
|
||||
|
||||
## Installation
|
||||
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
|
||||
|
||||
## Usage
|
||||
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
|
||||
|
||||
## Support
|
||||
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
|
||||
|
||||
## Roadmap
|
||||
If you have ideas for releases in the future, it is a good idea to list them in the README.
|
||||
|
||||
## Contributing
|
||||
State if you are open to contributions and what your requirements are for accepting them.
|
||||
|
||||
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
|
||||
|
||||
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
|
||||
|
||||
## Authors and acknowledgment
|
||||
Show your appreciation to those who have contributed to the project.
|
||||
|
||||
## License
|
||||
For open source projects, say how it is licensed.
|
||||
|
||||
## Project status
|
||||
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
|
||||
If the values are properly set, next is to build the image. The docker image should be
|
||||
built from the main icarus web API.
|
||||
|
12
docker-compose.yaml
Normal file
12
docker-compose.yaml
Normal file
@@ -0,0 +1,12 @@
|
||||
version: '3.8' # Use a recent version
|
||||
|
||||
services:
|
||||
# Your Rust Application Service
|
||||
songparser:
|
||||
build: # Tells docker-compose to build the Dockerfile in the current directory
|
||||
context: .
|
||||
ssh: ["default"] # Uses host's SSH agent
|
||||
container_name: songparser # Optional: Give the container a specific name
|
||||
env_file:
|
||||
- .env
|
||||
restart: unless-stopped # Optional: Restart policy
|
169
src/api.rs
Normal file
169
src/api.rs
Normal file
@@ -0,0 +1,169 @@
|
||||
pub async fn fetch_next_queue_item(
|
||||
app: &crate::config::App,
|
||||
) -> Result<reqwest::Response, reqwest::Error> {
|
||||
let client = reqwest::Client::new();
|
||||
let fetch_endpoint = String::from("api/v2/song/queue/next");
|
||||
let api_url = format!("{}/{fetch_endpoint}", app.uri);
|
||||
let (key, header) = auth_header(app).await;
|
||||
|
||||
client.get(api_url).header(key, header).send().await
|
||||
}
|
||||
|
||||
pub async fn auth_header(
|
||||
app: &crate::config::App,
|
||||
) -> (reqwest::header::HeaderName, reqwest::header::HeaderValue) {
|
||||
let bearer = format!("Bearer {}", app.token.token);
|
||||
let header_value = reqwest::header::HeaderValue::from_str(&bearer).unwrap();
|
||||
(reqwest::header::AUTHORIZATION, header_value)
|
||||
}
|
||||
|
||||
pub mod parsing {
|
||||
use futures::StreamExt;
|
||||
|
||||
pub async fn parse_response_into_bytes(
|
||||
response: reqwest::Response,
|
||||
) -> Result<Vec<u8>, reqwest::Error> {
|
||||
// TODO: At some point, handle the flow if the size is small or
|
||||
// large
|
||||
let mut byte_stream = response.bytes_stream();
|
||||
let mut all_bytes = Vec::new();
|
||||
|
||||
while let Some(chunk) = byte_stream.next().await {
|
||||
let chunk = chunk?;
|
||||
all_bytes.extend_from_slice(&chunk);
|
||||
}
|
||||
|
||||
Ok(all_bytes)
|
||||
}
|
||||
}
|
||||
|
||||
pub mod fetch_song_queue_data {
|
||||
pub async fn get_data(
|
||||
app: &crate::config::App,
|
||||
id: &uuid::Uuid,
|
||||
) -> Result<reqwest::Response, reqwest::Error> {
|
||||
let client = reqwest::Client::new();
|
||||
let endpoint = String::from("api/v2/song/queue");
|
||||
let api_url = format!("{}/{endpoint}/{id}", app.uri);
|
||||
let (key, header) = super::auth_header(app).await;
|
||||
client.get(api_url).header(key, header).send().await
|
||||
}
|
||||
}
|
||||
|
||||
pub mod get_metadata_queue {
|
||||
pub async fn get(
|
||||
app: &crate::config::App,
|
||||
song_queue_id: &uuid::Uuid,
|
||||
) -> Result<reqwest::Response, reqwest::Error> {
|
||||
let client = reqwest::Client::new();
|
||||
let endpoint = String::from("api/v2/song/metadata/queue");
|
||||
let api_url = format!("{}/{endpoint}", app.uri);
|
||||
let (key, header) = super::auth_header(app).await;
|
||||
client
|
||||
.get(api_url)
|
||||
.query(&[("song_queue_id", song_queue_id)])
|
||||
.header(key, header)
|
||||
.send()
|
||||
.await
|
||||
}
|
||||
|
||||
pub mod response {
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct Metadata {
|
||||
pub song_queue_id: uuid::Uuid,
|
||||
pub album: String,
|
||||
pub album_artist: String,
|
||||
pub artist: String,
|
||||
pub disc: i32,
|
||||
pub disc_count: i32,
|
||||
pub duration: i64,
|
||||
pub genre: String,
|
||||
pub title: String,
|
||||
pub track: i32,
|
||||
pub track_count: i32,
|
||||
pub year: i32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct QueueItem {
|
||||
pub id: uuid::Uuid,
|
||||
pub metadata: Metadata,
|
||||
#[serde(with = "time::serde::rfc3339")]
|
||||
pub created_at: time::OffsetDateTime,
|
||||
pub song_queue_id: uuid::Uuid,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct Response {
|
||||
pub message: String,
|
||||
pub data: Vec<QueueItem>,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub mod get_coverart_queue {
|
||||
pub async fn get(
|
||||
app: &crate::config::App,
|
||||
song_queue_id: &uuid::Uuid,
|
||||
) -> Result<reqwest::Response, reqwest::Error> {
|
||||
let client = reqwest::Client::new();
|
||||
let endpoint = String::from("api/v2/coverart/queue");
|
||||
let api_url = format!("{}/{endpoint}", app.uri);
|
||||
let (key, header) = super::auth_header(app).await;
|
||||
client
|
||||
.get(api_url)
|
||||
.query(&[("song_queue_id", song_queue_id)])
|
||||
.header(key, header)
|
||||
.send()
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn get_data(
|
||||
app: &crate::config::App,
|
||||
coverart_queue_id: &uuid::Uuid,
|
||||
) -> Result<reqwest::Response, reqwest::Error> {
|
||||
let client = reqwest::Client::new();
|
||||
let endpoint = String::from("api/v2/coverart/queue/data");
|
||||
let api_url = format!("{}/{endpoint}/{coverart_queue_id}", app.uri);
|
||||
let (key, header) = super::auth_header(app).await;
|
||||
client.get(api_url).header(key, header).send().await
|
||||
}
|
||||
|
||||
pub mod response {
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct CoverArtQueue {
|
||||
pub id: uuid::Uuid,
|
||||
pub song_queue_id: uuid::Uuid,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct Response {
|
||||
pub message: String,
|
||||
pub data: Vec<CoverArtQueue>,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub mod service_token {
|
||||
pub mod response {
|
||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
||||
pub struct Response {
|
||||
pub message: String,
|
||||
pub data: Vec<icarus_models::login_result::LoginResult>,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub mod refresh_token {
|
||||
pub mod response {
|
||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
||||
pub struct Response {
|
||||
pub message: String,
|
||||
pub data: Vec<icarus_models::login_result::LoginResult>,
|
||||
}
|
||||
}
|
||||
}
|
6
src/config.rs
Normal file
6
src/config.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
#[derive(Default, Debug)]
|
||||
pub struct App {
|
||||
pub uri: String,
|
||||
pub auth_uri: String,
|
||||
pub token: icarus_models::login_result::LoginResult,
|
||||
}
|
608
src/main.rs
608
src/main.rs
@@ -1,3 +1,607 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
pub mod api;
|
||||
pub mod config;
|
||||
pub mod responses;
|
||||
pub mod the_rest;
|
||||
pub mod update_queued_song;
|
||||
pub mod util;
|
||||
|
||||
use std::io::Write;
|
||||
|
||||
pub const SECONDS_TO_SLEEP: u64 = 5;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut app = config::App {
|
||||
uri: icarus_envy::environment::get_icarus_base_api_url().await,
|
||||
auth_uri: icarus_envy::environment::get_icarus_auth_base_api_url().await,
|
||||
..Default::default()
|
||||
};
|
||||
println!("Base URL: {:?}", app.uri);
|
||||
println!("Auth URL: {:?}", app.auth_uri);
|
||||
|
||||
match auth::get_token(&app).await {
|
||||
Ok(login_result) => {
|
||||
app.token = login_result;
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("Error: {err:?}");
|
||||
std::process::exit(-1);
|
||||
}
|
||||
};
|
||||
|
||||
loop {
|
||||
println!("Token: {:?}", app.token);
|
||||
|
||||
if auth::did_token_expire(&app.token).await {
|
||||
println!("Token expired");
|
||||
app.token = match auth::get_refresh_token(&app).await {
|
||||
Ok(login_result) => login_result,
|
||||
Err(err) => {
|
||||
eprintln!("Error: {err:?}");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
println!("Token refreshed");
|
||||
println!("Refreshed token: {:?}", app.token);
|
||||
} else {
|
||||
println!("Token did not expire");
|
||||
}
|
||||
|
||||
match is_queue_empty(&app).await {
|
||||
Ok((empty, song_queue_item)) => {
|
||||
if !empty {
|
||||
println!("Queue is not empty");
|
||||
println!("SongQueueItem: {song_queue_item:?}");
|
||||
|
||||
let song_queue_id = song_queue_item.data[0].id;
|
||||
let user_id = song_queue_item.data[0].user_id;
|
||||
|
||||
// TODO: Do something with the result later
|
||||
match some_work(&app, &song_queue_id, &user_id).await {
|
||||
Ok((
|
||||
_song,
|
||||
_coverart,
|
||||
(song_queue_id, song_queue_path),
|
||||
(coverart_queue_id, coverart_queue_path),
|
||||
)) => {
|
||||
match wipe_data_from_queues(&app, &song_queue_id, &coverart_queue_id)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {
|
||||
match cleanup(&song_queue_path, &coverart_queue_path).await {
|
||||
Ok(_) => {
|
||||
println!("Successful cleanup");
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("Error: {err:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("Error: {err:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("Error: {err:?}");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println!("Queue is empty");
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("Error checking if queue is empty: {err:?}");
|
||||
}
|
||||
}
|
||||
|
||||
println!("Sleeping");
|
||||
tokio::time::sleep(tokio::time::Duration::from_secs(SECONDS_TO_SLEEP)).await;
|
||||
}
|
||||
}
|
||||
|
||||
mod auth {
|
||||
pub async fn get_token(
|
||||
app: &crate::config::App,
|
||||
) -> Result<icarus_models::login_result::LoginResult, std::io::Error> {
|
||||
let client = reqwest::Client::new();
|
||||
let endpoint = String::from("api/v2/service/login");
|
||||
let api_url = format!("{}/{endpoint}", app.auth_uri);
|
||||
|
||||
let payload = serde_json::json!({
|
||||
"passphrase": icarus_envy::environment::get_service_passphrase().await,
|
||||
});
|
||||
|
||||
match client.post(api_url).json(&payload).send().await {
|
||||
Ok(response) => match response
|
||||
.json::<crate::api::service_token::response::Response>()
|
||||
.await
|
||||
{
|
||||
Ok(resp) => {
|
||||
if resp.data.is_empty() {
|
||||
Err(std::io::Error::other(String::from("No token returned")))
|
||||
} else {
|
||||
Ok(resp.data[0].clone())
|
||||
}
|
||||
}
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
},
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Might want to put the functionality within icarus_models at some point
|
||||
pub async fn did_token_expire(login_result: &icarus_models::login_result::LoginResult) -> bool {
|
||||
let current_time = time::OffsetDateTime::now_utc();
|
||||
let expire_time =
|
||||
time::OffsetDateTime::from_unix_timestamp(login_result.expiration).unwrap();
|
||||
current_time > expire_time
|
||||
}
|
||||
|
||||
pub async fn get_refresh_token(
|
||||
app: &crate::config::App,
|
||||
) -> Result<icarus_models::login_result::LoginResult, std::io::Error> {
|
||||
let client = reqwest::Client::new();
|
||||
let endpoint = String::from("api/v2/token/refresh");
|
||||
let api_url = format!("{}/{endpoint}", app.auth_uri);
|
||||
|
||||
let payload = serde_json::json!({
|
||||
"access_token": app.token.token
|
||||
});
|
||||
|
||||
match client.post(api_url).json(&payload).send().await {
|
||||
Ok(response) => match response
|
||||
.json::<crate::api::refresh_token::response::Response>()
|
||||
.await
|
||||
{
|
||||
Ok(resp) => {
|
||||
if resp.data.is_empty() {
|
||||
Err(std::io::Error::other(String::from("No token returned")))
|
||||
} else {
|
||||
Ok(resp.data[0].clone())
|
||||
}
|
||||
}
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
},
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn wipe_data_from_queues(
|
||||
app: &config::App,
|
||||
song_queue_id: &uuid::Uuid,
|
||||
coverart_queue_id: &uuid::Uuid,
|
||||
) -> Result<(), std::io::Error> {
|
||||
match the_rest::wipe_data::song_queue::wipe_data(app, song_queue_id).await {
|
||||
Ok(response) => match response
|
||||
.json::<the_rest::wipe_data::song_queue::response::Response>()
|
||||
.await
|
||||
{
|
||||
Ok(_resp) => {
|
||||
match the_rest::wipe_data::coverart_queue::wipe_data(app, coverart_queue_id).await {
|
||||
Ok(inner_response) => match inner_response
|
||||
.json::<the_rest::wipe_data::coverart_queue::response::Response>()
|
||||
.await
|
||||
{
|
||||
Ok(_inner_resp) => {
|
||||
println!("Wiped data from CoverArt queue");
|
||||
println!("Resp: {_inner_resp:?}");
|
||||
Ok(())
|
||||
}
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
},
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
}
|
||||
}
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
},
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
async fn cleanup(
|
||||
song_queue_path: &String,
|
||||
coverart_queue_path: &String,
|
||||
) -> Result<(), std::io::Error> {
|
||||
match the_rest::cleanup::clean_song_queue(song_queue_path) {
|
||||
Ok(_) => {}
|
||||
Err(err) => {
|
||||
eprintln!("Error: Problem cleaning up SongQueue files {err:?}");
|
||||
}
|
||||
}
|
||||
|
||||
match the_rest::cleanup::clean_coverart_queue(coverart_queue_path) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
async fn is_queue_empty(
|
||||
app: &config::App,
|
||||
) -> Result<(bool, responses::fetch_next_queue_item::SongQueueItem), reqwest::Error> {
|
||||
match api::fetch_next_queue_item(app).await {
|
||||
Ok(response) => {
|
||||
match response
|
||||
.json::<responses::fetch_next_queue_item::SongQueueItem>()
|
||||
.await
|
||||
{
|
||||
Ok(response) => {
|
||||
if response.data.is_empty() {
|
||||
Ok((true, response))
|
||||
} else {
|
||||
Ok((false, response))
|
||||
}
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
async fn some_work(
|
||||
app: &crate::config::App,
|
||||
song_queue_id: &uuid::Uuid,
|
||||
user_id: &uuid::Uuid,
|
||||
) -> Result<
|
||||
(
|
||||
icarus_models::song::Song,
|
||||
icarus_models::coverart::CoverArt,
|
||||
(uuid::Uuid, String),
|
||||
(uuid::Uuid, String),
|
||||
),
|
||||
std::io::Error,
|
||||
> {
|
||||
match prep_song(app, song_queue_id).await {
|
||||
Ok((song_queue_path, coverart_queue_path, metadata, coverart_queue_id)) => {
|
||||
match apply_metadata(&song_queue_path, &coverart_queue_path, &metadata).await {
|
||||
Ok(_applied) => {
|
||||
match update_queued_song::update_queued_song(
|
||||
app,
|
||||
&song_queue_path,
|
||||
song_queue_id,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(response) => {
|
||||
match response
|
||||
.json::<update_queued_song::response::Response>()
|
||||
.await
|
||||
{
|
||||
Ok(_inner_response) => {
|
||||
println!("Response: {_inner_response:?}");
|
||||
|
||||
// TODO: Place this somewhere else
|
||||
let song_type = String::from("flac");
|
||||
|
||||
match the_rest::create_song::create(
|
||||
app, &metadata, user_id, &song_type,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(response) => match response
|
||||
.json::<the_rest::create_song::response::Response>()
|
||||
.await
|
||||
{
|
||||
Ok(resp) => {
|
||||
println!("Response: {resp:?}");
|
||||
|
||||
let song = &resp.data[0];
|
||||
match the_rest::create_coverart::create(app, &song.id, &coverart_queue_id).await {
|
||||
Ok(response) => match response.json::<the_rest::create_coverart::response::Response>().await {
|
||||
Ok(resp) => {
|
||||
println!("CoverArt sent and successfully parsed response");
|
||||
println!("json: {resp:?}");
|
||||
let coverart = &resp.data[0];
|
||||
Ok((song.clone(), coverart.clone(), (metadata.song_queue_id, song_queue_path), (coverart_queue_id, coverart_queue_path)))
|
||||
}
|
||||
Err(err) => {
|
||||
Err(std::io::Error::other(err.to_string()))
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
Err(std::io::Error::other(err.to_string()))
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
},
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
}
|
||||
}
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
}
|
||||
}
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
}
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
async fn prep_song(
|
||||
app: &crate::config::App,
|
||||
song_queue_id: &uuid::Uuid,
|
||||
) -> Result<
|
||||
(
|
||||
String,
|
||||
String,
|
||||
api::get_metadata_queue::response::Metadata,
|
||||
uuid::Uuid,
|
||||
),
|
||||
reqwest::Error,
|
||||
> {
|
||||
match api::fetch_song_queue_data::get_data(app, song_queue_id).await {
|
||||
Ok(response) => {
|
||||
// Process data here...
|
||||
match api::parsing::parse_response_into_bytes(response).await {
|
||||
Ok(song_bytes) => {
|
||||
let (directory, filename) = generate_song_queue_dir_and_filename().await;
|
||||
let song_queue_path = save_file_to_fs(&directory, &filename, &song_bytes).await;
|
||||
|
||||
println!("Saved at: {song_queue_path:?}");
|
||||
|
||||
match api::get_metadata_queue::get(app, song_queue_id).await {
|
||||
Ok(response) => {
|
||||
match response
|
||||
.json::<api::get_metadata_queue::response::Response>()
|
||||
.await
|
||||
{
|
||||
Ok(response) => {
|
||||
let id = &response.data[0].id;
|
||||
let created_at = &response.data[0].created_at;
|
||||
let metadata = &response.data[0].metadata;
|
||||
println!("Id: {id:?}");
|
||||
println!("Metadata: {metadata:?}");
|
||||
println!("Created at: {created_at:?}");
|
||||
|
||||
println!("Getting coverart queue");
|
||||
match api::get_coverart_queue::get(app, song_queue_id).await {
|
||||
Ok(response) => {
|
||||
match response.json::<api::get_coverart_queue::response::Response>().await {
|
||||
Ok(response) => {
|
||||
let coverart_queue_id = &response.data[0].id;
|
||||
println!("Coverart queue Id: {coverart_queue_id:?}");
|
||||
|
||||
match api::get_coverart_queue::get_data(app, coverart_queue_id).await {
|
||||
Ok(response) => match api::parsing::parse_response_into_bytes(response).await {
|
||||
Ok(coverart_queue_bytes) => {
|
||||
let (directory, filename) = generate_coverart_queue_dir_and_filename().await;
|
||||
let coverart_queue_path = save_file_to_fs(&directory, &filename, &coverart_queue_bytes).await;
|
||||
|
||||
println!("Saved coverart queue file at: {coverart_queue_path:?}");
|
||||
|
||||
let c_path = util::path_buf_to_string(&coverart_queue_path);
|
||||
let s_path = util::path_buf_to_string(&song_queue_path);
|
||||
Ok((s_path, c_path, metadata.clone(), *coverart_queue_id))
|
||||
}
|
||||
Err(err) => {
|
||||
Err(err)
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
Err(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
Err(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Consider having something like this in icarus_models
|
||||
pub async fn generate_song_queue_dir_and_filename() -> (String, String) {
|
||||
let mut song = icarus_models::song::Song::default();
|
||||
song.filename = song.generate_filename(icarus_models::types::MusicTypes::FlacExtension, true);
|
||||
|
||||
song.directory = icarus_envy::environment::get_root_directory().await;
|
||||
|
||||
(song.directory, song.filename)
|
||||
}
|
||||
|
||||
// TODO: Consider having something like this in icarus_models
|
||||
pub async fn generate_coverart_queue_dir_and_filename() -> (String, String) {
|
||||
use rand::Rng;
|
||||
|
||||
let mut filename: String = String::new();
|
||||
let filename_len = 10;
|
||||
|
||||
let some_chars: String = String::from("abcdefghij0123456789");
|
||||
let mut rng = rand::rng();
|
||||
|
||||
for _i in 0..filename_len {
|
||||
let random_number: i32 = rng.random_range(0..=19);
|
||||
let index = random_number as usize;
|
||||
let rando_char = some_chars.chars().nth(index);
|
||||
|
||||
if let Some(c) = rando_char {
|
||||
filename.push(c);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Do not hard code the file extension
|
||||
filename += ".jpeg";
|
||||
|
||||
// TODO: Consider separating song and coverart when saving to the filesystem
|
||||
let directory = icarus_envy::environment::get_root_directory().await;
|
||||
|
||||
(directory, filename)
|
||||
}
|
||||
|
||||
// TODO: Check to see if this is available in icarus_models
|
||||
pub async fn save_file_to_fs(
|
||||
directory: &String,
|
||||
filename: &String,
|
||||
data: &[u8],
|
||||
) -> std::path::PathBuf {
|
||||
// TODO: Add function to save bytes to a file in icarus_models
|
||||
// repo
|
||||
let dir = std::path::Path::new(directory);
|
||||
let save_path = dir.join(filename);
|
||||
|
||||
let mut file = std::fs::File::create(&save_path).unwrap();
|
||||
file.write_all(data).unwrap();
|
||||
|
||||
save_path
|
||||
}
|
||||
|
||||
pub async fn apply_metadata(
|
||||
song_queue_path: &String,
|
||||
coverart_queue_path: &String,
|
||||
metadata: &api::get_metadata_queue::response::Metadata,
|
||||
) -> Result<bool, std::io::Error> {
|
||||
// Apply metadata fields
|
||||
let types = icarus_meta::types::all_metadata_types();
|
||||
|
||||
for t in types {
|
||||
match t {
|
||||
icarus_meta::types::Type::Album => {
|
||||
let meta_type =
|
||||
icarus_meta::types::MetadataType::from_string(metadata.album.clone());
|
||||
match icarus_meta::meta::metadata::set_meta_value(t, song_queue_path, meta_type) {
|
||||
Ok(_) => {}
|
||||
Err(_err) => {
|
||||
return Err(_err);
|
||||
}
|
||||
}
|
||||
}
|
||||
icarus_meta::types::Type::AlbumArtist => {
|
||||
let meta_type =
|
||||
icarus_meta::types::MetadataType::from_string(metadata.album_artist.clone());
|
||||
match icarus_meta::meta::metadata::set_meta_value(t, song_queue_path, meta_type) {
|
||||
Ok(_) => {}
|
||||
Err(_err) => {
|
||||
return Err(_err);
|
||||
}
|
||||
}
|
||||
}
|
||||
icarus_meta::types::Type::Artist => {
|
||||
let meta_type =
|
||||
icarus_meta::types::MetadataType::from_string(metadata.artist.clone());
|
||||
match icarus_meta::meta::metadata::set_meta_value(t, song_queue_path, meta_type) {
|
||||
Ok(_) => {}
|
||||
Err(_err) => {
|
||||
return Err(_err);
|
||||
}
|
||||
}
|
||||
}
|
||||
icarus_meta::types::Type::Date => {
|
||||
// TODO: Do something about this discrepancy
|
||||
let meta_type =
|
||||
icarus_meta::types::MetadataType::from_string(metadata.year.to_string());
|
||||
match icarus_meta::meta::metadata::set_meta_value(t, song_queue_path, meta_type) {
|
||||
Ok(_) => {}
|
||||
Err(_err) => {
|
||||
return Err(_err);
|
||||
}
|
||||
}
|
||||
}
|
||||
icarus_meta::types::Type::Disc => {
|
||||
let meta_type = icarus_meta::types::MetadataType::from_int(metadata.disc);
|
||||
match icarus_meta::meta::metadata::set_meta_value(t, song_queue_path, meta_type) {
|
||||
Ok(_) => {}
|
||||
Err(_err) => {
|
||||
return Err(_err);
|
||||
}
|
||||
}
|
||||
}
|
||||
icarus_meta::types::Type::Genre => {
|
||||
let meta_type =
|
||||
icarus_meta::types::MetadataType::from_string(metadata.genre.clone());
|
||||
match icarus_meta::meta::metadata::set_meta_value(t, song_queue_path, meta_type) {
|
||||
Ok(_) => {}
|
||||
Err(_err) => {
|
||||
return Err(_err);
|
||||
}
|
||||
}
|
||||
}
|
||||
icarus_meta::types::Type::Title => {
|
||||
let meta_type =
|
||||
icarus_meta::types::MetadataType::from_string(metadata.title.clone());
|
||||
match icarus_meta::meta::metadata::set_meta_value(t, song_queue_path, meta_type) {
|
||||
Ok(_) => {}
|
||||
Err(_err) => {
|
||||
return Err(_err);
|
||||
}
|
||||
}
|
||||
}
|
||||
icarus_meta::types::Type::Track => {
|
||||
let meta_type = icarus_meta::types::MetadataType::from_int(metadata.track);
|
||||
match icarus_meta::meta::metadata::set_meta_value(t, song_queue_path, meta_type) {
|
||||
Ok(_) => {}
|
||||
Err(_err) => {
|
||||
return Err(_err);
|
||||
}
|
||||
}
|
||||
}
|
||||
icarus_meta::types::Type::TrackCount => {
|
||||
let meta_type = icarus_meta::types::MetadataType::from_int(metadata.track_count);
|
||||
match icarus_meta::meta::metadata::set_meta_value(t, song_queue_path, meta_type) {
|
||||
Ok(_) => {}
|
||||
Err(_err) => {
|
||||
return Err(_err);
|
||||
}
|
||||
}
|
||||
}
|
||||
icarus_meta::types::Type::DiscCount => {
|
||||
let meta_type = icarus_meta::types::MetadataType::from_int(metadata.disc_count);
|
||||
match icarus_meta::meta::metadata::set_meta_value(t, song_queue_path, meta_type) {
|
||||
Ok(_) => {}
|
||||
Err(_err) => {
|
||||
return Err(_err);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Apply coverart
|
||||
match icarus_meta::meta::coverart::contains_coverart(song_queue_path) {
|
||||
Ok((exists, size)) => {
|
||||
if exists {
|
||||
println!("Coverart exists: {size:?} size");
|
||||
match icarus_meta::meta::coverart::remove_coverart(song_queue_path) {
|
||||
Ok(_data) => {}
|
||||
Err(err) => {
|
||||
return Err(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
match icarus_meta::meta::coverart::set_coverart(song_queue_path, coverart_queue_path) {
|
||||
Ok(_data) => {
|
||||
if _data.is_empty() {
|
||||
println!("There was an issue");
|
||||
Ok(false)
|
||||
} else {
|
||||
println!("Success in applying coverart to song");
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
17
src/responses.rs
Normal file
17
src/responses.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
pub mod fetch_next_queue_item {
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct QueueItem {
|
||||
pub id: uuid::Uuid,
|
||||
pub filename: String,
|
||||
pub status: String,
|
||||
pub user_id: uuid::Uuid,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct SongQueueItem {
|
||||
pub message: String,
|
||||
pub data: Vec<QueueItem>,
|
||||
}
|
||||
}
|
179
src/the_rest.rs
Normal file
179
src/the_rest.rs
Normal file
@@ -0,0 +1,179 @@
|
||||
// TODO: Refactor this file when this app is functional
|
||||
|
||||
pub mod create_song {
|
||||
pub async fn create(
|
||||
app: &crate::config::App,
|
||||
metadata_queue: &crate::api::get_metadata_queue::response::Metadata,
|
||||
user_id: &uuid::Uuid,
|
||||
song_type: &String,
|
||||
) -> Result<reqwest::Response, reqwest::Error> {
|
||||
let payload = serde_json::json!(
|
||||
{
|
||||
"album": &metadata_queue.album,
|
||||
"album_artist": &metadata_queue.album_artist,
|
||||
"artist": &metadata_queue.artist,
|
||||
"disc": metadata_queue.disc,
|
||||
"disc_count": metadata_queue.disc_count,
|
||||
"duration": metadata_queue.duration,
|
||||
"genre": &metadata_queue.genre,
|
||||
"title": &metadata_queue.title,
|
||||
"track": metadata_queue.track,
|
||||
"track_count": metadata_queue.track_count,
|
||||
"date": metadata_queue.year.to_string(),
|
||||
"audio_type": &song_type,
|
||||
"user_id": &user_id,
|
||||
"song_queue_id": &metadata_queue.song_queue_id,
|
||||
}
|
||||
);
|
||||
|
||||
let client = reqwest::Client::builder().build()?;
|
||||
|
||||
let url = format!("{}/api/v2/song", app.uri);
|
||||
let (key, header) = crate::api::auth_header(app).await;
|
||||
|
||||
let request = client.post(url).json(&payload).header(key, header);
|
||||
request.send().await
|
||||
}
|
||||
|
||||
pub mod response {
|
||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
||||
pub struct Response {
|
||||
pub message: String,
|
||||
pub data: Vec<icarus_models::song::Song>,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub mod create_coverart {
|
||||
|
||||
pub async fn create(
|
||||
app: &crate::config::App,
|
||||
song_id: &uuid::Uuid,
|
||||
coverart_queue_id: &uuid::Uuid,
|
||||
) -> Result<reqwest::Response, reqwest::Error> {
|
||||
let client = reqwest::Client::builder().build()?;
|
||||
let url = format!("{}/api/v2/coverart", app.uri);
|
||||
let payload = get_payload(song_id, coverart_queue_id);
|
||||
let (key, header) = crate::api::auth_header(app).await;
|
||||
let request = client.post(url).json(&payload).header(key, header);
|
||||
|
||||
request.send().await
|
||||
}
|
||||
|
||||
fn get_payload(song_id: &uuid::Uuid, coverart_queue_id: &uuid::Uuid) -> serde_json::Value {
|
||||
serde_json::json!({
|
||||
"song_id": &song_id,
|
||||
"coverart_queue_id": &coverart_queue_id,
|
||||
})
|
||||
}
|
||||
|
||||
pub mod response {
|
||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
||||
pub struct Response {
|
||||
pub message: String,
|
||||
pub data: Vec<icarus_models::coverart::CoverArt>,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub mod wipe_data {
|
||||
pub mod song_queue {
|
||||
pub async fn wipe_data(
|
||||
app: &crate::config::App,
|
||||
song_queue_id: &uuid::Uuid,
|
||||
) -> Result<reqwest::Response, reqwest::Error> {
|
||||
let client = reqwest::Client::builder().build()?;
|
||||
let url = format!("{}/api/v2/song/queue/data/wipe", app.uri);
|
||||
let payload = serde_json::json!({
|
||||
"song_queue_id": song_queue_id
|
||||
});
|
||||
let (key, header) = crate::api::auth_header(app).await;
|
||||
let request = client.patch(url).json(&payload).header(key, header);
|
||||
|
||||
request.send().await
|
||||
}
|
||||
|
||||
pub mod response {
|
||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
||||
pub struct Response {
|
||||
pub message: String,
|
||||
pub data: Vec<uuid::Uuid>,
|
||||
}
|
||||
}
|
||||
}
|
||||
pub mod coverart_queue {
|
||||
pub async fn wipe_data(
|
||||
app: &crate::config::App,
|
||||
coverart_queue_id: &uuid::Uuid,
|
||||
) -> Result<reqwest::Response, reqwest::Error> {
|
||||
let client = reqwest::Client::builder().build()?;
|
||||
let url = format!("{}/api/v2/coverart/queue/data/wipe", app.uri);
|
||||
let payload = serde_json::json!({
|
||||
"coverart_queue_id": coverart_queue_id
|
||||
});
|
||||
let (key, header) = crate::api::auth_header(app).await;
|
||||
let request = client.patch(url).json(&payload).header(key, header);
|
||||
|
||||
request.send().await
|
||||
}
|
||||
|
||||
pub mod response {
|
||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
||||
pub struct Response {
|
||||
pub message: String,
|
||||
pub data: Vec<uuid::Uuid>,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub mod cleanup {
|
||||
pub fn clean_song_queue(song_queue_path: &String) -> Result<(), std::io::Error> {
|
||||
let file_path = std::path::Path::new(song_queue_path);
|
||||
if file_path.exists() {
|
||||
match std::fs::remove_file(file_path) {
|
||||
Ok(_) => {
|
||||
if check_file_existence(song_queue_path) {
|
||||
Err(std::io::Error::other(String::from(
|
||||
"SongQueue file exists after a deletion",
|
||||
)))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
}
|
||||
} else {
|
||||
Err(std::io::Error::other(String::from(
|
||||
"SongQueue file path does not exists",
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clean_coverart_queue(coverart_queue_path: &String) -> Result<(), std::io::Error> {
|
||||
let coverart_file_path = std::path::Path::new(coverart_queue_path);
|
||||
if coverart_file_path.exists() {
|
||||
match std::fs::remove_file(coverart_file_path) {
|
||||
Ok(_) => {
|
||||
if !check_file_existence(coverart_queue_path) {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(std::io::Error::other(String::from(
|
||||
"CoverArt file stil exists",
|
||||
)))
|
||||
}
|
||||
}
|
||||
Err(err) => Err(std::io::Error::other(err.to_string())),
|
||||
}
|
||||
} else {
|
||||
Err(std::io::Error::other(String::from(
|
||||
"CoverArt file does not exists",
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
fn check_file_existence(file_path: &String) -> bool {
|
||||
let path = std::path::Path::new(file_path);
|
||||
path.exists()
|
||||
}
|
||||
}
|
36
src/update_queued_song.rs
Normal file
36
src/update_queued_song.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
pub async fn update_queued_song(
|
||||
app: &crate::config::App,
|
||||
song_path: &String,
|
||||
song_queue_id: &uuid::Uuid,
|
||||
) -> Result<reqwest::Response, reqwest::Error> {
|
||||
let client = reqwest::Client::builder().build()?;
|
||||
|
||||
println!("Song path: {song_path:?}");
|
||||
|
||||
// TODO: Make the filename random
|
||||
let form = reqwest::multipart::Form::new().part(
|
||||
"file",
|
||||
reqwest::multipart::Part::bytes(std::fs::read(song_path).unwrap())
|
||||
.file_name("track01.flac"),
|
||||
);
|
||||
|
||||
let url = format!("{}/api/v2/song/queue/{song_queue_id}", app.uri);
|
||||
println!("Url: {url:?}");
|
||||
|
||||
let (key, header) = crate::api::auth_header(app).await;
|
||||
let request = client.patch(url).multipart(form).header(key, header);
|
||||
|
||||
let response = request.send().await?;
|
||||
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
pub mod response {
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct Response {
|
||||
pub message: String,
|
||||
pub data: Vec<uuid::Uuid>,
|
||||
}
|
||||
}
|
6
src/util.rs
Normal file
6
src/util.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
pub fn path_buf_to_string(path: &std::path::Path) -> String {
|
||||
match path.to_str() {
|
||||
Some(val) => String::from(val),
|
||||
None => String::new(),
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user