Skip to content

Commit

Permalink
fix: Save files under understandable folders (#288)
Browse files Browse the repository at this point in the history
Signed-off-by: carlosthe19916 <[email protected]>
  • Loading branch information
carlosthe19916 committed May 29, 2023
1 parent e6394b7 commit 6729423
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import io.github.project.openubl.ublhub.documents.exceptions.NoCertificateToSignFoundException;
import io.github.project.openubl.ublhub.documents.exceptions.NoUBLXMLFileCompliantException;
import io.github.project.openubl.ublhub.documents.exceptions.ProjectNotFoundException;
import io.github.project.openubl.ublhub.files.FilesManager;
import io.github.project.openubl.ublhub.files.UblhubFileConstants;
import io.github.project.openubl.ublhub.models.jpa.entities.SunatEntity;
import io.github.project.openubl.xbuilder.content.models.standard.general.CreditNote;
import io.github.project.openubl.xbuilder.content.models.standard.general.DebitNote;
Expand Down Expand Up @@ -53,7 +55,9 @@

import javax.enterprise.context.ApplicationScoped;
import javax.json.JsonObject;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import static io.github.project.openubl.xsender.camel.utils.CamelUtils.getBillServiceCamelData;
Expand Down Expand Up @@ -286,6 +290,11 @@ public void configure() throws Exception {
.handled(true)
.end()

.process(exchange -> {
String projectName = exchange.getIn().getHeader(DocumentRoute.DOCUMENT_PROJECT, String.class);
List<String> baseFolder = Arrays.asList(projectName, UblhubFileConstants.XML_BASE_PATH);
exchange.getIn().setHeader(FilesManager.FILE_FOLDERS, baseFolder);
})
.setHeader("shouldZipFile", constant(true))
.enrich("direct:" + storageType + "-save-file", (oldExchange, newExchange) -> {
String documentFileId = newExchange.getIn().getBody(String.class);
Expand Down Expand Up @@ -397,6 +406,11 @@ public void configure() throws Exception {
.endChoice()
.end()

.process(exchange -> {
String projectName = exchange.getIn().getHeader(DocumentRoute.DOCUMENT_PROJECT, String.class);
List<String> baseFolder = Arrays.asList(projectName, UblhubFileConstants.CDR_BASE_PATH);
exchange.getIn().setHeader(FilesManager.FILE_FOLDERS, baseFolder);
})
.choice()
.when(body().isNotNull())
.setHeader("shouldZipFile", constant(false))
Expand Down Expand Up @@ -476,6 +490,11 @@ public void configure() throws Exception {
.bean("documentBean", "saveSunatResponse")
.choice()
.when(body().isNotNull())
.process(exchange -> {
String projectName = exchange.getIn().getHeader(DocumentRoute.DOCUMENT_PROJECT, String.class);
List<String> baseFolder = Arrays.asList(projectName, UblhubFileConstants.CDR_BASE_PATH);
exchange.getIn().setHeader(FilesManager.FILE_FOLDERS, baseFolder);
})
.setHeader("shouldZipFile", constant(false))
.to("direct:" + storageType + "-save-file")
.bean("documentBean", "saveCdr")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
*/
package io.github.project.openubl.ublhub.files;

import com.github.f4b6a3.tsid.TsidFactory;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.eclipse.microprofile.config.inject.ConfigProperty;

import javax.enterprise.context.ApplicationScoped;
Expand All @@ -25,21 +27,26 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

@ApplicationScoped
public class FilesManager {

public static final String FILE_FOLDERS = "ObjectFolders";
public static final String DEFAULT_FILENAME = "DefaultFilename";

@Inject
CamelContext camelContext;

@ConfigProperty(name = "openubl.storage.type")
String storageType;

public String createFile(byte[] file, boolean shouldZipFile) {
public String createFile(List<String> folders, byte[] file, boolean shouldZipFile) {
Map<String, Object> headers = new HashMap<>();
headers.put("shouldZipFile", shouldZipFile);
headers.put(FilesManager.FILE_FOLDERS, folders);

return camelContext
.createProducerTemplate()
Expand Down Expand Up @@ -86,4 +93,17 @@ public void delete(String fileID) {
.requestBody("direct:" + storageType + "-delete-file", fileID);
}

public static String generateZipFilename(TsidFactory tsidFactory, Exchange exchange) {
Path fileDir = Paths.get("");
List<String> parentFileDirectories = exchange.getIn().getHeader(FilesManager.FILE_FOLDERS, List.class);
if (parentFileDirectories != null) {
for (String folder : parentFileDirectories) {
fileDir = fileDir.resolve(folder);
}
}

String filename = Optional.ofNullable(exchange.getIn().getHeader(FilesManager.DEFAULT_FILENAME, String.class))
.orElse(tsidFactory.create() + ".zip");
return fileDir.resolve(filename).toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2019 Project OpenUBL, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.project.openubl.ublhub.files;

public class UblhubFileConstants {
public static final String XML_BASE_PATH = "xml";
public static final String CDR_BASE_PATH = "cdr";
public static final String IMG_BASE_PATH = "images";
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,26 @@
*/
package io.github.project.openubl.ublhub.files.camel;

import com.github.f4b6a3.tsid.TsidFactory;
import io.github.project.openubl.ublhub.files.FilesManager;
import org.apache.camel.LoggingLevel;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.file.FileConstants;
import org.eclipse.microprofile.config.inject.ConfigProperty;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.UUID;

@ApplicationScoped
public class FilesystemRoute extends RouteBuilder {

@Inject
TsidFactory tsidFactory;

@ConfigProperty(name = "openubl.storage.type")
String storageType;

Expand All @@ -46,12 +52,16 @@ public void configure() throws Exception {
.marshal().zipFile()
.endChoice()
.end()
.setHeader("CamelFileName", () -> UUID.randomUUID() + ".zip")
.setHeader("folderName", constant(fileSystemFolder))
.process(exchange -> {
String filename = FilesManager.generateZipFilename(tsidFactory, exchange);

exchange.getIn().setHeader("folderName", fileSystemFolder);
exchange.getIn().setHeader(FileConstants.FILE_NAME, filename);
})
.toD("file:${header.folderName}")
.process(exchange -> {
String folderName = exchange.getIn().getHeader("folderName", String.class);
String fileName = exchange.getIn().getHeader("CamelFileName", String.class);
String fileName = exchange.getIn().getHeader(FileConstants.FILE_NAME, String.class);

Path resolve = Paths.get(folderName).resolve(fileName);
exchange.getIn().setBody(resolve.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package io.github.project.openubl.ublhub.files.camel;

import com.github.f4b6a3.tsid.TsidFactory;
import io.github.project.openubl.ublhub.files.FilesManager;
import io.minio.MinioClient;
import org.apache.camel.LoggingLevel;
import org.apache.camel.builder.RouteBuilder;
Expand All @@ -24,15 +26,18 @@

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import java.util.Iterator;
import java.util.Optional;
import java.util.UUID;

@ApplicationScoped
public class MinioFilesRoute extends RouteBuilder {

@Inject
TsidFactory tsidFactory;

@ConfigProperty(name = "openubl.storage.type")
String storageType;

Expand Down Expand Up @@ -69,7 +74,9 @@ public void configure() throws Exception {
.endChoice()
.end()
.process(exchange -> {
exchange.getIn().setHeader(MinioConstants.OBJECT_NAME, UUID.randomUUID().toString());
String filename = FilesManager.generateZipFilename(tsidFactory, exchange);

exchange.getIn().setHeader(MinioConstants.OBJECT_NAME, filename);
exchange.getIn().setHeader(MinioConstants.DESTINATION_BUCKET_NAME, s3Bucket);
})
.toD("minio://" + s3Bucket + "?autoCreateBucket=true&deleteAfterWrite=true&minioClient=#minioClient")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package io.github.project.openubl.ublhub.files.camel;

import com.github.f4b6a3.tsid.TsidFactory;
import io.github.project.openubl.ublhub.files.FilesManager;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.aws2.s3.AWS2S3Constants;
Expand All @@ -31,15 +33,18 @@

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Named;
import java.net.URI;
import java.util.Iterator;
import java.util.Optional;
import java.util.UUID;

@ApplicationScoped
public class S3FilesRoute extends RouteBuilder {

@Inject
TsidFactory tsidFactory;

@ConfigProperty(name = "openubl.storage.type")
String storageType;

Expand Down Expand Up @@ -117,7 +122,9 @@ public void configure() throws Exception {
.endChoice()
.end()
.process(exchange -> {
exchange.getIn().setHeader(AWS2S3Constants.KEY, UUID.randomUUID().toString());
String filename = FilesManager.generateZipFilename(tsidFactory, exchange);

exchange.getIn().setHeader(AWS2S3Constants.KEY, filename);
exchange.getIn().setHeader(AWS2S3Constants.BUCKET_DESTINATION_NAME, s3Bucket);
})
.toD("aws2-s3://" + s3Bucket + "?autoCreateBucket=true&deleteAfterWrite=true&amazonS3Client=#s3client")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
*/

import io.github.project.openubl.ublhub.dto.CompanyDto;
import io.github.project.openubl.ublhub.files.UblhubFileConstants;
import io.github.project.openubl.ublhub.files.FilesManager;
import io.github.project.openubl.ublhub.keys.DefaultKeyProviders;
import io.github.project.openubl.ublhub.keys.component.ComponentOwner;
Expand All @@ -56,6 +57,7 @@
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.function.Function;
Expand Down Expand Up @@ -163,7 +165,7 @@ public RestResponse<CompanyDto> createCompany(
if (companyDto.getLogo() != null && !companyDto.getLogo().isEmpty()) {
String logoBase64 = companyDto.getLogo().trim().replaceFirst("data[:]image[/]([a-z])+;base64,", "");
byte[] logoBytes = Base64.getDecoder().decode(logoBase64);
logoFileId = filesManager.createFile(logoBytes, true);
logoFileId = filesManager.createFile(Arrays.asList(project, UblhubFileConstants.IMG_BASE_PATH), logoBytes, true);
}

companyEntity = companyMapper.updateEntityFromDto(companyDto, CompanyEntity.builder()
Expand Down Expand Up @@ -209,7 +211,7 @@ public RestResponse<CompanyDto> updateCompany(
if (companyDto.getLogo() != null && !companyDto.getLogo().isEmpty()) {
String logoBase64 = companyDto.getLogo().trim().replaceFirst("data:image/([a-z])+;base64,", "");
byte[] logoBytes = Base64.getDecoder().decode(logoBase64);
String logoFileId = filesManager.createFile(logoBytes, true);
String logoFileId = filesManager.createFile(Arrays.asList(project, UblhubFileConstants.IMG_BASE_PATH), logoBytes, true);

companyEntity.setLogoFileId(logoFileId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import io.quarkus.qute.Template;
import io.quarkus.security.identity.SecurityIdentity;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.component.file.FileConstants;
import org.jboss.logging.Logger;
import org.jboss.resteasy.reactive.MultipartForm;
import org.jboss.resteasy.reactive.RestForm;
Expand Down Expand Up @@ -182,7 +183,10 @@ public RestResponse<DocumentDto> uploadXML(

File file = formData.file.uploadedFile().toFile();

Map<String, Object> headers = Map.of(DocumentRoute.DOCUMENT_PROJECT, project);
Map<String, Object> headers = Map.of(
DocumentRoute.DOCUMENT_PROJECT, project,
FileConstants.FILE_NAME, UUID.randomUUID() + ".xml"
);
DocumentImportResult importResult = producerTemplate.requestBodyAndHeaders("direct:import-xml", file, headers, DocumentImportResult.class);

return mapDocumentImportResult(project, importResult);
Expand All @@ -199,7 +203,10 @@ public RestResponse<DocumentDto> createDocument(
return documentDtoNotFoundResponse.get();
}

Map<String, Object> headers = Map.of(DocumentRoute.DOCUMENT_PROJECT, project);
Map<String, Object> headers = Map.of(
DocumentRoute.DOCUMENT_PROJECT, project,
FileConstants.FILE_NAME, UUID.randomUUID() + ".xml"
);
DocumentImportResult importResult = producerTemplate.requestBodyAndHeaders("direct:import-json", jsonObject, headers, DocumentImportResult.class);

return mapDocumentImportResult(project, importResult);
Expand Down Expand Up @@ -258,7 +265,7 @@ public RestResponse<String> renderDocument(
.entity(xmlString)
.build();
} else {
throw new IllegalStateException("Unexpected result");
throw new IllegalStateException("Unexpected result");
}
}

Expand Down
Loading

0 comments on commit 6729423

Please sign in to comment.