Files
no-copy/src/main/java/ru/soune/nocopy/entity/file/FileEntity.java
T

173 lines
4.6 KiB
Java
Raw Normal View History

2025-12-17 22:37:46 +07:00
package ru.soune.nocopy.entity.file;
2025-12-17 01:19:48 +07:00
import com.fasterxml.jackson.annotation.JsonIgnore;
2025-12-17 01:19:48 +07:00
import jakarta.persistence.*;
import lombok.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
2026-05-04 16:59:30 +07:00
import ru.soune.nocopy.entity.file.permission.PermissionType;
2026-04-13 17:16:32 +07:00
import ru.soune.nocopy.entity.monitoring.FileMonitoringEntity;
2025-12-17 01:19:48 +07:00
import java.time.LocalDateTime;
2026-05-04 16:59:30 +07:00
import java.util.HashMap;
2026-04-13 17:16:32 +07:00
import java.util.List;
2026-05-04 16:59:30 +07:00
import java.util.Map;
2026-02-24 18:35:35 +07:00
import java.util.concurrent.atomic.AtomicInteger;
2025-12-17 01:19:48 +07:00
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "file_entities")
@EntityListeners(AuditingEntityListener.class)
public class FileEntity {
2026-02-24 18:35:35 +07:00
private static AtomicInteger supportIdCounter = new AtomicInteger(0);
2025-12-17 01:19:48 +07:00
@Id
@GeneratedValue(strategy = GenerationType.UUID)
private String id;
2026-01-20 21:55:42 +07:00
@Column(name = "support_id", unique = true, updatable = false)
private Integer supportId;
2025-12-17 01:19:48 +07:00
@Column(name = "user_id", nullable = false)
private Long userId;
@Column(name = "original_file_name", nullable = false)
private String originalFileName;
@Column(name = "stored_file_name", nullable = false)
private String storedFileName;
@Column(name = "file_path", nullable = false, unique = true)
private String filePath;
@Column(name = "file_size", nullable = false)
private Long fileSize;
@Column(name = "mime_type")
private String mimeType;
@Column(name = "file_extension")
private String fileExtension;
@Column(name = "checksum")
private String checksum;
@Column(name = "upload_session_id")
private String uploadSessionId;
@Enumerated(EnumType.STRING)
@Column(name = "status")
private FileStatus status;
@CreatedDate
@Column(name = "created_at", updatable = false)
private LocalDateTime createdAt;
@LastModifiedDate
@Column(name = "updated_at")
private LocalDateTime updatedAt;
2026-01-08 03:22:32 +07:00
@Column(name = "protected_file_path")
private String protectedFilePath;
@Column(name = "protection_status")
@Enumerated(EnumType.STRING)
private ProtectionStatus protectionStatus;
@Column(name = "protected_at")
private LocalDateTime protectedAt;
@Column(name = "signature")
@JsonIgnore
private String signature;
2026-02-26 00:27:31 +07:00
@Column(name = "thumbnail_path")
private String thumbnailPath;
@Column(name = "medium_path")
private String mediumPath;
2026-03-31 13:06:27 +07:00
@Column
private String newExtension;
2026-05-04 16:59:30 +07:00
@ElementCollection
@CollectionTable(name = "file_permissions",
joinColumns = @JoinColumn(name = "file_id"))
@Column(name = "allowed")
@MapKeyEnumerated(EnumType.STRING)
@MapKeyColumn(name = "permission_type")
private Map<PermissionType, Boolean> permissions = new HashMap<>();
@OneToOne(mappedBy = "file", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonIgnore
private ImageHashEntity imageHash;
2025-12-17 01:19:48 +07:00
@PrePersist
public void prePersist() {
2026-02-24 18:35:35 +07:00
if (this.supportId == null) {
this.supportId = supportIdCounter.incrementAndGet();
}
2025-12-17 01:19:48 +07:00
if (this.status == null) {
2026-03-27 19:08:15 +07:00
this.status = FileStatus.MODERATION;
2025-12-17 01:19:48 +07:00
}
2026-01-08 03:22:32 +07:00
if (this.protectionStatus == null) {
this.protectionStatus = ProtectionStatus.NOT_PROTECTED;
}
2025-12-17 01:19:48 +07:00
if (this.createdAt == null) {
this.createdAt = LocalDateTime.now();
}
2026-05-04 16:59:30 +07:00
if (this.permissions == null) {
this.permissions = new HashMap<>();
this.permissions.put(PermissionType.DOWNLOAD, true);
// this.permissions.put(PermissionType.VIEW, true);
// this.permissions.put(PermissionType.EDIT, true);
}
2025-12-17 01:19:48 +07:00
this.updatedAt = LocalDateTime.now();
}
@PreUpdate
public void preUpdate() {
this.updatedAt = LocalDateTime.now();
}
2026-02-24 18:35:35 +07:00
public static void initializeCounter(int maxId) {
supportIdCounter = new AtomicInteger(maxId);
}
2026-05-04 16:59:30 +07:00
@Transient
public boolean hasPermission(PermissionType type) {
return permissions.getOrDefault(type, false);
}
@Transient
public void setPermission(PermissionType type, boolean allowed) {
permissions.put(type, allowed);
}
@Transient
public boolean canDownload() {
return hasPermission(PermissionType.DOWNLOAD);
}
// @Transient
// public boolean canView() {
// return hasPermission(PermissionType.VIEW);
// }
//
// @Transient
// public boolean canEdit() {
// return hasPermission(PermissionType.EDIT);
// }
2025-12-17 01:19:48 +07:00
}