From ba96490d51d8e4c2f56cd87807d10bd620cc7531 Mon Sep 17 00:00:00 2001 From: Akshath Sai Pittala Date: Wed, 26 Jun 2024 16:48:24 +0530 Subject: [PATCH] - Added User Preferences & other UX Improvements --- pom.xml | 2 +- .../streamspace/entity/Movie.java | 2 + .../streamspace/entity/Preference.java | 21 ++++++++++ .../streamspace/indexer/MediaIndexer.java | 42 ++++++++++--------- .../repository/UserPreferences.java | 11 +++++ .../services/BackgroundServices.java | 21 ++++++++-- .../streamspace/web/api/PreferencesAPI.java | 37 ++++++++++++++++ .../web/controllers/WebController.java | 14 ++++++- src/main/resources/application.properties | 4 +- src/main/resources/templates/downloads.html | 1 + src/main/resources/templates/index.html | 37 ++++++++-------- src/main/resources/templates/player.html | 2 +- src/main/resources/templates/ytsMovie.html | 36 ++++++++++------ 13 files changed, 171 insertions(+), 59 deletions(-) create mode 100644 src/main/java/com/akshathsaipittala/streamspace/entity/Preference.java create mode 100644 src/main/java/com/akshathsaipittala/streamspace/repository/UserPreferences.java create mode 100644 src/main/java/com/akshathsaipittala/streamspace/web/api/PreferencesAPI.java diff --git a/pom.xml b/pom.xml index 9ae9efb..d167c9e 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 3.2.3 + 3.3.0 com.akshathsaipittala diff --git a/src/main/java/com/akshathsaipittala/streamspace/entity/Movie.java b/src/main/java/com/akshathsaipittala/streamspace/entity/Movie.java index a56afd2..e5fe99b 100644 --- a/src/main/java/com/akshathsaipittala/streamspace/entity/Movie.java +++ b/src/main/java/com/akshathsaipittala/streamspace/entity/Movie.java @@ -6,6 +6,7 @@ import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; +import lombok.experimental.Accessors; import org.springframework.content.commons.annotations.ContentId; import org.springframework.content.commons.annotations.ContentLength; import org.springframework.content.commons.annotations.MimeType; @@ -17,6 +18,7 @@ @Entity @Getter @Setter +@Accessors(chain = true) @NoArgsConstructor public class Movie implements Serializable { diff --git a/src/main/java/com/akshathsaipittala/streamspace/entity/Preference.java b/src/main/java/com/akshathsaipittala/streamspace/entity/Preference.java new file mode 100644 index 0000000..c9e0cf5 --- /dev/null +++ b/src/main/java/com/akshathsaipittala/streamspace/entity/Preference.java @@ -0,0 +1,21 @@ +package com.akshathsaipittala.streamspace.entity; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.experimental.Accessors; + +@Entity +@Getter +@Setter +@Accessors(chain = true) +@NoArgsConstructor +public class Preference { + + @Id + private Integer prefId; + private String name; + private boolean enabled; +} diff --git a/src/main/java/com/akshathsaipittala/streamspace/indexer/MediaIndexer.java b/src/main/java/com/akshathsaipittala/streamspace/indexer/MediaIndexer.java index b7b329a..2bb2519 100644 --- a/src/main/java/com/akshathsaipittala/streamspace/indexer/MediaIndexer.java +++ b/src/main/java/com/akshathsaipittala/streamspace/indexer/MediaIndexer.java @@ -50,22 +50,24 @@ public void indexMovie(TorrentFile file, String torrentName, String fileName, To if (movie != null) { log.info("Movie Found {}", movie); movieRepository.delete(movie); - // Primary key cannot be updated due to which new record is created need to revisit + // TODO: need to revisit + // Primary key cannot be updated due to which new record is created + // hence deleting and inserting as new with latest torrentId movie.setMovieCode(torrentId.toString().toUpperCase()); log.info("{} already indexed", fileName); movieRepository.save(movie); } else { - movie = new Movie(); - movie.setContentLength(file.getSize()); - movie.setName(fileName); - movie.setSummary(fileName); - movie.setContentMimeType(MediaType.APPLICATION_OCTET_STREAM_VALUE); - log.info(runtimeHelper.getMoviesContentStore() + torrentName + "/" + fileName); - movie.setContentId(runtimeHelper.getMoviesContentStore() + torrentName + "/" + fileName); - movie.setMovieCode(torrentId.toString().toUpperCase()); - movie.setMediaSource(ApplicationConstants.TORRENT); + movie = new Movie() + .setContentLength(file.getSize()) + .setName(fileName) + .setSummary(fileName) + .setContentMimeType(MediaType.APPLICATION_OCTET_STREAM_VALUE) + .setContentId(runtimeHelper.getMoviesContentStore() + torrentName + "/" + fileName) + .setMovieCode(torrentId.toString().toUpperCase()) + .setMediaSource(ApplicationConstants.TORRENT); + + log.info("Content ID {}", runtimeHelper.getMoviesContentStore() + torrentName + "/" + fileName); movieRepository.save(movie); - log.info("{}", movie); } } @@ -84,7 +86,6 @@ public void indexMusic(TorrentFile file, String torrentName, String fileName, To song.setSongId(torrentId.toString().toUpperCase()); song.setMediaSource(ApplicationConstants.TORRENT); musicRepository.save(song); - log.info("{}", song); } /** @@ -198,14 +199,15 @@ private List createMovieEntities(List paths) throws IOException { log.debug("Content Store {}", contentStoreDir); log.debug("Local Directory {}", userDir); - Movie movie = new Movie(); - movie.setName(encodedFileName); - movie.setContentLength(Files.size(entry)); - movie.setSummary(entry.getFileName().toString()); - movie.setContentId(decodePathSegment.apply(contentStoreDir)); - movie.setContentMimeType(decodeContentType.apply(entry)); - movie.setMovieCode(HelperFunctions.generateUniqueCode()); - movie.setMediaSource(ApplicationConstants.LOCAL_MEDIA); + Movie movie = new Movie() + .setName(encodedFileName) + .setContentLength(Files.size(entry)) + .setSummary(entry.getFileName().toString()) + .setContentId(decodePathSegment.apply(contentStoreDir)) + .setContentMimeType(decodeContentType.apply(entry)) + .setMovieCode(HelperFunctions.generateUniqueCode()) + .setMediaSource(ApplicationConstants.LOCAL_MEDIA); + moviesList.add(movie); } diff --git a/src/main/java/com/akshathsaipittala/streamspace/repository/UserPreferences.java b/src/main/java/com/akshathsaipittala/streamspace/repository/UserPreferences.java new file mode 100644 index 0000000..c5ba525 --- /dev/null +++ b/src/main/java/com/akshathsaipittala/streamspace/repository/UserPreferences.java @@ -0,0 +1,11 @@ +package com.akshathsaipittala.streamspace.repository; + +import com.akshathsaipittala.streamspace.entity.Preference; +import org.springframework.data.repository.ListCrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface UserPreferences extends ListCrudRepository { + +} + \ No newline at end of file diff --git a/src/main/java/com/akshathsaipittala/streamspace/services/BackgroundServices.java b/src/main/java/com/akshathsaipittala/streamspace/services/BackgroundServices.java index c78f3b7..296314b 100644 --- a/src/main/java/com/akshathsaipittala/streamspace/services/BackgroundServices.java +++ b/src/main/java/com/akshathsaipittala/streamspace/services/BackgroundServices.java @@ -1,11 +1,9 @@ package com.akshathsaipittala.streamspace.services; -import com.akshathsaipittala.streamspace.entity.CONTENTTYPE; -import com.akshathsaipittala.streamspace.entity.DownloadTask; -import com.akshathsaipittala.streamspace.entity.STATUS; -import com.akshathsaipittala.streamspace.entity.DownloadTaskSpecs; +import com.akshathsaipittala.streamspace.entity.*; import com.akshathsaipittala.streamspace.indexer.MediaIndexer; import com.akshathsaipittala.streamspace.repository.DownloadTaskRepository; +import com.akshathsaipittala.streamspace.repository.UserPreferences; import com.akshathsaipittala.streamspace.utils.RuntimeHelper; import jakarta.annotation.PreDestroy; import lombok.extern.slf4j.Slf4j; @@ -41,12 +39,20 @@ public class BackgroundServices { @Autowired private TorrentDownloadService torrentDownloadService; + @Lazy + @Autowired + private UserPreferences userPreferences; @Async @EventListener(ApplicationReadyEvent.class) public void onApplicationReadyEvent() { try { log.info("Indexing Local Media"); + // Runs only during initial setup + if (userPreferences.count() == 0) { + configurePreferences(); + } + mediaIndexer.indexLocalMedia( runtimeHelper.getMediaFolders().get(CONTENTTYPE.VIDEO), runtimeHelper.getMediaFolders().get(CONTENTTYPE.AUDIO) @@ -60,6 +66,13 @@ public void onApplicationReadyEvent() { } } + private void configurePreferences() { + List features = List.of( + new Preference().setPrefId(1).setName("DARK_MODE_ENABLED") + ); + userPreferences.saveAll(features); + } + @Async public void startBackgroundDownloads() { diff --git a/src/main/java/com/akshathsaipittala/streamspace/web/api/PreferencesAPI.java b/src/main/java/com/akshathsaipittala/streamspace/web/api/PreferencesAPI.java new file mode 100644 index 0000000..e4f5387 --- /dev/null +++ b/src/main/java/com/akshathsaipittala/streamspace/web/api/PreferencesAPI.java @@ -0,0 +1,37 @@ +package com.akshathsaipittala.streamspace.web.api; + +import com.akshathsaipittala.streamspace.entity.Preference; +import com.akshathsaipittala.streamspace.repository.UserPreferences; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PatchMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Optional; + +@Slf4j +@RestController +@RequestMapping("/preference") +@RequiredArgsConstructor +public class PreferencesAPI { + + final UserPreferences userPreferences; + + @PatchMapping + public ResponseEntity saveDarkModePreference(@RequestBody Preference preference) { + Optional existingPref = userPreferences.findById(preference.getPrefId()); + if (existingPref.isPresent()) { + Preference updatedPref = existingPref.get(); + updatedPref.setEnabled(preference.isEnabled()); + userPreferences.save(updatedPref); + } else { + //userPreferences.save(preference); + log.info("Preference with id {} not found", preference.getPrefId()); + } + return ResponseEntity.ok().build(); + } + +} diff --git a/src/main/java/com/akshathsaipittala/streamspace/web/controllers/WebController.java b/src/main/java/com/akshathsaipittala/streamspace/web/controllers/WebController.java index 0fe8ba2..08d3409 100644 --- a/src/main/java/com/akshathsaipittala/streamspace/web/controllers/WebController.java +++ b/src/main/java/com/akshathsaipittala/streamspace/web/controllers/WebController.java @@ -1,13 +1,25 @@ package com.akshathsaipittala.streamspace.web.controllers; +import com.akshathsaipittala.streamspace.entity.Preference; +import com.akshathsaipittala.streamspace.repository.UserPreferences; +import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; +import java.util.Optional; + @Controller +@RequiredArgsConstructor public class WebController { + final UserPreferences userPreferences; + @GetMapping("/") - public String index() { + public String index(Model model) { + Optional darkModePreference = userPreferences.findById(1); + boolean darkModeEnabled = darkModePreference.map(Preference::isEnabled).orElse(false); + model.addAttribute("darkmodeenabled", darkModeEnabled); return "index"; } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 650cd30..d5d8b51 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -18,8 +18,8 @@ management.endpoints.web.exposure.include=health,metrics #server.ssl.bundle=web-server #server.ssl.client-auth=none -spring.datasource.url=jdbc:h2:file:~/.h2 -#spring.datasource.url=jdbc:h2:mem:db +#spring.datasource.url=jdbc:h2:file:~/.h2 +spring.datasource.url=jdbc:h2:mem:db spring.datasource.username=sa spring.datasource.password=password spring.datasource.driver-class-name=org.h2.Driver diff --git a/src/main/resources/templates/downloads.html b/src/main/resources/templates/downloads.html index cb1519c..2a25432 100644 --- a/src/main/resources/templates/downloads.html +++ b/src/main/resources/templates/downloads.html @@ -80,6 +80,7 @@

Torrents Web Downloader
+ diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index b3a7b60..7933ef4 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -1,7 +1,8 @@ + lang="en" + th:attrappend="data-bs-theme=${darkmodeenabled ? 'dark' : ''}"> Stream Space @@ -169,8 +170,7 @@ } .dark { background-color: black; - color: white; - + /*color: white;*/ } /* YT TRAILER MODAL-------------------------------------------------- */ @@ -288,8 +288,7 @@ - - +
@@ -316,7 +315,7 @@    - +
@@ -627,17 +626,21 @@

Download Queue