torrust_index/config/v2/
image_cache.rs

1use serde::{Deserialize, Serialize};
2
3/// Configuration for the image proxy cache.
4///
5/// Users have a cache quota per period. For example: 100MB per day.
6/// When users are navigating the site, they will be downloading images that are
7/// embedded in the torrent description. These images will be cached in the
8/// proxy. The proxy will not download new images if the user has reached the
9/// quota.
10#[allow(clippy::module_name_repetitions)]
11#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
12pub struct ImageCache {
13    /// Cache size in bytes.
14    #[serde(default = "ImageCache::default_capacity")]
15    pub capacity: usize,
16
17    /// Maximum size in bytes for a single image.
18    #[serde(default = "ImageCache::default_entry_size_limit")]
19    pub entry_size_limit: usize,
20
21    /// Maximum time in seconds to wait for downloading the image form the original source.
22    #[serde(default = "ImageCache::default_max_request_timeout_ms")]
23    pub max_request_timeout_ms: u64,
24
25    /// Users have a cache quota per period. For example: 100MB per day.
26    /// This is the maximum size in bytes (100MB in bytes).    
27    #[serde(default = "ImageCache::default_user_quota_bytes")]
28    pub user_quota_bytes: usize,
29
30    /// Users have a cache quota per period. For example: 100MB per day.
31    /// This is the period in seconds (1 day in seconds).
32    #[serde(default = "ImageCache::default_user_quota_period_seconds")]
33    pub user_quota_period_seconds: u64,
34}
35
36impl Default for ImageCache {
37    fn default() -> Self {
38        Self {
39            max_request_timeout_ms: Self::default_max_request_timeout_ms(),
40            capacity: Self::default_capacity(),
41            entry_size_limit: Self::default_entry_size_limit(),
42            user_quota_period_seconds: Self::default_user_quota_period_seconds(),
43            user_quota_bytes: Self::default_user_quota_bytes(),
44        }
45    }
46}
47
48impl ImageCache {
49    fn default_max_request_timeout_ms() -> u64 {
50        1000
51    }
52
53    fn default_capacity() -> usize {
54        128_000_000
55    }
56
57    fn default_entry_size_limit() -> usize {
58        4_000_000
59    }
60
61    fn default_user_quota_period_seconds() -> u64 {
62        3600
63    }
64
65    fn default_user_quota_bytes() -> usize {
66        64_000_000
67    }
68}