Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -20,17 +20,13 @@ namespace Jellyfin.Plugin.MediaBarEnhanced.Api
|
||||
public OverlayImageController(IApplicationPaths applicationPaths)
|
||||
{
|
||||
_applicationPaths = applicationPaths;
|
||||
|
||||
// We use the plugin's data folder to store the image
|
||||
_imageDirectory = MediaBarEnhancedPlugin.Instance?.DataFolderPath ?? Path.Combine(applicationPaths.DataPath, "plugins", "MediaBarEnhanced");
|
||||
|
||||
// We no longer define the exact path here, just the directory
|
||||
// The filename is determined per request
|
||||
_imageDirectory = Path.Combine(applicationPaths.DataPath, "plugins", "configurations", "MediaBarEnhancedAssets");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Uploads a new custom overlay image.
|
||||
/// </summary>
|
||||
// [Microsoft.AspNetCore.Authorization.Authorize]
|
||||
[HttpPost("OverlayImage")]
|
||||
[Consumes("multipart/form-data")]
|
||||
public async Task<IActionResult> UploadImage([FromForm] IFormFile file, [FromQuery] string? filename = null)
|
||||
@@ -40,10 +36,12 @@ namespace Jellyfin.Plugin.MediaBarEnhanced.Api
|
||||
return BadRequest("No file uploaded.");
|
||||
}
|
||||
|
||||
string targetFileName = string.IsNullOrWhiteSpace(filename)
|
||||
? "custom_overlay_image.dat"
|
||||
: $"custom_overlay_image_{filename}.dat";
|
||||
string targetPath = Path.Combine(_imageDirectory, targetFileName);
|
||||
// Extract original extension or fallback to .jpg
|
||||
string extension = Path.GetExtension(file.FileName);
|
||||
if (string.IsNullOrWhiteSpace(extension)) extension = ".jpg";
|
||||
|
||||
// Delete any existing file with this prefix before saving the new one (as extensions might differ)
|
||||
string prefix = string.IsNullOrWhiteSpace(filename) ? "custom_overlay_image_global" : $"custom_overlay_image_{filename}";
|
||||
|
||||
try
|
||||
{
|
||||
@@ -52,6 +50,16 @@ namespace Jellyfin.Plugin.MediaBarEnhanced.Api
|
||||
Directory.CreateDirectory(_imageDirectory);
|
||||
}
|
||||
|
||||
// Remove existing
|
||||
var existingFiles = Directory.GetFiles(_imageDirectory, $"{prefix}.*");
|
||||
foreach(var extFile in existingFiles)
|
||||
{
|
||||
System.IO.File.Delete(extFile);
|
||||
}
|
||||
|
||||
string targetFileName = $"{prefix}{extension}";
|
||||
string targetPath = Path.Combine(_imageDirectory, targetFileName);
|
||||
|
||||
// Delete is not strictly necessary and can cause locking issues if someone is currently reading it.
|
||||
// FileMode.Create will truncate the file if it exists, effectively overwriting it.
|
||||
// We use FileShare.None to ensure we have exclusive write access, but handle potential IOExceptions gracefully.
|
||||
@@ -74,18 +82,20 @@ namespace Jellyfin.Plugin.MediaBarEnhanced.Api
|
||||
/// <summary>
|
||||
/// Retrieves the custom overlay image.
|
||||
/// </summary>
|
||||
// [Microsoft.AspNetCore.Authorization.Authorize]
|
||||
[HttpGet("OverlayImage")]
|
||||
public IActionResult GetImage([FromQuery] string? filename = null)
|
||||
{
|
||||
string targetFileName = string.IsNullOrWhiteSpace(filename)
|
||||
? "custom_overlay_image.dat"
|
||||
: $"custom_overlay_image_{filename}.dat";
|
||||
string targetPath = Path.Combine(_imageDirectory, targetFileName);
|
||||
|
||||
if (!System.IO.File.Exists(targetPath))
|
||||
{
|
||||
string prefix = string.IsNullOrWhiteSpace(filename) ? "custom_overlay_image_global" : $"custom_overlay_image_{filename}";
|
||||
|
||||
if (!Directory.Exists(_imageDirectory))
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
var existingFiles = Directory.GetFiles(_imageDirectory, $"{prefix}.*");
|
||||
if (existingFiles.Length == 0)
|
||||
return NotFound();
|
||||
|
||||
string targetPath = existingFiles[0];
|
||||
|
||||
// Read the file and return as a generic octet stream.
|
||||
// We use FileShare.ReadWrite so that if someone is currently overwriting the file (uploading), we don't block them,
|
||||
@@ -99,25 +109,27 @@ namespace Jellyfin.Plugin.MediaBarEnhanced.Api
|
||||
/// <summary>
|
||||
/// Deletes a custom overlay image.
|
||||
/// </summary>
|
||||
// [Microsoft.AspNetCore.Authorization.Authorize]
|
||||
[HttpDelete("OverlayImage")]
|
||||
public IActionResult DeleteImage([FromQuery] string? filename = null)
|
||||
{
|
||||
string targetFileName = string.IsNullOrWhiteSpace(filename)
|
||||
? "custom_overlay_image.dat"
|
||||
: $"custom_overlay_image_{filename}.dat";
|
||||
string targetPath = Path.Combine(_imageDirectory, targetFileName);
|
||||
|
||||
if (System.IO.File.Exists(targetPath))
|
||||
string prefix = string.IsNullOrWhiteSpace(filename) ? "custom_overlay_image_global" : $"custom_overlay_image_{filename}";
|
||||
|
||||
if (Directory.Exists(_imageDirectory))
|
||||
{
|
||||
try
|
||||
var existingFiles = Directory.GetFiles(_imageDirectory, $"{prefix}.*");
|
||||
foreach(var file in existingFiles)
|
||||
{
|
||||
System.IO.File.Delete(targetPath);
|
||||
return Ok();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return StatusCode(500, $"Error deleting file: {ex.Message}");
|
||||
try
|
||||
{
|
||||
System.IO.File.Delete(file);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return StatusCode(500, $"Error deleting file: {ex.Message}");
|
||||
}
|
||||
}
|
||||
return Ok();
|
||||
}
|
||||
|
||||
return NotFound();
|
||||
@@ -126,6 +138,7 @@ namespace Jellyfin.Plugin.MediaBarEnhanced.Api
|
||||
/// <summary>
|
||||
/// Renames a custom overlay image (used when a seasonal section is renamed).
|
||||
/// </summary>
|
||||
// [Microsoft.AspNetCore.Authorization.Authorize]
|
||||
[HttpPut("OverlayImage/Rename")]
|
||||
public IActionResult RenameImage([FromQuery] string oldName, [FromQuery] string newName)
|
||||
{
|
||||
@@ -134,22 +147,23 @@ namespace Jellyfin.Plugin.MediaBarEnhanced.Api
|
||||
return BadRequest("Both oldName and newName must be provided.");
|
||||
}
|
||||
|
||||
string oldPath = Path.Combine(_imageDirectory, $"custom_overlay_image_{oldName}.dat");
|
||||
string newPath = Path.Combine(_imageDirectory, $"custom_overlay_image_{newName}.dat");
|
||||
if (!Directory.Exists(_imageDirectory))
|
||||
return Ok();
|
||||
|
||||
if (!System.IO.File.Exists(oldPath))
|
||||
{
|
||||
// If it doesn't exist, there is nothing to rename, but we still consider it a success
|
||||
// since the end state (file with oldName is gone, file with newName doesn't exist yet) is acceptable.
|
||||
var oldFiles = Directory.GetFiles(_imageDirectory, $"custom_overlay_image_{oldName}.*");
|
||||
if (oldFiles.Length == 0)
|
||||
return Ok();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
string oldPath = oldFiles[0];
|
||||
string extension = Path.GetExtension(oldPath);
|
||||
string newPath = Path.Combine(_imageDirectory, $"custom_overlay_image_{newName}{extension}");
|
||||
|
||||
// If a file with the new name already exists, delete it first to avoid conflicts
|
||||
if (System.IO.File.Exists(newPath))
|
||||
{
|
||||
System.IO.File.Delete(newPath);
|
||||
var existingNewFiles = Directory.GetFiles(_imageDirectory, $"custom_overlay_image_{newName}.*");
|
||||
foreach(var existing in existingNewFiles) {
|
||||
System.IO.File.Delete(existing);
|
||||
}
|
||||
|
||||
System.IO.File.Move(oldPath, newPath);
|
||||
|
||||
Reference in New Issue
Block a user