|
7 | 7 | import com.google.gson.JsonParser; |
8 | 8 |
|
9 | 9 | import ai.elimu.entity.content.multimedia.Image; |
| 10 | +import ai.elimu.entity.content.multimedia.Video; |
10 | 11 | import ai.elimu.web.context.EnvironmentContextLoaderListener; |
11 | 12 | import kong.unirest.core.HttpResponse; |
12 | 13 | import kong.unirest.core.Unirest; |
|
19 | 20 | @Slf4j |
20 | 21 | public class GitHubLfsHelper { |
21 | 22 |
|
| 23 | + private static final String API_BASE_URL = "https://api.github.com/repos/elimu-ai/webapp-lfs/contents/"; |
| 24 | + |
22 | 25 | /** |
23 | | - * Upload file to LFS. |
| 26 | + * Upload image to LFS. |
24 | 27 | * |
25 | 28 | * @param image The Image representing the file bytes. |
26 | 29 | * @param bytes The file bytes to be stored. |
27 | | - * @return The hash (CID) generated by GitHub. |
| 30 | + * @return The checksum (SHA) generated by GitHub. |
28 | 31 | */ |
29 | 32 | public static String uploadImageToLfs(Image image, byte[] bytes) { |
30 | 33 | log.info("uploadImageToLfs"); |
31 | 34 |
|
32 | | - // Store file in webapp-lfs |
33 | | - // https://docs.github.com/en/rest/repos/contents?apiVersion=2022-11-28#create-or-update-file-contents |
34 | | - |
35 | 35 | String languageCode = EnvironmentContextLoaderListener.PROPERTIES.getProperty("content.language"); |
36 | 36 | log.info("languageCode: " + languageCode); |
37 | 37 |
|
38 | 38 | String filename = image.getId() + "_r" + image.getRevisionNumber() + "." + image.getImageFormat().toString().toLowerCase(); |
39 | 39 | log.info("filename: " + filename); |
| 40 | + |
| 41 | + String path = "lang-" + languageCode + "/images/" + filename; |
| 42 | + log.info("path: " + path); |
40 | 43 |
|
41 | | - String url = "https://api.github.com/repos/elimu-ai/webapp-lfs/contents/" + |
42 | | - "lang-" + languageCode + "/" + |
43 | | - "images/" + filename; |
44 | | - log.info("url: " + url); |
| 44 | + return uploadFileToLfs(path, bytes); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Upload video to LFS. |
| 49 | + * |
| 50 | + * @param video The Video representing the file bytes. |
| 51 | + * @param bytes The file bytes to be stored. |
| 52 | + * @return The checksum (SHA) generated by GitHub. |
| 53 | + */ |
| 54 | + public static String uploadVideoToLfs(Video video, byte[] bytes) { |
| 55 | + log.info("uploadVideoToLfs"); |
| 56 | + |
| 57 | + String languageCode = EnvironmentContextLoaderListener.PROPERTIES.getProperty("content.language"); |
| 58 | + log.info("languageCode: " + languageCode); |
| 59 | + |
| 60 | + String filename = video.getChecksumMd5() + "." + video.getVideoFormat().toString().toLowerCase(); |
| 61 | + log.info("filename: " + filename); |
| 62 | + |
| 63 | + String path = "lang-" + languageCode + "/videos/" + filename; |
| 64 | + log.info("path: " + path); |
45 | 65 |
|
| 66 | + return uploadFileToLfs(path, bytes); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Store file in webapp-lfs |
| 71 | + * https://docs.github.com/en/rest/repos/contents?apiVersion=2022-11-28#create-or-update-file-contents |
| 72 | + */ |
| 73 | + private static String uploadFileToLfs(String path, byte[] bytes) { |
| 74 | + log.info("uploadFileToLfs"); |
| 75 | + |
| 76 | + String url = API_BASE_URL + path; |
| 77 | + log.info("url: " + url); |
| 78 | + |
46 | 79 | JsonObject body = new JsonObject(); |
47 | | - body.addProperty("message", "chore(images): add " + filename); |
| 80 | + body.addProperty("message", "chore: add " + path ); |
48 | 81 | body.addProperty("content", Base64.getEncoder().encodeToString(bytes)); |
49 | 82 |
|
50 | 83 | JsonObject committer = new JsonObject(); |
|
0 commit comments