Skip to content

Commit

Permalink
Add Downloadpath option
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Prange committed Aug 12, 2022
1 parent 48ad83b commit 27c45c1
Show file tree
Hide file tree
Showing 17 changed files with 329 additions and 234 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -20,7 +21,14 @@ class MainController {
private final UserService userService;

@GetMapping("/")
public String index() {
public String index(Principal principal) {
String username = "";
if (principal instanceof User) {
username = ((User)principal).getUsername();
} else {
username = principal.toString();
}
userService.setCurrentUser(username);
return "index";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public ModelAndView registerUserAccount(
String username = userForm.getUsername();
String password = new BCryptPasswordEncoder(11).encode(userForm.getPassword());
UserSettings userSettings = userService.saveUserSettings(new UserSettings());
userService.saveUser(new User(username, password, UserRole.USER.getExternalString(), true, userSettings));
userService.saveUser(new User(username, password, UserRole.USER.getExternalString(), false, userSettings));

try {
request.login(username, userForm.getPassword());
Expand Down Expand Up @@ -108,9 +108,7 @@ public ResponseEntity<?> updateUserSettings(
@RequestBody UserSettingsForm userSettingsForm, Principal principal) {
User user = userService.findUserByName(principal.getName());
UserSettings userSettingsById = user.getUserSettings();
userSettingsById.setDownloadSortBy(userSettingsForm.getDownloadSortBy());
userSettingsById.setRefreshrateInSeconds(userSettingsForm.getRefreshrateInSeconds());
userSettingsById.setSessionTimeout(userSettingsForm.getSessionTimeout());
userSettingsById.update(userSettingsForm);
userService.saveUserSettings(userSettingsById);
user.setUserSettings(userSettingsById);
userService.saveUser(user);
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/de/maggiwuerze/xdccloader/irc/IrcEventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import de.maggiwuerze.xdccloader.model.download.Download;
import de.maggiwuerze.xdccloader.model.download.DownloadState;
import de.maggiwuerze.xdccloader.model.entity.Bot;
import de.maggiwuerze.xdccloader.model.entity.User;
import de.maggiwuerze.xdccloader.service.DownloadService;
import de.maggiwuerze.xdccloader.service.UserService;
import java.io.File;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -21,6 +23,7 @@
import org.pircbotx.hooks.events.OutputEvent;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

@Component
Expand All @@ -29,9 +32,10 @@
public class IrcEventListener extends ListenerAdapter {

// final String DL_PATH = File.separator + "opt" + File.separator + "xdcc" + File.separator + "data";
private static final String DL_PATH = "." + File.separator + "xdcc";
private String downloadPath = "";
private final EventPublisher eventPublisher;
private final DownloadService downloadService;
private final UserService userService;

@Override
public void onBanList(BanListEvent event) {
Expand Down Expand Up @@ -70,10 +74,15 @@ public void onException(ExceptionEvent event) {

@Override
public void onIncomingFileTransfer(IncomingFileTransferEvent event) throws Exception {

if(downloadPath.isBlank()) {
downloadPath = userService.getCurrentUser().getUserSettings().getDownloadPath();
}

IrcBot bot = event.getBot();
Download download = downloadService.getById(bot.getDownloadId());
download.setFilename(event.getSafeFilename());
String path = DL_PATH + File.separatorChar + event.getSafeFilename();
String path = downloadPath + File.separatorChar + event.getSafeFilename();
File downloadFile = new File(path);

//Receive the file from the user
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.maggiwuerze.xdccloader.model.entity;

import de.maggiwuerze.xdccloader.model.download.DownloadSort;
import de.maggiwuerze.xdccloader.model.forms.UserSettingsForm;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -40,10 +41,19 @@ public class UserSettings {
@Column(nullable = false)
Long sessionTimeout = 300L;

@Column(nullable = false)
String downloadPath = "xdcc/";

public UserSettings(Long refreshrateInSeconds, Long sessionTimeout) {
this.refreshrateInSeconds = refreshrateInSeconds;
this.sessionTimeout = sessionTimeout;
}

public void update(UserSettingsForm newSettings) {
setDownloadSortBy(newSettings.getDownloadSortBy());
setRefreshrateInSeconds(newSettings.getRefreshrateInSeconds());
setSessionTimeout(newSettings.getSessionTimeout());
setDownloadPath(newSettings.getDownloadPath());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public class UserSettingsForm {
DownloadSort downloadSortBy;

Long sessionTimeout;

String downloadPath;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class UserSettingsTO {
Boolean showAllBotsInQuickWindow = false;
Map<Bot, Boolean> botsVisibleInQuickWindow = new HashMap<>();
Boolean showAllItemsInDownloadCard = true;
String downloadPath = "";
private Map<String, Boolean> itemsVisibleInDownloadCard;

public UserSettingsTO(Long refreshrateInSeconds, Long sessionTimeout) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@

@Data
public class UserTO {

String name;

String userRole;

Boolean initialized = false;

UserSettingsTO userSettings;

List<Bot> bots;
Expand All @@ -31,9 +27,7 @@ public UserTO(String name, String password, String userRole, boolean initialized
}

public UserTO(User user) {

BeanUtils.copyProperties(user, this);
userSettings = new UserSettingsTO(user.getUserSettings());

}
}
12 changes: 12 additions & 0 deletions src/main/java/de/maggiwuerze/xdccloader/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@
import de.maggiwuerze.xdccloader.model.entity.Bot;
import de.maggiwuerze.xdccloader.model.entity.User;
import de.maggiwuerze.xdccloader.model.entity.UserSettings;
import de.maggiwuerze.xdccloader.model.transport.UserTO;
import de.maggiwuerze.xdccloader.persistency.IrcUserRepository;
import de.maggiwuerze.xdccloader.persistency.UserRepository;
import de.maggiwuerze.xdccloader.persistency.UserSettingsRepository;
import java.util.List;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.context.annotation.SessionScope;

@Component
@SessionScope
@RequiredArgsConstructor
@Slf4j
public class UserService {
Expand All @@ -20,6 +25,9 @@ public class UserService {
private final IrcUserRepository ircUserRepository;
private final UserSettingsRepository userSettingsRepository;

@Getter
private UserTO currentUser;

public long getUserCount() {
return userRepository.count();
}
Expand All @@ -46,4 +54,8 @@ public User findUserByName(String username) {
public List<Bot> listIrcUsers() {
return ircUserRepository.findAll();
}

public void setCurrentUser(String currentUser) {
this.currentUser = new UserTO(findUserByName(currentUser));
}
}
Loading

0 comments on commit 27c45c1

Please sign in to comment.