neo3/neo_fs/
object.rs

1// Copyright (c) 2023-2025 R3E Network
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6//     http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14//! # NeoFS Object Operations
15//!
16//! This module provides functionality for managing NeoFS objects.
17//! Objects in NeoFS are the actual data files stored in containers.
18
19use chrono::{DateTime, Utc};
20use serde::{Deserialize, Serialize};
21
22use crate::neo_fs::types::{Attributes, ContainerId, ObjectId, ObjectType, OwnerId};
23
24/// Represents a storage object in NeoFS
25#[derive(Debug, Clone, Serialize, Deserialize)]
26pub struct Object {
27	/// Object unique identifier
28	pub id: Option<ObjectId>,
29	/// Container identifier where the object is stored
30	pub container_id: ContainerId,
31	/// Owner's identifier
32	pub owner_id: OwnerId,
33	/// Object type
34	pub object_type: ObjectType,
35	/// Content payload
36	#[serde(skip_serializing, skip_deserializing)]
37	pub payload: Vec<u8>,
38	/// User-defined attributes and system headers
39	pub attributes: Attributes,
40	/// Creation timestamp
41	#[serde(skip_serializing_if = "Option::is_none")]
42	#[serde(with = "chrono::serde::ts_seconds_option")]
43	pub created_at: Option<DateTime<Utc>>,
44}
45
46impl Object {
47	/// Creates a new object in the specified container
48	pub fn new(container_id: ContainerId, owner_id: OwnerId) -> Self {
49		Self {
50			id: None,
51			container_id,
52			owner_id,
53			object_type: ObjectType::Regular,
54			payload: Vec::new(),
55			attributes: Attributes::new(),
56			created_at: None,
57		}
58	}
59
60	/// Sets the object payload from a byte array
61	pub fn with_payload(mut self, payload: Vec<u8>) -> Self {
62		self.payload = payload;
63		self
64	}
65
66	/// Sets the object type
67	pub fn with_type(mut self, object_type: ObjectType) -> Self {
68		self.object_type = object_type;
69		self
70	}
71
72	/// Adds an attribute to the object
73	pub fn with_attribute(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
74		self.attributes.add(key, value);
75		self
76	}
77
78	/// Sets the object's file name
79	pub fn with_filename(self, filename: impl Into<String>) -> Self {
80		self.with_attribute("FileName", filename)
81	}
82
83	/// Sets the object's content type
84	pub fn with_content_type(self, content_type: impl Into<String>) -> Self {
85		self.with_attribute("Content-Type", content_type)
86	}
87
88	/// Returns the size of the object's payload in bytes
89	pub fn size(&self) -> usize {
90		self.payload.len()
91	}
92}
93
94/// Helper structs for multipart upload operations
95
96/// Represents an initialized multipart upload
97#[derive(Debug, Clone, Serialize, Deserialize)]
98pub struct MultipartUpload {
99	/// Object unique identifier (assigned after completion)
100	pub id: Option<ObjectId>,
101	/// Container identifier where the object is stored
102	pub container_id: ContainerId,
103	/// Owner's identifier
104	pub owner_id: OwnerId,
105	/// Upload identifier
106	pub upload_id: String,
107	/// User-defined attributes
108	pub attributes: Attributes,
109	/// Part size for multipart upload (in bytes)
110	pub part_size: u64,
111	/// Maximum number of parts
112	pub max_parts: u64,
113}
114
115/// Represents a single part in a multipart upload
116#[derive(Debug, Clone, Serialize, Deserialize)]
117pub struct Part {
118	/// Part number (starting from 1)
119	pub part_number: u32,
120	/// Payload data for this part
121	#[serde(skip_serializing, skip_deserializing)]
122	pub payload: Vec<u8>,
123}
124
125impl Part {
126	/// Creates a new upload part
127	pub fn new(part_number: u32, payload: Vec<u8>) -> Self {
128		Self { part_number, payload }
129	}
130}
131
132/// Represents the result of a completed multipart upload
133#[derive(Debug, Clone, Serialize, Deserialize)]
134pub struct MultipartUploadResult {
135	/// Object unique identifier for the uploaded object
136	pub object_id: ObjectId,
137	/// Container identifier
138	pub container_id: ContainerId,
139}