Move FileId out of SpotifyId

This commit is contained in:
Roderick van Domburg 2021-12-08 19:53:45 +01:00
parent f03a7e95c1
commit 9b2ca1442e
No known key found for this signature in database
GPG key ID: A9EF5222A26F0451
12 changed files with 108 additions and 68 deletions

View file

@ -9,13 +9,15 @@ use std::time::{Duration, Instant};
use byteorder::{BigEndian, ByteOrder}; use byteorder::{BigEndian, ByteOrder};
use futures_util::{future, StreamExt, TryFutureExt, TryStreamExt}; use futures_util::{future, StreamExt, TryFutureExt, TryStreamExt};
use librespot_core::channel::{ChannelData, ChannelError, ChannelHeaders};
use librespot_core::session::Session;
use librespot_core::spotify_id::FileId;
use tempfile::NamedTempFile; use tempfile::NamedTempFile;
use tokio::sync::{mpsc, oneshot}; use tokio::sync::{mpsc, oneshot};
use librespot_core::channel::{ChannelData, ChannelError, ChannelHeaders};
use librespot_core::file_id::FileId;
use librespot_core::session::Session;
use self::receive::{audio_file_fetch, request_range}; use self::receive::{audio_file_fetch, request_range};
use crate::range_set::{Range, RangeSet}; use crate::range_set::{Range, RangeSet};
/// The minimum size of a block that is requested from the Spotify servers in one request. /// The minimum size of a block that is requested from the Spotify servers in one request.

View file

@ -7,13 +7,14 @@ use atomic::Ordering;
use byteorder::{BigEndian, WriteBytesExt}; use byteorder::{BigEndian, WriteBytesExt};
use bytes::Bytes; use bytes::Bytes;
use futures_util::StreamExt; use futures_util::StreamExt;
use librespot_core::channel::{Channel, ChannelData};
use librespot_core::packet::PacketType;
use librespot_core::session::Session;
use librespot_core::spotify_id::FileId;
use tempfile::NamedTempFile; use tempfile::NamedTempFile;
use tokio::sync::{mpsc, oneshot}; use tokio::sync::{mpsc, oneshot};
use librespot_core::channel::{Channel, ChannelData};
use librespot_core::file_id::FileId;
use librespot_core::packet::PacketType;
use librespot_core::session::Session;
use crate::range_set::{Range, RangeSet}; use crate::range_set::{Range, RangeSet};
use super::{AudioFileShared, DownloadStrategy, StreamLoaderCommand}; use super::{AudioFileShared, DownloadStrategy, StreamLoaderCommand};

View file

@ -4,8 +4,9 @@ use std::collections::HashMap;
use std::io::Write; use std::io::Write;
use tokio::sync::oneshot; use tokio::sync::oneshot;
use crate::file_id::FileId;
use crate::packet::PacketType; use crate::packet::PacketType;
use crate::spotify_id::{FileId, SpotifyId}; use crate::spotify_id::SpotifyId;
use crate::util::SeqGenerator; use crate::util::SeqGenerator;
#[derive(Debug, Hash, PartialEq, Eq, Copy, Clone)] #[derive(Debug, Hash, PartialEq, Eq, Copy, Clone)]

View file

@ -9,7 +9,7 @@ use std::time::SystemTime;
use priority_queue::PriorityQueue; use priority_queue::PriorityQueue;
use crate::authentication::Credentials; use crate::authentication::Credentials;
use crate::spotify_id::FileId; use crate::file_id::FileId;
/// Some kind of data structure that holds some paths, the size of these files and a timestamp. /// Some kind of data structure that holds some paths, the size of these files and a timestamp.
/// It keeps track of the file sizes and is able to pop the path with the oldest timestamp if /// It keeps track of the file sizes and is able to pop the path with the oldest timestamp if

55
core/src/file_id.rs Normal file
View file

@ -0,0 +1,55 @@
use librespot_protocol as protocol;
use std::fmt;
use crate::spotify_id::to_base16;
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FileId(pub [u8; 20]);
impl FileId {
pub fn from_raw(src: &[u8]) -> FileId {
let mut dst = [0u8; 20];
dst.clone_from_slice(src);
FileId(dst)
}
pub fn to_base16(&self) -> String {
to_base16(&self.0, &mut [0u8; 40])
}
}
impl fmt::Debug for FileId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("FileId").field(&self.to_base16()).finish()
}
}
impl fmt::Display for FileId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&self.to_base16())
}
}
impl From<&[u8]> for FileId {
fn from(src: &[u8]) -> Self {
Self::from_raw(src)
}
}
impl From<&protocol::metadata::Image> for FileId {
fn from(image: &protocol::metadata::Image) -> Self {
Self::from(image.get_file_id())
}
}
impl From<&protocol::metadata::AudioFile> for FileId {
fn from(file: &protocol::metadata::AudioFile) -> Self {
Self::from(file.get_file_id())
}
}
impl From<&protocol::metadata::VideoFile> for FileId {
fn from(video: &protocol::metadata::VideoFile) -> Self {
Self::from(video.get_file_id())
}
}

View file

@ -18,6 +18,7 @@ mod connection;
mod dealer; mod dealer;
#[doc(hidden)] #[doc(hidden)]
pub mod diffie_hellman; pub mod diffie_hellman;
pub mod file_id;
mod http_client; mod http_client;
pub mod mercury; pub mod mercury;
pub mod packet; pub mod packet;

View file

@ -1,10 +1,11 @@
use crate::apresolve::SocketAddress; use crate::apresolve::SocketAddress;
use crate::file_id::FileId;
use crate::http_client::HttpClientError; use crate::http_client::HttpClientError;
use crate::mercury::MercuryError; use crate::mercury::MercuryError;
use crate::protocol::canvaz::EntityCanvazRequest; use crate::protocol::canvaz::EntityCanvazRequest;
use crate::protocol::connect::PutStateRequest; use crate::protocol::connect::PutStateRequest;
use crate::protocol::extended_metadata::BatchedEntityRequest; use crate::protocol::extended_metadata::BatchedEntityRequest;
use crate::spotify_id::{FileId, SpotifyId}; use crate::spotify_id::SpotifyId;
use bytes::Bytes; use bytes::Bytes;
use http::header::HeaderValue; use http::header::HeaderValue;

View file

@ -6,6 +6,9 @@ use std::convert::{TryFrom, TryInto};
use std::fmt; use std::fmt;
use std::ops::Deref; use std::ops::Deref;
// re-export FileId for historic reasons, when it was part of this mod
pub use crate::file_id::FileId;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SpotifyItemType { pub enum SpotifyItemType {
Album, Album,
@ -45,7 +48,7 @@ impl From<SpotifyItemType> for &str {
} }
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct SpotifyId { pub struct SpotifyId {
pub id: u128, pub id: u128,
pub item_type: SpotifyItemType, pub item_type: SpotifyItemType,
@ -258,7 +261,19 @@ impl SpotifyId {
} }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] impl fmt::Debug for SpotifyId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("SpotifyId").field(&self.to_uri()).finish()
}
}
impl fmt::Display for SpotifyId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&self.to_uri())
}
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct NamedSpotifyId { pub struct NamedSpotifyId {
pub inner_id: SpotifyId, pub inner_id: SpotifyId,
pub username: String, pub username: String,
@ -314,6 +329,20 @@ impl Deref for NamedSpotifyId {
} }
} }
impl fmt::Debug for NamedSpotifyId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("NamedSpotifyId")
.field(&self.inner_id.to_uri())
.finish()
}
}
impl fmt::Display for NamedSpotifyId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&self.inner_id.to_uri())
}
}
impl TryFrom<&[u8]> for SpotifyId { impl TryFrom<&[u8]> for SpotifyId {
type Error = SpotifyIdError; type Error = SpotifyIdError;
fn try_from(src: &[u8]) -> Result<Self, Self::Error> { fn try_from(src: &[u8]) -> Result<Self, Self::Error> {
@ -456,58 +485,7 @@ impl TryFrom<&protocol::playlist_annotate3::TranscodedPicture> for SpotifyId {
} }
} }
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub fn to_base16(src: &[u8], buf: &mut [u8]) -> String {
pub struct FileId(pub [u8; 20]);
impl FileId {
pub fn from_raw(src: &[u8]) -> FileId {
let mut dst = [0u8; 20];
dst.clone_from_slice(src);
FileId(dst)
}
pub fn to_base16(&self) -> String {
to_base16(&self.0, &mut [0u8; 40])
}
}
impl fmt::Debug for FileId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("FileId").field(&self.to_base16()).finish()
}
}
impl fmt::Display for FileId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&self.to_base16())
}
}
impl From<&[u8]> for FileId {
fn from(src: &[u8]) -> Self {
Self::from_raw(src)
}
}
impl From<&protocol::metadata::Image> for FileId {
fn from(image: &protocol::metadata::Image) -> Self {
Self::from(image.get_file_id())
}
}
impl From<&protocol::metadata::AudioFile> for FileId {
fn from(file: &protocol::metadata::AudioFile) -> Self {
Self::from(file.get_file_id())
}
}
impl From<&protocol::metadata::VideoFile> for FileId {
fn from(video: &protocol::metadata::VideoFile) -> Self {
Self::from(video.get_file_id())
}
}
#[inline]
fn to_base16(src: &[u8], buf: &mut [u8]) -> String {
let mut i = 0; let mut i = 0;
for v in src { for v in src {
buf[i] = BASE16_DIGITS[(v >> 4) as usize]; buf[i] = BASE16_DIGITS[(v >> 4) as usize];

View file

@ -2,7 +2,7 @@ use std::collections::HashMap;
use std::fmt::Debug; use std::fmt::Debug;
use std::ops::Deref; use std::ops::Deref;
use librespot_core::spotify_id::FileId; use librespot_core::file_id::FileId;
use librespot_protocol as protocol; use librespot_protocol as protocol;
use protocol::metadata::AudioFile as AudioFileMessage; use protocol::metadata::AudioFile as AudioFileMessage;

View file

@ -10,7 +10,7 @@ use protocol::metadata::ExternalId as ExternalIdMessage;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct ExternalId { pub struct ExternalId {
pub external_type: String, pub external_type: String,
pub id: String, pub id: String, // this can be anything from a URL to a ISRC, EAN or UPC
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]

View file

@ -7,7 +7,8 @@ use crate::{
util::{from_repeated_message, try_from_repeated_message}, util::{from_repeated_message, try_from_repeated_message},
}; };
use librespot_core::spotify_id::{FileId, SpotifyId}; use librespot_core::file_id::FileId;
use librespot_core::spotify_id::SpotifyId;
use librespot_protocol as protocol; use librespot_protocol as protocol;
use protocol::metadata::Image as ImageMessage; use protocol::metadata::Image as ImageMessage;

View file

@ -3,7 +3,7 @@ use std::ops::Deref;
use crate::util::from_repeated_message; use crate::util::from_repeated_message;
use librespot_core::spotify_id::FileId; use librespot_core::file_id::FileId;
use librespot_protocol as protocol; use librespot_protocol as protocol;
use protocol::metadata::VideoFile as VideoFileMessage; use protocol::metadata::VideoFile as VideoFileMessage;