coven_database/
file_ref.rs1use crate::sqlite::StoreSql;
6use crate::{
7 file_location::{FileLocation, Place},
8 file_table::Record,
9 DbError,
10};
11use coven_crypto::ContentHash;
12use coven_foundation::id_source::{DeviceId, FileId};
13use coven_merge::Audience;
14
15#[path = "local_file.rs"]
16mod local_file;
17pub(crate) use local_file::{device_only_source, open_copy, open_source, queued_source};
18pub use local_file::{LocalFileError, LocalFileStream, Located};
19
20#[derive(Clone, Debug, PartialEq)]
22pub struct FileRef {
23 id: FileId,
24 audience: Audience,
25 namespace: String,
26 size: u64,
27 place: Stored,
28}
29
30#[derive(Clone, Debug, PartialEq)]
31enum Stored {
32 Cloud { location: FileLocation, hash: ContentHash },
33 DeviceOnly(DeviceId),
34}
35
36#[derive(Clone, Copy, Debug, PartialEq)]
38pub enum FilePlace<'a> {
39 Cloud {
41 location: &'a FileLocation,
43 uploader: DeviceId,
45 content_hash: &'a ContentHash,
47 },
48 DeviceOnly {
51 device: DeviceId,
53 },
54}
55
56impl FileRef {
57 #[cfg(test)]
60 pub(crate) fn planted_size(mut self, size: u64) -> Self {
61 self.size = size;
62 self
63 }
64 pub fn id(&self) -> FileId {
66 self.id
67 }
68 pub fn size(&self) -> u64 {
70 self.size
71 }
72 pub fn namespace(&self) -> &str {
75 &self.namespace
76 }
77 pub fn audience(&self) -> Audience {
80 self.audience.clone()
81 }
82 pub fn place(&self) -> FilePlace<'_> {
84 match &self.place {
85 Stored::Cloud { location, hash } => {
86 FilePlace::Cloud { location, uploader: location.uploader(), content_hash: hash }
87 }
88 Stored::DeviceOnly(device) => FilePlace::DeviceOnly { device: *device },
89 }
90 }
91
92 pub(crate) fn of(record: Record) -> Result<Self, DbError> {
93 let place = match (record.place, record.hash) {
94 (Place::Cloud(location), Some(hash)) => Stored::Cloud { location, hash },
95 (Place::DeviceOnly(device), None) => Stored::DeviceOnly(device),
96 _ => return Err(DbError::DamagedDatabase),
97 };
98 Ok(Self {
99 id: record.id,
100 audience: record.audience,
101 namespace: record.namespace,
102 size: record.size,
103 place,
104 })
105 }
106}
107
108pub(crate) fn read(db: &StoreSql, file: FileId) -> Result<FileRef, DbError> {
110 FileRef::of(Record::read(db, file)?.ok_or(DbError::FileAbsent { file })?)
111}
112
113#[cfg(test)]
114#[path = "file_ref_tests.rs"]
115mod tests;