163 lines
No EOL
6.7 KiB
C#
163 lines
No EOL
6.7 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using JD.Adobe.Service.API;
|
|
using JetBrains.Annotations;
|
|
using Plume.Core.Logging;
|
|
using Plume.Interfaces.Services;
|
|
using Shuttle.Api.PhotoshopDTO;
|
|
using Shuttle.Api.ServerAPI;
|
|
using Shuttle.Servers;
|
|
using Shuttle.Servers.Managers.Notifications;
|
|
|
|
namespace JD.Adobe.Service.Implementation
|
|
{
|
|
public class PhotoshopService : AdobeService, IPhotoshopService
|
|
{
|
|
private IPhotoshopOperationExecutor _operationExecutor;
|
|
public PhotoshopService(ILogger logger) : base(logger, ServerConstants.ShuttleServerUrl)
|
|
{
|
|
}
|
|
|
|
protected override void InitializeImpl(IServiceContext context)
|
|
{
|
|
_operationExecutor = new PhotoshopOperationExecutor(Logger, _clientManager, _asyncProvider);
|
|
_isInitialized = true;
|
|
}
|
|
|
|
public override async Task<TargetAppInfo> StartNewInstance(AdobeApplicationType appType)
|
|
{
|
|
string requestToken = _asyncProvider.CreateResourceRequest("start-new-instance");
|
|
|
|
// define a local function to capture the token and avoid keeping a state in the service
|
|
void AcknowledgeConnection(TargetAppInfo targetAppInfo)
|
|
{
|
|
if (targetAppInfo.AppType != appType)
|
|
return;
|
|
_asyncProvider.DepositResource(requestToken, targetAppInfo);
|
|
}
|
|
|
|
_clientManager.OnTargetConnection += AcknowledgeConnection;
|
|
|
|
try
|
|
{
|
|
if (!_adobeExecutableManager.TryStartNewInstance(appType))
|
|
{
|
|
throw new ArgumentException($"Couldn't start a new instance of {appType}. Are you sure this Adobe application is installed on your machine?");
|
|
}
|
|
|
|
int waitingTime = 60_000;
|
|
bool success = await _asyncProvider.Wait(requestToken, waitingTime);
|
|
if (!success)
|
|
{
|
|
throw new TimeoutException($"{appType} took more than {waitingTime}ms to launch and establish a connection. " +
|
|
$"You may need to disable the Home startup menu from Edit > Preferences > General.");
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
_clientManager.OnTargetConnection -= AcknowledgeConnection;
|
|
}
|
|
|
|
TargetAppInfo startedAppInfo = _asyncProvider.GetResource<TargetAppInfo>(requestToken);
|
|
return startedAppInfo;
|
|
}
|
|
|
|
public bool IsPhotoshopRunning => CheckPhotoshopIsRunning();
|
|
|
|
private bool CheckPhotoshopIsRunning()
|
|
{
|
|
var processes = Process.GetProcessesByName("Photoshop");
|
|
return processes.Length > 0 && processes.Any(x => !x.HasExited);
|
|
}
|
|
|
|
#region TASKS
|
|
|
|
public async Task OpenProject(TargetAppInfo target, string projectFilePath)
|
|
{
|
|
ThrowIfUnknown(target);
|
|
if (!File.Exists(projectFilePath))
|
|
{
|
|
throw new ArgumentException($"{nameof(projectFilePath)} doesn't exist at '{projectFilePath}'.");
|
|
}
|
|
|
|
await _operationExecutor.OpenProject(target.ConnectionId, projectFilePath);
|
|
}
|
|
|
|
public async Task CloseActiveDocument(TargetAppInfo target)
|
|
{
|
|
ThrowIfUnknown(target);
|
|
await _operationExecutor.CloseActiveDocument(target.ConnectionId);
|
|
}
|
|
|
|
public async Task ExecuteAction(TargetAppInfo target, PhotoshopActionDefinition actionDefinition)
|
|
{
|
|
ThrowIfUnknown(target);
|
|
if (actionDefinition.ActionName == null) throw new ArgumentNullException(nameof(actionDefinition.ActionName));
|
|
if (actionDefinition.ActionSet == null) throw new ArgumentNullException(nameof(actionDefinition.ActionSet));
|
|
await _operationExecutor.ExecuteAction(target.ConnectionId, actionDefinition.ActionName, actionDefinition.ActionSet);
|
|
}
|
|
|
|
public async Task SaveActiveDocumentAsTGA(TargetAppInfo target, [NotNull] string outputPath, TargaSaveOptionsDTO targaSaveOptions)
|
|
{
|
|
ThrowIfUnknown(target);
|
|
if (outputPath == null) throw new ArgumentNullException(nameof(outputPath));
|
|
await _operationExecutor.SaveActiveDocumentAsTGA(target.ConnectionId, outputPath, targaSaveOptions);
|
|
}
|
|
|
|
public async Task SaveActiveDocumentAsPNG(TargetAppInfo target, string outputPath)
|
|
{
|
|
ThrowIfUnknown(target);
|
|
if (outputPath == null) throw new ArgumentNullException(nameof(outputPath));
|
|
await _operationExecutor.SaveActiveDocumentAsPNG(target.ConnectionId, outputPath);
|
|
}
|
|
|
|
public async Task ResizeDocument(TargetAppInfo target, int width, int height)
|
|
{
|
|
ThrowIfUnknown(target);
|
|
if (width <= 0) throw new ArgumentOutOfRangeException(nameof(width));
|
|
if (height <= 0) throw new ArgumentOutOfRangeException(nameof(height));
|
|
await _operationExecutor.ResizeDocument(target.ConnectionId, width, height);
|
|
|
|
}
|
|
|
|
public async Task SaveForWebJPEG(TargetAppInfo target, [NotNull] string outputPath)
|
|
{
|
|
ThrowIfUnknown(target);
|
|
if (outputPath == null) throw new ArgumentNullException(nameof(outputPath));
|
|
await _operationExecutor.SaveForWebJPEG(target.ConnectionId, outputPath);
|
|
}
|
|
|
|
public async Task SaveForWebPNG(TargetAppInfo target, [NotNull] string outputPath)
|
|
{
|
|
ThrowIfUnknown(target);
|
|
if (outputPath == null) throw new ArgumentNullException(nameof(outputPath));
|
|
await _operationExecutor.SaveForWebPNG(target.ConnectionId, outputPath);
|
|
}
|
|
|
|
public async Task SetLayerSetVisibility(TargetAppInfo target, string layerSetName, bool visibility)
|
|
{
|
|
ThrowIfUnknown(target);
|
|
if (layerSetName == null) throw new ArgumentNullException(nameof(layerSetName));
|
|
await _operationExecutor.SetLayerSetVisibility(target.ConnectionId, layerSetName, visibility);
|
|
}
|
|
|
|
public async Task SetLayerVisibility(TargetAppInfo target, string layerName, bool visibility)
|
|
{
|
|
ThrowIfUnknown(target);
|
|
if (layerName == null) throw new ArgumentNullException(nameof(layerName));
|
|
await _operationExecutor.SetLayerVisibility(target.ConnectionId, layerName, visibility);
|
|
}
|
|
|
|
public async Task SaveActiveDocumentAsJPEG(TargetAppInfo target, string outputPath)
|
|
{
|
|
ThrowIfUnknown(target);
|
|
if (outputPath == null) throw new ArgumentNullException(nameof(outputPath));
|
|
await _operationExecutor.SaveActiveDocumentAsJPEG(target.ConnectionId, outputPath);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |