Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed initialization bug in watermark #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetApplyWatermarkSOI", "NetApplyWatermarkSOI\NetApplyWatermarkSOI.VS2012.csproj", "{62137B81-B479-48CD-9BB4-DC7A7EC2A835}"
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetApplyWatermarkSOI.VS2012", "NetApplyWatermarkSOI\NetApplyWatermarkSOI.VS2012.csproj", "{1C397940-76A9-4A87-B94B-16933B1F49D2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{62137B81-B479-48CD-9BB4-DC7A7EC2A835}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{62137B81-B479-48CD-9BB4-DC7A7EC2A835}.Debug|Any CPU.Build.0 = Debug|Any CPU
{62137B81-B479-48CD-9BB4-DC7A7EC2A835}.Release|Any CPU.ActiveCfg = Release|Any CPU
{62137B81-B479-48CD-9BB4-DC7A7EC2A835}.Release|Any CPU.Build.0 = Release|Any CPU
{1C397940-76A9-4A87-B94B-16933B1F49D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1C397940-76A9-4A87-B94B-16933B1F49D2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1C397940-76A9-4A87-B94B-16933B1F49D2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1C397940-76A9-4A87-B94B-16933B1F49D2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class NetApplyWatermarkSOI : IServerObjectExtension, IRESTRequestHandler,
private ServerLogger _serverLog;
private string _outputDirectory = string.Empty;
private RestSOIHelper _restSOIHelper;
private bool _directoryInitialized = false;

public NetApplyWatermarkSOI ()
{
Expand All @@ -60,38 +61,8 @@ public void Init ( IServerObjectHelper pSOH )
_soHelper = pSOH;
_serverLog = new ServerLogger();
_restSOIHelper = new RestSOIHelper(pSOH);

try
{
//interop problem?
var se4 = _restSOIHelper.ServerEnvironment as IServerEnvironmentEx;
var dirInfos = se4.GetServerDirectoryInfos();
dirInfos.Reset();
object dirInfo = dirInfos.Next();
while (dirInfo != null)
{
var dinfo2 = dirInfo as IServerDirectoryInfo2;
if (null != dinfo2 && dinfo2.Type == esriServerDirectoryType.esriSDTypeOutput)
{
_outputDirectory = dinfo2.Path;
break;
}
dirInfo = dirInfos.Next();
}
}
catch (Exception ignore)
{
_outputDirectory = string.Empty;
}

_outputDirectory = _outputDirectory.Trim();
if (string.IsNullOrEmpty(_outputDirectory))
{
_serverLog.LogMessage(ServerLogger.msgType.error, _soiName + ".init()", 500, "OutputDirectory is empty or missing. Reset to default.");
_outputDirectory = "C:\\arcgisserver\\directories\\arcgisoutput";
}

_serverLog.LogMessage(ServerLogger.msgType.infoDetailed, _soiName + ".init()", 500, "OutputDirectory is " + _outputDirectory);
// this won't work, serverenvironment not ready until after Init ...
//InitializeDirectory();
_serverLog.LogMessage(ServerLogger.msgType.infoStandard, _soiName + ".init()", 200, "Initialized " + _soiName + " SOI.");
}
catch (Exception e)
Expand Down Expand Up @@ -158,13 +129,19 @@ public byte[] HandleRESTRequest ( string Capabilities, string resourceName, stri
if (operationName.Equals("export", StringComparison.CurrentCultureIgnoreCase))
{
Image sourceImage = null;
var watermarker = new ApplyWatermark()
{
// knowing the processID makes it easier to attach the debugger ...
WatermarkText = $"ProcessID: {System.Diagnostics.Process.GetCurrentProcess().Id}"
};
if (!_directoryInitialized)
InitializeDirectory();

if (outputFormat.Equals("image", StringComparison.CurrentCultureIgnoreCase))
{
sourceImage = Image.FromStream(new System.IO.MemoryStream(response));

var watermarker = new ApplyWatermark();

var watermarkedImage = watermarker.Mark(sourceImage, "(c) ESRI Inc.");
var watermarkedImage = watermarker.Mark(sourceImage);
var newResponse = new System.IO.MemoryStream();
watermarkedImage.Save(newResponse, sourceImage.RawFormat);

Expand All @@ -188,14 +165,13 @@ public byte[] HandleRESTRequest ( string Capabilities, string resourceName, stri
// debug logging
//_serverLog.LogMessage(ServerLogger.msgType.error, "debug", 0, "output is " + outputImageFileLocation);

var watermarker = new ApplyWatermark();
Image watermarkedImage;

System.Drawing.Imaging.ImageFormat sourceImageFormat;
using( sourceImage = Image.FromFile(outputImageFileLocation))
{
sourceImageFormat = sourceImage.RawFormat;
watermarkedImage = watermarker.Mark(sourceImage, "(c) ESRI Inc.");
watermarkedImage = watermarker.Mark(sourceImage);
}
// make sure we dispose sourceImage handles before saving to it

Expand Down Expand Up @@ -319,6 +295,39 @@ public string HandleStringRequest ( string Capabilities, string request )

#region Utility code

private void InitializeDirectory()
{
try
{
var se4 = _restSOIHelper.ServerEnvironment as IServerEnvironmentEx;
var dirInfos = se4.GetServerDirectoryInfos();
dirInfos.Reset();
object dirInfo = dirInfos.Next();
while (dirInfo != null)
{
var dinfo2 = dirInfo as IServerDirectoryInfo2;
if (null != dinfo2 && dinfo2.Type == esriServerDirectoryType.esriSDTypeOutput)
{
_outputDirectory = dinfo2.Path;
break;
}
dirInfo = dirInfos.Next();
}
}
catch (Exception ignore)
{
_outputDirectory = string.Empty;
}

_outputDirectory = _outputDirectory.Trim();
if (string.IsNullOrEmpty(_outputDirectory))
{
_serverLog.LogMessage(ServerLogger.msgType.error, _soiName + ".init()", 500, "OutputDirectory is empty or missing. Reset to default.");
_outputDirectory = "C:\\arcgisserver\\directories\\arcgisoutput";
}
_serverLog.LogMessage(ServerLogger.msgType.infoDetailed, _soiName + ".init()", 500, "OutputDirectory is " + _outputDirectory);
_directoryInitialized = true;
}

/**
* Generate physical file path from virtual path
Expand Down