Browse Source

disk space wotcher tested successfully

develop
bvn13 3 years ago
parent
commit
1663a7e1e4
  1. 12
      adastor.iml
  2. 246
      adastor.ipr
  3. 8
      pom.xml
  4. 7
      src/main/java/ru/bvn13/adastor/entities/Stortion.java
  5. 2
      src/main/java/ru/bvn13/adastor/entities/dtos/StortionDto.java
  6. 6
      src/main/java/ru/bvn13/adastor/tasks/DiskFreeSpaceChecker.java
  7. 4
      src/main/java/ru/bvn13/adastor/web/repositories/CustomStortionRepository.java
  8. 5
      src/main/java/ru/bvn13/adastor/web/repositories/impl/StortionRepositoryImpl.java
  9. 58
      src/main/java/ru/bvn13/adastor/web/services/StortionService.java

12
adastor.iml

@ -4,15 +4,6 @@ @@ -4,15 +4,6 @@
<facet type="Spring" name="Spring">
<configuration />
</facet>
<facet type="web" name="Web">
<configuration>
<webroots />
<sourceRoots>
<root url="file://$MODULE_DIR$/src/main/java" />
<root url="file://$MODULE_DIR$/src/main/resources" />
</sourceRoots>
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_11">
<output url="file://$MODULE_DIR$/target/classes" />
@ -23,7 +14,7 @@ @@ -23,7 +14,7 @@
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="jdk" jdkName="11" jdkType="JavaSDK" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-cache:2.1.3.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:2.1.3.RELEASE" level="project" />
@ -119,6 +110,7 @@ @@ -119,6 +110,7 @@
<orderEntry type="library" name="Maven: org.eclipse.jetty.websocket:javax-websocket-client-impl:9.4.14.v20181114" level="project" />
<orderEntry type="library" name="Maven: javax.websocket:javax.websocket-api:1.1" level="project" />
<orderEntry type="library" name="Maven: org.mortbay.jasper:apache-el:8.5.35.1" level="project" />
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.12" level="project" />
<orderEntry type="library" name="Maven: org.projectlombok:lombok:1.18.6" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-starter-test:2.1.3.RELEASE" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test:2.1.3.RELEASE" level="project" />

246
adastor.ipr

@ -14,17 +14,15 @@ @@ -14,17 +14,15 @@
</bytecodeTargetLevel>
</component>
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
<data-source source="LOCAL" name="database" uuid="8f9d20f6-5642-4a5c-a61a-7743b445808b">
<data-source source="LOCAL" name="database" uuid="f4f60a8c-2571-491a-a74a-bebd988bc7e5">
<driver-ref>hsqldb.local</driver-ref>
<synchronize>true</synchronize>
<configured-by-url>true</configured-by-url>
<jdbc-driver>org.hsqldb.jdbc.JDBCDriver</jdbc-driver>
<jdbc-url>jdbc:hsqldb:file:$PROJECT_DIR$/database/adastor</jdbc-url>
<jdbc-url>jdbc:hsqldb:file:./database/adastor</jdbc-url>
</data-source>
</component>
<component name="Encoding" addBOMForNewFiles="with NO BOM">
<file url="file://$PROJECT_DIR$" charset="UTF-8" />
<file url="PROJECT" charset="UTF-8" />
</component>
<component name="InspectionProjectProfileManager">
<profile version="1.0">
@ -37,6 +35,89 @@ @@ -37,6 +35,89 @@
<module name="adastor" options="-parameters" />
</option>
</component>
<component name="MarkdownNavigator.ProfileManager" default="" pdf-export="" />
<component name="MarkdownProjectSettings" wasCopied="true">
<PreviewSettings splitEditorLayout="SPLIT" splitEditorPreview="PREVIEW" useGrayscaleRendering="false" zoomFactor="1.0" maxImageWidth="0" showGitHubPageIfSynced="false" allowBrowsingInPreview="false" synchronizePreviewPosition="true" highlightPreviewType="NONE" highlightFadeOut="5" highlightOnTyping="true" synchronizeSourcePosition="true" verticallyAlignSourceAndPreviewSyncPosition="true" showSearchHighlightsInPreview="false" showSelectionInPreview="true" openRemoteLinks="true" replaceUnicodeEmoji="false" lastLayoutSetsDefault="false">
<PanelProvider>
<provider providerId="com.vladsch.idea.multimarkdown.editor.swing.html.panel" providerName="Default - Swing" />
</PanelProvider>
</PreviewSettings>
<ParserSettings gitHubSyntaxChange="false" emojiShortcuts="1" emojiImages="0">
<PegdownExtensions>
<option name="ABBREVIATIONS" value="false" />
<option name="ANCHORLINKS" value="true" />
<option name="ASIDE" value="false" />
<option name="ATXHEADERSPACE" value="true" />
<option name="AUTOLINKS" value="true" />
<option name="DEFINITIONS" value="false" />
<option name="DEFINITION_BREAK_DOUBLE_BLANK_LINE" value="false" />
<option name="FENCED_CODE_BLOCKS" value="true" />
<option name="FOOTNOTES" value="false" />
<option name="HARDWRAPS" value="false" />
<option name="HTML_DEEP_PARSER" value="false" />
<option name="INSERTED" value="false" />
<option name="QUOTES" value="false" />
<option name="RELAXEDHRULES" value="true" />
<option name="SMARTS" value="false" />
<option name="STRIKETHROUGH" value="true" />
<option name="SUBSCRIPT" value="false" />
<option name="SUPERSCRIPT" value="false" />
<option name="SUPPRESS_HTML_BLOCKS" value="false" />
<option name="SUPPRESS_INLINE_HTML" value="false" />
<option name="TABLES" value="true" />
<option name="TASKLISTITEMS" value="true" />
<option name="TOC" value="false" />
<option name="WIKILINKS" value="false" />
</PegdownExtensions>
<ParserOptions>
<option name="ADMONITION_EXT" value="false" />
<option name="ATTRIBUTES_EXT" value="false" />
<option name="COMMONMARK_LISTS" value="true" />
<option name="DUMMY" value="false" />
<option name="EMOJI_SHORTCUTS" value="true" />
<option name="ENUMERATED_REFERENCES_EXT" value="false" />
<option name="FLEXMARK_FRONT_MATTER" value="false" />
<option name="GFM_LOOSE_BLANK_LINE_AFTER_ITEM_PARA" value="false" />
<option name="GFM_TABLE_RENDERING" value="true" />
<option name="GITBOOK_URL_ENCODING" value="false" />
<option name="GITHUB_LISTS" value="false" />
<option name="GITHUB_WIKI_LINKS" value="false" />
<option name="GITLAB_EXT" value="false" />
<option name="GITLAB_MATH_EXT" value="false" />
<option name="GITLAB_MERMAID_EXT" value="false" />
<option name="HEADER_ID_NON_ASCII_TO_LOWERCASE" value="false" />
<option name="HEADER_ID_NO_DUPED_DASHES" value="false" />
<option name="JEKYLL_FRONT_MATTER" value="false" />
<option name="MACROS_EXT" value="false" />
<option name="NO_TEXT_ATTRIBUTES" value="false" />
<option name="PARSE_HTML_ANCHOR_ID" value="false" />
<option name="PLANTUML_FENCED_CODE" value="false" />
<option name="PUML_FENCED_CODE" value="false" />
<option name="SIM_TOC_BLANK_LINE_SPACER" value="true" />
</ParserOptions>
</ParserSettings>
<HtmlSettings headerTopEnabled="false" headerBottomEnabled="false" bodyTopEnabled="false" bodyBottomEnabled="false" embedUrlContent="false" addPageHeader="true" embedImages="false" embedHttpImages="false" imageUriSerials="false" addDocTypeHtml="true" noParaTags="false" plantUmlConversion="0" mathConversion="-1">
<GeneratorProvider>
<provider providerId="com.vladsch.idea.multimarkdown.editor.swing.html.generator" providerName="Default Swing HTML Generator" />
</GeneratorProvider>
<headerTop />
<headerBottom />
<bodyTop />
<bodyBottom />
</HtmlSettings>
<CssSettings previewScheme="UI_SCHEME" cssUri="" isCssUriEnabled="false" isCssUriSerial="false" isCssTextEnabled="false" isDynamicPageWidth="true">
<StylesheetProvider>
<provider providerId="com.vladsch.idea.multimarkdown.editor.swing.html.css" providerName="Default Swing Stylesheet" />
</StylesheetProvider>
<ScriptProviders />
<cssText />
<cssUriHistory />
</CssSettings>
<HtmlExportSettings updateOnSave="false" parentDir="" targetDir="" cssDir="" scriptDir="" plainHtml="false" imageDir="" copyLinkedImages="false" imageUniquifyType="0" targetPathType="2" targetExt="" useTargetExt="false" noCssNoScripts="false" useElementStyleAttribute="false" linkToExportedHtml="true" exportOnSettingsChange="true" regenerateOnProjectOpen="false" linkFormatType="HTTP_ABSOLUTE" />
<LinkMapSettings>
<textMaps />
</LinkMapSettings>
</component>
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
@ -44,126 +125,19 @@ @@ -44,126 +125,19 @@
</list>
</option>
</component>
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
<component name="ProjectInspectionProfilesVisibleTreeState">
<entry key="Project Default">
<profile-state>
<expanded-state>
<State />
</expanded-state>
<selected-state>
<State>
<id>Android</id>
</State>
</selected-state>
</profile-state>
</entry>
</component>
<component name="ProjectModuleManager">
<modules>
@ -171,19 +145,24 @@ @@ -171,19 +145,24 @@
</modules>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" project-jdk-name="11" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
<output url="file://$PROJECT_DIR$/classes" />
</component>
<component name="ScalaSbtSettings">
<option name="customVMEnabled" value="true" />
<option name="customVMPath" value="/usr/java/jdk1.8.0_201-amd64" />
</component>
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="" vcs="Git" />
</component>
<component name="dataSourceStorageLocal">
<data-source name="database" uuid="8f9d20f6-5642-4a5c-a61a-7743b445808b">
<database-info product="HSQL Database Engine" version="2.4.0" jdbc-version="4.2" driver-name="HSQL Database Engine Driver" driver-version="2.4.0" dbms="HSQLDB" exact-version="0">
<data-source name="database" uuid="f4f60a8c-2571-491a-a74a-bebd988bc7e5">
<database-info product="HSQL Database Engine" version="2.4.0" jdbc-version="4.2" driver-name="HSQL Database Engine Driver" driver-version="2.4.0" dbms="HSQLDB" exact-version="2.4.0" exact-driver-version="2.4">
<identifier-quote-string>&quot;</identifier-quote-string>
</database-info>
<case-sensitivity plain-identifiers="upper" quoted-identifiers="exact" />
<secret-storage>master_key</secret-storage>
<user-name>sa</user-name>
<introspection-schemas>@:@</introspection-schemas>
</data-source>
</component>
<component name="libraryTable">
@ -330,6 +309,17 @@ @@ -330,6 +309,17 @@
<root url="jar://$MAVEN_REPOSITORY$/com/zaxxer/HikariCP/3.2.0/HikariCP-3.2.0-sources.jar!/" />
</SOURCES>
</library>
<library name="Maven: commons-codec:commons-codec:1.12">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/commons-codec/commons-codec/1.12/commons-codec-1.12.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/commons-codec/commons-codec/1.12/commons-codec-1.12-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/commons-codec/commons-codec/1.12/commons-codec-1.12-sources.jar!/" />
</SOURCES>
</library>
<library name="Maven: javax.activation:javax.activation-api:1.2.0">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/javax/activation/javax.activation-api/1.2.0/javax.activation-api-1.2.0.jar!/" />

8
pom.xml

@ -66,6 +66,14 @@ @@ -66,6 +66,14 @@
<!--<type>jar</type>-->
<!--</dependency>-->
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.12</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>

7
src/main/java/ru/bvn13/adastor/entities/Stortion.java

@ -5,14 +5,15 @@ import lombok.Getter; @@ -5,14 +5,15 @@ import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.*;
import java.time.LocalDateTime;
/**
* @author boykovn at 11.03.2019
*/
@Table(
indexes = @Index(columnList = "hash")
)
@Entity
@Getter
@Setter

2
src/main/java/ru/bvn13/adastor/entities/dtos/StortionDto.java

@ -28,4 +28,6 @@ public class StortionDto { @@ -28,4 +28,6 @@ public class StortionDto {
@Transient
private long retention;
private String hash;
}

6
src/main/java/ru/bvn13/adastor/tasks/DiskFreeSpaceChecker.java

@ -40,7 +40,7 @@ public class DiskFreeSpaceChecker { @@ -40,7 +40,7 @@ public class DiskFreeSpaceChecker {
private double getSpaceLeft() {
File path = new File(config.getStoragePath());
double space = (double) path.getFreeSpace() / 1024 / 1024;
double space = (double) path.getFreeSpace();// / 1024 / 1024;
return space;
}
@ -51,8 +51,8 @@ public class DiskFreeSpaceChecker { @@ -51,8 +51,8 @@ public class DiskFreeSpaceChecker {
final ExecutorService es = Executors.newFixedThreadPool(10);
stortionService.findAllSortedByRetention().forEach(stortionDto -> {
double space = spaceLeft.accumulateAndGet((double) stortionDto.getSize(), (a, b) -> a + b);
if (space >= mustFreeSpace) {
double virtualSpace = spaceLeft.accumulateAndGet((double) stortionDto.getSize(), (a, b) -> a + b);
if (virtualSpace <= mustFreeSpace) {
es.submit(() -> {
File file = new File(String.format("%s%s", config.getStoragePath(), stortionDto.getPath()));
if (file.exists()) {

4
src/main/java/ru/bvn13/adastor/web/repositories/CustomStortionRepository.java

@ -2,13 +2,13 @@ package ru.bvn13.adastor.web.repositories; @@ -2,13 +2,13 @@ package ru.bvn13.adastor.web.repositories;
import ru.bvn13.adastor.entities.Stortion;
import java.util.stream.Stream;
import java.util.Collection;
/**
* @author boykovn at 12.03.2019
*/
public interface CustomStortionRepository {
Stream<Stortion> findAllSortedByRetention();
Collection<Stortion> findAllSortedByRetention();
}

5
src/main/java/ru/bvn13/adastor/web/repositories/impl/StortionRepositoryImpl.java

@ -9,6 +9,7 @@ import ru.bvn13.adastor.web.repositories.CustomStortionRepository; @@ -9,6 +9,7 @@ import ru.bvn13.adastor.web.repositories.CustomStortionRepository;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Collection;
import java.util.stream.Stream;
/**
@ -39,7 +40,7 @@ public class StortionRepositoryImpl implements CustomStortionRepository { @@ -39,7 +40,7 @@ public class StortionRepositoryImpl implements CustomStortionRepository {
* @return queried collection
*/
@Override
public Stream<Stortion> findAllSortedByRetention() {
public Collection<Stortion> findAllSortedByRetention() {
long min_age = config.getMinDaysStoring();
long max_age = config.getMaxDaysStoring();
@ -51,7 +52,7 @@ public class StortionRepositoryImpl implements CustomStortionRepository { @@ -51,7 +52,7 @@ public class StortionRepositoryImpl implements CustomStortionRepository {
query.setParameter("max_age", (double)max_age);
query.setParameter("max_size", max_size);
return (Stream<Stortion>) query.getResultStream();
return query.getResultList();
}
}

58
src/main/java/ru/bvn13/adastor/web/services/StortionService.java

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
package ru.bvn13.adastor.web.services;
import org.apache.commons.codec.binary.Hex;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -18,9 +19,7 @@ import java.security.DigestInputStream; @@ -18,9 +19,7 @@ import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.util.Formatter;
import java.util.Optional;
import java.util.UUID;
import java.util.*;
import java.util.stream.Stream;
/**
@ -78,17 +77,35 @@ public class StortionService { @@ -78,17 +77,35 @@ public class StortionService {
String path = String.format("/%s", uuid);
String fullPath = String.format("%s/%s", config.getStoragePath(), uuid);
long bytesCount;
String hash;
try(DigestInputStream dis = new DigestInputStream(new BufferedInputStream(is), MessageDigest.getInstance("SHA-1")); FileOutputStream fos = new FileOutputStream(fullPath)) {
bytesCount = is.transferTo(fos);
hash = formatMessageDigestToHex(dis);
long bytesCount = 0;
MessageDigest md;
try {
md = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
throw new InternalServerError("SHA-1 not found, Sorry.", e);
}
try(
DigestInputStream dis = new DigestInputStream(new BufferedInputStream(is), md);
FileOutputStream fos = new FileOutputStream(fullPath)
) {
byte[] buffer = new byte[128];
int length = 0;
while ((length = dis.read(buffer)) > 0) {
fos.write(buffer, 0, length);
bytesCount += length;
}
}
if (dataLength != bytesCount) {
throw new InternalServerError("Something went wrong. Sorry.");
}
char[] hex = Hex.encodeHex(md.digest());
String hash = new String(hex);
Optional<StortionDto> similarByHash = findAnyByHash(hash);
if (similarByHash.isPresent()) {
(new File(fullPath)).delete();
throw new StortionExistByHash(similarByHash.get());
}
@ -97,27 +114,12 @@ public class StortionService { @@ -97,27 +114,12 @@ public class StortionService {
stortion.setStoreDate(LocalDateTime.now());
stortion.setPath(path);
stortion.setSize(bytesCount);
stortion.setHash(hash);
stortionRepository.save(stortion);
return convertToDto(stortion);
}
private String formatMessageDigestToHex(DigestInputStream dis) {
final MessageDigest md = dis.getMessageDigest();
final byte[] digest = md.digest();
// Format as HEX
try (Formatter formatter = new Formatter()) {
for (final byte b : digest) {
formatter.format("%02x", b);
}
final String sha1 = formatter.toString();
return sha1;
}
}
private StortionDto convertToDto(Stortion stortion) {
StortionDto stortionDto = modelMapper.map(stortion, StortionDto.class);
stortionDto.setRetention(computeRetention(stortion));
@ -125,8 +127,8 @@ public class StortionService { @@ -125,8 +127,8 @@ public class StortionService {
}
public Stream<StortionDto> findAllSortedByRetention() {
Stream<Stortion> stortions = stortionRepository.findAllSortedByRetention();
return stortions.map(this::convertToDto);
Collection<Stortion> stortions = stortionRepository.findAllSortedByRetention();
return stortions.stream().map(this::convertToDto);
}
/**
@ -143,10 +145,6 @@ public class StortionService { @@ -143,10 +145,6 @@ public class StortionService {
stortionRepository.deleteById(uuid);
}
private Iterable<Stortion> findAllByHash(String hash) {
return stortionRepository.findAllByHash(hash);
}
private Optional<StortionDto> findAnyByHash(String hash) {
return stortionRepository.findFirstByHash(hash).map(this::convertToDto);
}

Loading…
Cancel
Save