Skip to content
This repository has been archived by the owner on Nov 6, 2024. It is now read-only.

Commit

Permalink
fix-checkstyle
Browse files Browse the repository at this point in the history
  • Loading branch information
A-Little-Excited committed Apr 22, 2024
1 parent 4d4a124 commit 341e95c
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ public class AliOSSFileStore implements FileStore {
private final OSS ossClient;
private final FileIdGenerator fileIdGenerator;

/**
* DI constructor.
*
* @param ossClient An Ali OSS Java client for managing OSS resources such as storage space and files. To initiate
* an OSS request using the Java SDK, you need to initialize an OSSClient instance and modify the default
* configuration items of the ClientConfiguration as needed.
* @param fileIdGenerator An object that provides file unique identifiers
*
* @throws NullPointerException if any constructor argument is {@code null}
*/
public AliOSSFileStore(@NotNull final OSS ossClient, @NotNull final FileIdGenerator fileIdGenerator) {
this.ossClient = Objects.requireNonNull(ossClient);
this.fileIdGenerator = Objects.requireNonNull(fileIdGenerator);
Expand All @@ -50,28 +60,17 @@ public AliOSSFileStore(@NotNull final OSS ossClient, @NotNull final FileIdGenera
@Override
public String upload(final File file) {
Objects.requireNonNull(file);
final String fileId = getFileIdGenerator().apply(file);
final String fileId = fileIdGenerator.apply(file);

getOSSClient()
.putObject(DEFAULT_BUCKET, fileId, file.getFileContent());
ossClient.putObject(DEFAULT_BUCKET, fileId, file.getFileContent());

return fileId;
}

@Override
public InputStream download(final String fileId) {
return getOSSClient()
return ossClient
.getObject(DEFAULT_BUCKET, fileId)
.getObjectContent();
}

@NotNull
private OSS getOSSClient() {
return this.ossClient;
}

@NotNull
private FileIdGenerator getFileIdGenerator() {
return this.fileIdGenerator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
import java.security.NoSuchAlgorithmException;
import java.util.Objects;

/**
* A configuration class responsible for injecting all the beans required by filecontroller
* <p>
* The key bean injected in this class is aliOssFileStore. This bean needs ossClient and fileIdGenerator as arguments.
* Users can provide the required configuration information when injecting ossClient.
*/
@Configuration
public class BeanConfig {

Expand All @@ -55,6 +61,18 @@ public class BeanConfig {
return new IllegalStateException(ErrorMessageFormat.CONFIG_NOT_FOUND.format());
});

/**
* Inject aliOssFileStore.
*
* @param ossClient An Ali OSS Java client for managing OSS resources such as storage space and files. To initiate
* an OSS request using the Java SDK, you need to initialize an OSSClient instance and modify the default
* configuration items of the ClientConfiguration as needed.
* @param fileIdGenerator An object that provides file unique identifiers.
*
* @return a fileStore for uploading and downloading files in Ali OSS
*
* @throws NullPointerException if any constructor argument is {@code null}
*/
@Bean
@ConditionalOnProperty(name = "athena.spring.alioss.enabled", havingValue = "true")
public AliOSSFileStore aliOssFileStore(
Expand All @@ -63,17 +81,42 @@ public AliOSSFileStore aliOssFileStore(
return new AliOSSFileStore(Objects.requireNonNull(ossClient), Objects.requireNonNull(fileIdGenerator));
}

/**
* Inject ossClient when the athena.spring.alioss.enabled configuration item is true.
*
* @param credentialsProvider Used to obtain the access credentials from environment variables.
*
* @return an Ali OSS Java client
*
* @throws NullPointerException if {@code credentialsProvider} is {@code null}
*/
@Bean
@ConditionalOnProperty(name = "athena.spring.alioss.enabled", havingValue = "true")
public OSS ossClient(@NotNull final EnvironmentVariableCredentialsProvider credentialsProvider) {
return new OSSClientBuilder().build(OSS_ENDPOINT, Objects.requireNonNull(credentialsProvider));
}

/**
* Inject fileIdGenerator.
*
* @param messageDigest An information summarization algorithm is provided.
*
* @return a FileIdGenerator generated based on a specified digest algorithm
*/
@Bean
public FileIdGenerator fileIdGenerator(@NotNull final MessageDigest messageDigest) {
return new FileNameAndUploadedTimeBasedIdGenerator(messageDigest);
}

/**
* Inject messageDigest according to the preset algorithm name to get a MessageDigest instance that provides the
* corresponding algorithm.
*
* @return a MessageDigest instance
*
* @throws IllegalStateException if the particular cryptographic algorithm is requested but is not available in the
* environment.
*/
@Bean
public MessageDigest messageDigest() {
try {
Expand All @@ -88,6 +131,14 @@ public MessageDigest messageDigest() {
}
}

/**
* Inject credentialsProvider when the athena.spring.alioss.enabled configuration item is true. Obtain the
* user-configured access credentials from the environment variable and inject them.
*
* @return an EnvironmentVariableCredentialsProvider loaded with the required access configuration
*
* @throws IllegalStateException if the client fails to send a request to OSS or transmit data.
*/
@Bean
@ConditionalOnProperty(name = "athena.spring.alioss.enabled", havingValue = "true")
public EnvironmentVariableCredentialsProvider credentialsProvider() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,42 @@
import java.io.InputStream;
import java.util.Objects;

/**
* A controller that receives user requests to upload and download files
* <p>
* Users need to supply a file object as the argument to upload a file, and receive a fileId generated based on the file
* information. Then users can find the corresponding file on Ali OSS based on the fileId and download it.
*/
@RestController
@RequestMapping("/file")
public class FileController {

@Autowired
private AliOSSFileStore aliOSSFileStore;

/**
* Receive a request to upload a file. Then upload the file to Ali OSS and store the file metadata in the database.
*
* @param file The file content and the file metadata
*
* @return the generated fileId of the uploaded file
*
* @throws NullPointerException if {@code file} is {@code null}
*/
@RequestMapping("/upload")
public String upload(@NotNull final File file) {
return aliOSSFileStore.upload(Objects.requireNonNull(file));
}

/**
* Receive a request to download a file and find the corresponding file on Ali OSS based on the fileId.
*
* @param fileId The fileId of the file requested to be downloaded
*
* @return the inputStream of the file content
*
* @throws NullPointerException if {@code fileId} is {@code null}
*/
@RequestMapping("/download")
public InputStream download(@NotNull final String fileId) {
return aliOSSFileStore.download(Objects.requireNonNull(fileId));
Expand Down

0 comments on commit 341e95c

Please sign in to comment.