-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from pbcccBeatBoard/main
repository edit
- Loading branch information
Showing
7 changed files
with
137 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package kr.co.mcmp.util; | ||
|
||
import kr.co.mcmp.dto.oss.component.CommonUploadComponent; | ||
import org.apache.commons.fileupload.FileItem; | ||
import org.apache.commons.fileupload.disk.DiskFileItem; | ||
import org.springframework.web.multipart.MultipartFile; | ||
import org.springframework.web.multipart.commons.CommonsMultipartFile; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
|
||
public class FileUtil { | ||
|
||
public static MultipartFile generatedMultipartFile(CommonUploadComponent.TextComponentDto textComponent) throws IOException { | ||
byte[] content = textComponent.getText().getBytes(); | ||
String fileName = textComponent.getFilename() + "." + textComponent.getExtension(); | ||
String mimeType = getMimeType(textComponent.getExtension()); | ||
|
||
FileItem fileItem = new DiskFileItem( | ||
"file", | ||
mimeType, | ||
false, | ||
fileName, | ||
content.length, | ||
null | ||
); | ||
|
||
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(content)) { | ||
fileItem.getOutputStream().write(inputStream.readAllBytes()); | ||
} | ||
|
||
return new CommonsMultipartFile(fileItem); | ||
} | ||
|
||
private static String getMimeType(String extension) { | ||
switch (extension.toLowerCase()) { | ||
case "txt": | ||
return "text/plain"; | ||
case "html": | ||
return "text/html"; | ||
case "sh": | ||
return "application/x-sh"; | ||
case "yaml": | ||
return "text/x-yaml"; | ||
default: | ||
return "application/octet-stream"; | ||
} | ||
} | ||
} |