"Fossies" - the Fresh Open Source Software Archive

Member "selenium-selenium-4.8.1/rust/src/downloads.rs" (17 Feb 2023, 2615 Bytes) of package /linux/www/selenium-selenium-4.8.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Rust source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "downloads.rs": 4.7.0_vs_4.8.0.

    1 // Licensed to the Software Freedom Conservancy (SFC) under one
    2 // or more contributor license agreements.  See the NOTICE file
    3 // distributed with this work for additional information
    4 // regarding copyright ownership.  The SFC licenses this file
    5 // to you under the Apache License, Version 2.0 (the
    6 // "License"); you may not use this file except in compliance
    7 // with the License.  You may obtain a copy of the License at
    8 //
    9 //   http://www.apache.org/licenses/LICENSE-2.0
   10 //
   11 // Unless required by applicable law or agreed to in writing,
   12 // software distributed under the License is distributed on an
   13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   14 // KIND, either express or implied.  See the License for the
   15 // specific language governing permissions and limitations
   16 // under the License.
   17 
   18 use reqwest::Client;
   19 use std::error::Error;
   20 use std::fs::File;
   21 use std::io::copy;
   22 use std::io::Cursor;
   23 
   24 use tempfile::{Builder, TempDir};
   25 
   26 use crate::files::parse_version;
   27 use crate::Logger;
   28 
   29 #[tokio::main]
   30 pub async fn download_driver_to_tmp_folder(
   31     http_client: &Client,
   32     url: String,
   33     log: &Logger,
   34 ) -> Result<(TempDir, String), Box<dyn Error>> {
   35     let tmp_dir = Builder::new().prefix("selenium-manager").tempdir()?;
   36     log.trace(format!(
   37         "Downloading {} to temporal folder {:?}",
   38         url,
   39         tmp_dir.path()
   40     ));
   41 
   42     let response = http_client.get(url).send().await?;
   43     let target_path;
   44     let mut tmp_file = {
   45         let target_name = response
   46             .url()
   47             .path_segments()
   48             .and_then(|segments| segments.last())
   49             .and_then(|name| if name.is_empty() { None } else { Some(name) })
   50             .unwrap_or("tmp.bin");
   51 
   52         log.trace(format!("File to be downloaded: {}", target_name));
   53         let target_name = tmp_dir.path().join(target_name);
   54         target_path = String::from(target_name.to_str().unwrap());
   55 
   56         log.trace(format!(
   57             "Temporal folder for driver package: {}",
   58             target_path
   59         ));
   60         File::create(target_name)?
   61     };
   62     let mut content = Cursor::new(response.bytes().await?);
   63     copy(&mut content, &mut tmp_file)?;
   64 
   65     Ok((tmp_dir, target_path))
   66 }
   67 
   68 #[tokio::main]
   69 pub async fn read_content_from_link(
   70     http_client: &Client,
   71     url: String,
   72 ) -> Result<String, Box<dyn Error>> {
   73     parse_version(http_client.get(url).send().await?.text().await?)
   74 }
   75 
   76 #[tokio::main]
   77 pub async fn read_redirect_from_link(
   78     http_client: &Client,
   79     url: String,
   80 ) -> Result<String, Box<dyn Error>> {
   81     parse_version(http_client.get(&url).send().await?.url().path().to_string())
   82 }