use of com.tesshu.jpsonic.domain.TransferStatus in project jpsonic by tesshucom.
the class StatusService method getAllStreamStatuses.
public List<TransferStatus> getAllStreamStatuses() {
synchronized (STREAM_LOCK) {
List<TransferStatus> result = new ArrayList<>(streamStatuses);
// Add inactive status for those players that have no active status.
Set<Integer> activePlayers = new HashSet<>();
for (TransferStatus status : streamStatuses) {
activePlayers.add(status.getPlayer().getId());
}
for (Map.Entry<Integer, TransferStatus> entry : inactiveStreamStatuses.entrySet()) {
if (!activePlayers.contains(entry.getKey())) {
result.add(entry.getValue());
}
}
return unmodifiableList(result);
}
}
use of com.tesshu.jpsonic.domain.TransferStatus in project jpsonic by tesshucom.
the class UploadController method handleRequestInternal.
@PostMapping
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) {
TransferStatus status = null;
UnzipResult result = null;
Map<String, Object> model = LegacyMap.of();
try {
status = statusService.createUploadStatus(playerService.getPlayer(request, response, false, false));
status.setBytesTotal(request.getContentLength());
request.getSession().setAttribute(Attributes.Session.UPLOAD_STATUS.value(), status);
// Check that we have a file upload request
if (!ServletFileUpload.isMultipartContent(request)) {
throw new ExecutionException(new IOException("Illegal request."));
}
List<FileItem> items = getUploadItems(request, status);
File dir = getDir(items);
if (dir == null) {
throw new IOException("Missing 'dir' parameter.");
}
boolean unzip = isUnzip(items);
result = doUnzip(items, dir, unzip);
} catch (IOException | FileUploadException e) {
if (LOG.isWarnEnabled()) {
LOG.warn("Uploading failed.", e);
}
model.put("exception", e);
} catch (ExecutionException e) {
ConcurrentUtils.handleCauseUnchecked(e);
if (LOG.isWarnEnabled()) {
LOG.warn("Uploading failed.", e);
}
model.put("exception", e);
} finally {
afterUpload(request, status);
}
if (result != null) {
model.put("uploadedFiles", result.getUploadedFiles());
model.put("unzippedFiles", result.getUnzippedFiles());
}
return new ModelAndView("upload", "model", model);
}
use of com.tesshu.jpsonic.domain.TransferStatus in project jpsonic by tesshucom.
the class StatusController method handleRequestInternal.
// (TransferStatusHolder) Not reusable
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
@GetMapping
protected ModelAndView handleRequestInternal(HttpServletRequest request) {
List<TransferStatus> streamStatuses = statusService.getAllStreamStatuses();
List<TransferStatus> downloadStatuses = statusService.getAllDownloadStatuses();
List<TransferStatus> uploadStatuses = statusService.getAllUploadStatuses();
Locale locale = RequestContextUtils.getLocale(request);
List<TransferStatusHolder> transferStatuses = new ArrayList<>();
for (int i = 0; i < streamStatuses.size(); i++) {
long minutesAgo = streamStatuses.get(i).getMillisSinceLastUpdate() / 1000L / 60L;
if (minutesAgo < LIMIT_OF_HISTORY_TO_BE_PRESENTED) {
transferStatuses.add(new TransferStatusHolder(streamStatuses.get(i), true, false, false, i, locale));
}
}
for (int i = 0; i < downloadStatuses.size(); i++) {
transferStatuses.add(new TransferStatusHolder(downloadStatuses.get(i), false, true, false, i, locale));
}
for (int i = 0; i < uploadStatuses.size(); i++) {
transferStatuses.add(new TransferStatusHolder(uploadStatuses.get(i), false, false, true, i, locale));
}
return new ModelAndView("status", "model", LegacyMap.of("brand", SettingsService.getBrand(), "admin", securityService.isAdmin(securityService.getCurrentUser(request).getUsername()), "showStatus", settingsService.isShowStatus(), "transferStatuses", transferStatuses, "chartWidth", StatusChartController.IMAGE_WIDTH, "chartHeight", StatusChartController.IMAGE_HEIGHT));
}
use of com.tesshu.jpsonic.domain.TransferStatus in project jpsonic by tesshucom.
the class StreamController method handleRequest.
/**
* @param req
* @param res
* @param isRest
* True if the call is from SubsonicRESTController
*
* @throws ServletRequestBindingException
*/
@GetMapping
public void handleRequest(HttpServletRequest req, HttpServletResponse res, Boolean isRest) throws ServletRequestBindingException {
final Player player = playerService.getPlayer(req, res, false, true);
final User user = securityService.getUserByName(player.getUsername());
final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (!(auth instanceof JWTAuthenticationToken) && !user.isStreamRole()) {
sendForbidden(res, "Streaming is forbidden for user " + user.getUsername());
return;
}
// Processing for all responses
res.setHeader("Access-Control-Allow-Origin", "*");
String contentType = getMimeType(req.getParameter(Attributes.Request.SUFFIX.value()));
res.setContentType(contentType);
// Processing for Podcast response only
Integer playlistId = getIntParameter(req, Attributes.Request.PLAYLIST.value());
final boolean isPodcast = playlistId != null;
if (isPodcast) {
streamService.setUpPlayQueue(req, res, player, playlistId);
}
MediaFile file = streamService.getSingleFile(req);
boolean isSingleFile = file != null;
String format = streamService.getFormat(req, player, isRest);
Integer maxBitRate = getMaxBitRate(req);
// Processing for a single file
PrepareResponseResult result = null;
if (isSingleFile) {
result = prepareResponse(req, res, auth, user, player, file, format, maxBitRate);
}
// If Header request or folder access not allowed, end here.
boolean isHeaderRequest = HttpMethod.HEAD.name().equals(req.getMethod());
if (result == null || result.isFolderAccessNotAllowed() || isHeaderRequest) {
writeVerboseLog(settingsService.isVerboseLogPlaying(), isHeaderRequest, file);
return;
}
writeVerboseLog(settingsService.isVerboseLogPlaying(), res, file);
streamService.closeAllStreamFor(player, isPodcast, isSingleFile);
TransferStatus status = statusService.createStreamStatus(player);
try (InputStream in = streamService.createInputStream(player, status, maxBitRate, format, result.getVideoTranscodingSettings());
OutputStream out = RangeOutputStream.wrap(res.getOutputStream(), result.getRange())) {
writeStream(player, in, out, result.getFileLengthExpected(), isPodcast, isSingleFile);
} catch (IOException e) {
writeErrorLog(e, req);
} finally {
streamService.removeStreamStatus(user, status);
}
}
use of com.tesshu.jpsonic.domain.TransferStatus in project jpsonic by tesshucom.
the class StreamControllerTest method initMocks.
private void initMocks(Player player, TranscodingService ts, StreamService ss) {
this.transcodingService = ts;
this.streamService = ss;
settingsService = mock(SettingsService.class);
securityService = mock(SecurityService.class);
User user = new User(player.getUsername(), player.getUsername(), "");
Mockito.when(securityService.getUserByName(player.getUsername())).thenReturn(user);
playerService = mock(PlayerService.class);
Mockito.when(playerService.getPlayer(Mockito.any(), Mockito.any(), Mockito.anyBoolean(), Mockito.anyBoolean())).thenReturn(player);
statusService = mock(StatusService.class);
TransferStatus transferStatus = mock(TransferStatus.class);
Mockito.when(transferStatus.getPlayer()).thenReturn(player);
Mockito.when(transferStatus.isTerminated()).thenReturn(true);
Mockito.when(transferStatus.isActive()).thenReturn(true);
Mockito.when(statusService.createStreamStatus(Mockito.nullable(Player.class))).thenReturn(transferStatus);
Mockito.when(statusService.getStreamStatusesForPlayer(Mockito.nullable(Player.class))).thenReturn(Arrays.asList(transferStatus));
streamController = new StreamController(settingsService, securityService, playerService, ts, statusService, ss);
JWTAuthenticationToken token = new JWTAuthenticationToken(Collections.emptyList(), ServiceMockUtils.ADMIN_NAME, null);
SecurityContextHolder.getContext().setAuthentication(token);
this.mockMvc = MockMvcBuilders.standaloneSetup(streamController).build();
}
Aggregations