correction + regexp
This commit is contained in:
@ -23,6 +23,12 @@ impl Database {
|
||||
|
||||
let conn = Connection::open(base_path.join("clipboard.db"))?;
|
||||
|
||||
conn.execute_batch(
|
||||
"PRAGMA journal_mode=WAL;
|
||||
PRAGMA synchronous=NORMAL;
|
||||
PRAGMA foreign_keys=ON;",
|
||||
)?;
|
||||
|
||||
conn.execute_batch(
|
||||
"CREATE TABLE IF NOT EXISTS schema_version (version INTEGER NOT NULL);
|
||||
INSERT OR IGNORE INTO schema_version (version)
|
||||
@ -72,22 +78,32 @@ impl Database {
|
||||
("text", t.clone())
|
||||
}
|
||||
ClipboardData::Image(img) => {
|
||||
if let Some(px) = &img.raw_pixels {
|
||||
if px.len() > self.max_entry_size_bytes * 4 {
|
||||
match &img.raw_pixels {
|
||||
Some(px) => {
|
||||
if px.len() > self.max_entry_size_bytes * 4 {
|
||||
eprintln!(
|
||||
"Image rejetée dans DB : {} Mo > limite {} Mo",
|
||||
px.len() / 1_048_576,
|
||||
(self.max_entry_size_bytes * 4) / 1_048_576
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
let path = img.file_path(&self.dir_path);
|
||||
let file = fs::File::create(&path)?;
|
||||
let rgb: Vec<u8> = px
|
||||
.chunks_exact(4)
|
||||
.flat_map(|rgba| [rgba[0], rgba[1], rgba[2]])
|
||||
.collect();
|
||||
JpegEncoder::new_with_quality(file, 70).write_image(
|
||||
&rgb,
|
||||
img.width,
|
||||
img.height,
|
||||
ExtendedColorType::Rgb8,
|
||||
)?;
|
||||
}
|
||||
None => {
|
||||
return Ok(());
|
||||
}
|
||||
let path = img.file_path(&self.dir_path);
|
||||
let file = fs::File::create(&path)?;
|
||||
let rgb: Vec<u8> = px
|
||||
.chunks_exact(4)
|
||||
.flat_map(|rgba| [rgba[0], rgba[1], rgba[2]])
|
||||
.collect();
|
||||
JpegEncoder::new_with_quality(file, 70).write_image(
|
||||
&rgb,
|
||||
img.width,
|
||||
img.height,
|
||||
ExtendedColorType::Rgb8,
|
||||
)?;
|
||||
}
|
||||
("image", format!("{}.jpg", img.id))
|
||||
}
|
||||
@ -108,17 +124,18 @@ impl Database {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut stmt = self.conn.prepare(
|
||||
"SELECT content FROM history
|
||||
WHERE type = 'image'
|
||||
AND id NOT IN (
|
||||
SELECT id FROM history ORDER BY timestamp DESC LIMIT ?1
|
||||
)",
|
||||
)?;
|
||||
let to_delete: Vec<String> = stmt
|
||||
.query_map([self.max_entries as i64], |row| row.get(0))?
|
||||
.filter_map(|r| r.ok())
|
||||
.collect();
|
||||
let image_files: Vec<String> = {
|
||||
let mut stmt = self.conn.prepare(
|
||||
"SELECT content FROM history
|
||||
WHERE type = 'image'
|
||||
AND id NOT IN (
|
||||
SELECT id FROM history ORDER BY timestamp DESC LIMIT ?1
|
||||
)",
|
||||
)?;
|
||||
stmt.query_map([self.max_entries as i64], |row| row.get(0))?
|
||||
.filter_map(|r| r.ok())
|
||||
.collect()
|
||||
};
|
||||
|
||||
self.conn.execute(
|
||||
"DELETE FROM history WHERE id NOT IN (
|
||||
@ -127,7 +144,7 @@ impl Database {
|
||||
[self.max_entries as i64],
|
||||
)?;
|
||||
|
||||
for filename in to_delete {
|
||||
for filename in image_files {
|
||||
let path = Path::new(&self.dir_path).join("images").join(&filename);
|
||||
if path.exists() {
|
||||
if let Err(e) = fs::remove_file(&path) {
|
||||
@ -196,13 +213,14 @@ impl Database {
|
||||
let cutoff_ms = SystemTime::now().duration_since(UNIX_EPOCH)?.as_millis() as i64
|
||||
- (days as i64 * 86_400_000);
|
||||
|
||||
let mut stmt = self
|
||||
.conn
|
||||
.prepare("SELECT content FROM history WHERE type = 'image' AND timestamp < ?1")?;
|
||||
let image_files: Vec<String> = stmt
|
||||
.query_map([cutoff_ms], |row| row.get(0))?
|
||||
.filter_map(|r| r.ok())
|
||||
.collect();
|
||||
let image_files: Vec<String> = {
|
||||
let mut stmt = self
|
||||
.conn
|
||||
.prepare("SELECT content FROM history WHERE type = 'image' AND timestamp < ?1")?;
|
||||
stmt.query_map([cutoff_ms], |row| row.get(0))?
|
||||
.filter_map(|r| r.ok())
|
||||
.collect()
|
||||
};
|
||||
|
||||
let count = self
|
||||
.conn
|
||||
|
||||
Reference in New Issue
Block a user