mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-11-26 23:04:26 +01:00
add indx uid format guard on create ops
This commit is contained in:
parent
402203aa2a
commit
3987d17e40
@ -8,6 +8,7 @@ use std::ops::Deref;
|
||||
use std::sync::Arc;
|
||||
|
||||
use sha2::Digest;
|
||||
use anyhow::bail;
|
||||
|
||||
use crate::index_controller::{IndexController, LocalIndexController, IndexMetadata, Settings, IndexSettings};
|
||||
use crate::option::Opt;
|
||||
@ -126,6 +127,9 @@ impl Data {
|
||||
}
|
||||
|
||||
pub fn create_index(&self, name: impl AsRef<str>, primary_key: Option<impl AsRef<str>>) -> anyhow::Result<IndexMetadata> {
|
||||
if !is_index_uid_valid(name.as_ref()) {
|
||||
bail!("invalid index uid: {:?}", name.as_ref())
|
||||
}
|
||||
let settings = IndexSettings {
|
||||
name: Some(name.as_ref().to_string()),
|
||||
primary_key: primary_key.map(|s| s.as_ref().to_string()),
|
||||
@ -145,3 +149,8 @@ impl Data {
|
||||
&self.api_keys
|
||||
}
|
||||
}
|
||||
|
||||
fn is_index_uid_valid(uid: &str) -> bool {
|
||||
uid.chars().all(|x| x.is_ascii_alphanumeric() || x == '-' || x == '_')
|
||||
}
|
||||
|
||||
|
@ -1,13 +1,13 @@
|
||||
use std::ops::Deref;
|
||||
|
||||
use anyhow::bail;
|
||||
use async_compression::tokio_02::write::GzipEncoder;
|
||||
use futures_util::stream::StreamExt;
|
||||
use milli::update::{IndexDocumentsMethod, UpdateFormat};
|
||||
use tokio::io::AsyncWriteExt;
|
||||
|
||||
use crate::index_controller::UpdateStatus;
|
||||
use crate::index_controller::{IndexController, Settings, IndexSettings, IndexMetadata};
|
||||
use super::Data;
|
||||
use super::{Data, is_index_uid_valid};
|
||||
use crate::index_controller::{UpdateStatus, IndexController, Settings, IndexSettings, IndexMetadata};
|
||||
|
||||
impl Data {
|
||||
pub async fn add_documents<B, E>(
|
||||
@ -22,6 +22,10 @@ impl Data {
|
||||
B: Deref<Target = [u8]>,
|
||||
E: std::error::Error + Send + Sync + 'static,
|
||||
{
|
||||
if !is_index_uid_valid(index.as_ref()) {
|
||||
bail!("invalid index uid: {:?}", index.as_ref())
|
||||
}
|
||||
|
||||
let file = tokio::task::spawn_blocking(tempfile::tempfile).await?;
|
||||
let file = tokio::fs::File::from_std(file?);
|
||||
let mut encoder = GzipEncoder::new(file);
|
||||
@ -57,6 +61,9 @@ impl Data {
|
||||
index: impl AsRef<str> + Send + Sync + 'static,
|
||||
settings: Settings
|
||||
) -> anyhow::Result<UpdateStatus> {
|
||||
if !is_index_uid_valid(index.as_ref()) {
|
||||
bail!("invalid index uid: {:?}", index.as_ref())
|
||||
}
|
||||
let index_controller = self.index_controller.clone();
|
||||
let update = tokio::task::spawn_blocking(move || index_controller.update_settings(index, settings)).await??;
|
||||
Ok(update.into())
|
||||
|
Loading…
Reference in New Issue
Block a user