|
| 1 | +use crate::model::AvailableExtension; |
| 2 | +use crate::repository::portal_corp::URL; |
| 3 | +use crate::repository::{portal_corp, Repository}; |
| 4 | +use crate::Result; |
| 5 | +use async_trait::async_trait; |
| 6 | +use postgresql_archive::repository::github::repository::GitHub; |
| 7 | +use postgresql_archive::{get_archive, matcher}; |
| 8 | +use semver::{Version, VersionReq}; |
| 9 | +use std::fmt::Debug; |
| 10 | +use std::io::Cursor; |
| 11 | +use std::path::PathBuf; |
| 12 | +use std::{fs, io}; |
| 13 | +use zip::ZipArchive; |
| 14 | + |
| 15 | +/// PortalCorp repository. |
| 16 | +#[derive(Debug)] |
| 17 | +pub struct PortalCorp; |
| 18 | + |
| 19 | +impl PortalCorp { |
| 20 | + /// Creates a new PortalCorp repository. |
| 21 | + /// |
| 22 | + /// # Errors |
| 23 | + /// * If the repository cannot be created |
| 24 | + #[allow(clippy::new_ret_no_self)] |
| 25 | + pub fn new() -> Result<Box<dyn Repository>> { |
| 26 | + Ok(Box::new(Self)) |
| 27 | + } |
| 28 | + |
| 29 | + /// Initializes the repository. |
| 30 | + /// |
| 31 | + /// # Errors |
| 32 | + /// * If the repository cannot be initialized. |
| 33 | + pub fn initialize() -> Result<()> { |
| 34 | + matcher::registry::register(|url| Ok(url.starts_with(URL)), portal_corp::matcher)?; |
| 35 | + postgresql_archive::repository::registry::register( |
| 36 | + |url| Ok(url.starts_with(URL)), |
| 37 | + Box::new(GitHub::new), |
| 38 | + )?; |
| 39 | + Ok(()) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +#[async_trait] |
| 44 | +impl Repository for PortalCorp { |
| 45 | + fn name(&self) -> &str { |
| 46 | + "portal-corp" |
| 47 | + } |
| 48 | + |
| 49 | + async fn get_available_extensions(&self) -> Result<Vec<AvailableExtension>> { |
| 50 | + let extensions = vec![AvailableExtension::new( |
| 51 | + self.name(), |
| 52 | + "pgvector_compiled", |
| 53 | + "Precompiled OS packages for pgvector", |
| 54 | + )]; |
| 55 | + Ok(extensions) |
| 56 | + } |
| 57 | + |
| 58 | + async fn get_archive( |
| 59 | + &self, |
| 60 | + postgresql_version: &str, |
| 61 | + name: &str, |
| 62 | + version: &VersionReq, |
| 63 | + ) -> Result<(Version, Vec<u8>)> { |
| 64 | + let url = format!("{URL}/{name}?postgresql_version={postgresql_version}"); |
| 65 | + let archive = get_archive(url.as_str(), version).await?; |
| 66 | + Ok(archive) |
| 67 | + } |
| 68 | + |
| 69 | + #[allow(clippy::case_sensitive_file_extension_comparisons)] |
| 70 | + async fn install( |
| 71 | + &self, |
| 72 | + _name: &str, |
| 73 | + library_dir: PathBuf, |
| 74 | + extension_dir: PathBuf, |
| 75 | + archive: &[u8], |
| 76 | + ) -> Result<Vec<PathBuf>> { |
| 77 | + let reader = Cursor::new(archive); |
| 78 | + let mut archive = ZipArchive::new(reader) |
| 79 | + .map_err(|_| io::Error::new(io::ErrorKind::Other, "Zip error"))?; |
| 80 | + let mut files = Vec::new(); |
| 81 | + |
| 82 | + for i in 0..archive.len() { |
| 83 | + let mut file = archive |
| 84 | + .by_index(i) |
| 85 | + .map_err(|_| io::Error::new(io::ErrorKind::Other, "Zip error"))?; |
| 86 | + let file_path = PathBuf::from(file.name()); |
| 87 | + let file_path = PathBuf::from(file_path.file_name().unwrap_or_default()); |
| 88 | + let file_name = file_path.to_string_lossy(); |
| 89 | + |
| 90 | + if file_name.ends_with(".dll") |
| 91 | + || file_name.ends_with(".dylib") |
| 92 | + || file_name.ends_with(".so") |
| 93 | + { |
| 94 | + let mut out = Vec::new(); |
| 95 | + io::copy(&mut file, &mut out)?; |
| 96 | + let path = PathBuf::from(&library_dir).join(file_path); |
| 97 | + fs::write(&path, out)?; |
| 98 | + files.push(path); |
| 99 | + } else if file_name.ends_with(".control") || file_name.ends_with(".sql") { |
| 100 | + let mut out = Vec::new(); |
| 101 | + io::copy(&mut file, &mut out)?; |
| 102 | + let path = PathBuf::from(&extension_dir).join(file_path); |
| 103 | + fs::write(&path, out)?; |
| 104 | + files.push(path); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + Ok(files) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +#[cfg(test)] |
| 113 | +mod tests { |
| 114 | + use super::*; |
| 115 | + use crate::repository::Repository; |
| 116 | + |
| 117 | + #[test] |
| 118 | + fn test_name() { |
| 119 | + let repository = PortalCorp; |
| 120 | + assert_eq!("portal-corp", repository.name()); |
| 121 | + } |
| 122 | + |
| 123 | + #[tokio::test] |
| 124 | + async fn test_get_available_extensions() -> Result<()> { |
| 125 | + let repository = PortalCorp; |
| 126 | + let extensions = repository.get_available_extensions().await?; |
| 127 | + let extension = &extensions[0]; |
| 128 | + |
| 129 | + assert_eq!("pgvector_compiled", extension.name()); |
| 130 | + assert_eq!( |
| 131 | + "Precompiled OS packages for pgvector", |
| 132 | + extension.description() |
| 133 | + ); |
| 134 | + Ok(()) |
| 135 | + } |
| 136 | +} |
0 commit comments