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

Master #5

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
Binary file added UpgradeLog.htm
Binary file not shown.
70 changes: 42 additions & 28 deletions WatchFolderService/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,19 @@
using Amazon.S3.Model;
using System.Web;
using System.Web.Script.Serialization;

using System.ComponentModel;
using System.Diagnostics;
namespace WatchFolderService
{
public class Common
{
// This is the target server url
public static string server = "foo.bar.com";

public static string uriStem = "https://" + server + "/Panopto/PublicAPI/REST";
public static readonly string AuthCookieName = ".ASPXAUTH";

public static readonly string UploadBucketName = "Upload";
public static readonly string UploadTargetPathFragment_Panopto = "/Panopto/";
public static readonly string UploadTargetPathFragment_PanoptoUpload = "/Panopto/Upload/";

/// <summary>
/// Change the server that the program is directed towards
/// </summary>
Expand Down Expand Up @@ -165,7 +163,7 @@ public static HttpWebRequest CreateRequest(
/// <param name="responseBody">Text to parse.</param>
/// <returns>New instance of the desired object type or null if a null string was passed in.</returns>
public static T ParseResponseBody<T>(
string responseBody) where T: class, new()
string responseBody) where T : class, new()
{
if (responseBody == null)
{
Expand Down Expand Up @@ -214,7 +212,7 @@ public static string GetResponseBody(
return reader.ReadToEnd();
}
}

/// <summary>
/// Assert that a response body is parsable as the desired type.
/// </summary>
Expand Down Expand Up @@ -301,7 +299,7 @@ public static T GetResponse<T>(
/// <returns>A new S3 client.</returns>
public static AmazonS3Client CreateS3Client(
string uploadTarget,
string accessKeyId = "foo",
string accessKeyId = "foo",
string secretAccessKey = "bar")
{
if (uploadTarget == null)
Expand Down Expand Up @@ -359,7 +357,7 @@ public static InitiateMultipartUploadResponse OpenUpload(
// AWS SDK for .NET 4.5 default content-length is 0.
// Set this to zero so it's always valid.
//

initiateRequest.Headers.ContentLength = 0;
return s3Client.InitiateMultipartUpload(initiateRequest);
}
Expand All @@ -375,11 +373,13 @@ public static InitiateMultipartUploadResponse OpenUpload(
/// <returns>List of upload part responses from the server.</returns>
public static List<UploadPartResponse> UploadParts(
AmazonS3Client s3Client,
string uploadTarget,
string uploadTarget,
string fileName,
string uploadId,
long partSize)
{


if (s3Client == null || uploadTarget == null || fileName == null || uploadId == null || partSize <= 0)
throw new InvalidDataException();

Expand All @@ -390,21 +390,35 @@ public static List<UploadPartResponse> UploadParts(
long filePosition = 0;
for (int i = 1; filePosition < fileSize; i++)
{
UploadPartRequest uploadRequest = new UploadPartRequest
try
{
UploadPartRequest uploadRequest = new UploadPartRequest
{
BucketName = Common.UploadBucketName,
Key = fileKey,
UploadId = uploadId,
PartNumber = i,
PartSize = partSize,
FilePosition = filePosition,
FilePath = fileName
};

// add the response to the list since it will be needed to complete the upload
uploadResponses.Add(s3Client.UploadPart(uploadRequest));

filePosition += partSize;
if (WatchFolderService.verbose)
{
WatchFolderService.eventLog.WriteEntry($"Part number {i} has uploaded successfully for {fileName}. {filePosition} of {fileSize} uploaded", EventLogEntryType.Information);
}
}
catch (Exception ex)
{
BucketName = Common.UploadBucketName,
Key = fileKey,
UploadId = uploadId,
PartNumber = i,
PartSize = partSize,
FilePosition = filePosition,
FilePath = fileName
};

// add the response to the list since it will be needed to complete the upload
uploadResponses.Add(s3Client.UploadPart(uploadRequest));

filePosition += partSize;
if (WatchFolderService.verbose)
{
WatchFolderService.eventLog.WriteEntry($"Part number {i} failed to upload for {fileName}. {filePosition} of {fileSize} uploaded. {ex}", EventLogEntryType.Warning);
}
}
}

return uploadResponses;
Expand Down Expand Up @@ -435,7 +449,7 @@ public static CompleteMultipartUploadResponse CloseUpload(
BucketName = Common.UploadBucketName,
Key = fileKey,
UploadId = uploadId,

};

completeRequest.AddPartETags(partResponses);
Expand Down Expand Up @@ -489,10 +503,10 @@ private static string GetServiceUrlFromUploadTarget(
int i = uploadTarget.IndexOf(Common.UploadTargetPathFragment_Panopto);

if (i < 0)
throw new InvalidDataException();
throw new InvalidDataException();

string result = uploadTarget.Substring(
0,
0,
i + Common.UploadTargetPathFragment_Panopto.Length);

return result;
Expand Down Expand Up @@ -543,7 +557,7 @@ private static string GetFileKey(string uploadTarget, string fileName)
throw new InvalidDataException();

return Path.Combine(
GetFileKeyPrefixFromUploadTarget(uploadTarget),
GetFileKeyPrefixFromUploadTarget(uploadTarget),
Path.GetFileName(fileName));
}

Expand All @@ -559,7 +573,7 @@ private static string GetFileKey(string uploadTarget, string fileName)
public static T CreateRestObject<T>(
string authCookie,
string noun,
T value) where T : BaseObject, new()
T value) where T : BaseObject, new()
{
HttpWebRequest request = Common.CreateRequest(
"POST",
Expand Down
26 changes: 15 additions & 11 deletions WatchFolderService/WatchFolderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public partial class WatchFolderService : ServiceBase
private bool inputValid = true;
private string inputFailureMessage = "";
private int maxNumberOfAttempts = 3;
private bool verbose = false;
public static bool verbose = false;
public static EventLog eventLog = new EventLog();


// This name must be something different from "PanoptoWatchFolderService".
// That name was once used associated with custom log and the association seems
Expand All @@ -48,16 +50,18 @@ public WatchFolderService()
InitializeComponent();

// Setup event log
this.AutoLog = true;
((ISupportInitialize)this.EventLog).BeginInit();
AutoLog = true;

((ISupportInitialize)EventLog).BeginInit();
if (!EventLog.SourceExists(WatchFolderService.EventLogSourceName))
{
EventLog.CreateEventSource(WatchFolderService.EventLogSourceName, "Application");
}
((ISupportInitialize)this.EventLog).EndInit();
((ISupportInitialize)EventLog).EndInit();

eventLog.Source = WatchFolderService.EventLogSourceName;
eventLog.Log = "Application";

this.EventLog.Source = WatchFolderService.EventLogSourceName;
this.EventLog.Log = "Application";

// Parse config file
server = ConfigurationManager.AppSettings["Server"];
Expand Down Expand Up @@ -484,7 +488,7 @@ private Dictionary<string, SyncInfo> GetLastSyncInfo()

folderInfo.Add(info[0], syncInfo);
}

return folderInfo;
}

Expand All @@ -500,7 +504,7 @@ private void SetSyncInfo(Dictionary<string, SyncInfo> info)
{
string line =
fileName + ";"
+ info[fileName].LastSyncWriteTime.ToString(DATETIME_FORMAT, CultureInfo.InvariantCulture) + ";"
+ info[fileName].LastSyncWriteTime.ToString(DATETIME_FORMAT, CultureInfo.InvariantCulture) + ";"
+ info[fileName].NewSyncWriteTime.ToString(DATETIME_FORMAT, CultureInfo.InvariantCulture) + ";"
+ info[fileName].FileStableTime + ";"
+ info[fileName].NumberOfAttempts;
Expand All @@ -509,7 +513,7 @@ private void SetSyncInfo(Dictionary<string, SyncInfo> info)
{
this.EventLog.WriteEntry("Writing to InfoFile: " + line, EventLogEntryType.Information);
}

infoFile.WriteLine(line);
}
}
Expand All @@ -522,7 +526,7 @@ private void SetSyncInfo(Dictionary<string, SyncInfo> info)
/// <returns>DateTime created from timeString</returns>
private DateTime GetDateTime(string timeString)
{
return DateTime.ParseExact(timeString,DATETIME_FORMAT,CultureInfo.InvariantCulture);
return DateTime.ParseExact(timeString, DATETIME_FORMAT, CultureInfo.InvariantCulture);
}

/// <summary>
Expand All @@ -548,7 +552,7 @@ private FileInfo[] GetFileInfo(DirectoryInfo dirInfo)
}

FileInfo[] result = new FileInfo[resultArray.Count];
int i = 0;
int i = 0;
foreach (FileInfo fileInfo in resultArray)
{
result[i] = fileInfo;
Expand Down
6 changes: 3 additions & 3 deletions WatchFolderService/WatchFolderService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="AWSSDK.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604, processorArchitecture=MSIL">
<HintPath>..\packages\AWSSDK.Core.3.5.1.2\lib\net45\AWSSDK.Core.dll</HintPath>
<HintPath>..\packages\AWSSDK.Core.3.7.104.5\lib\net45\AWSSDK.Core.dll</HintPath>
</Reference>
<Reference Include="AWSSDK.S3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604, processorArchitecture=MSIL">
<HintPath>..\packages\AWSSDK.S3.3.5.0.4\lib\net45\AWSSDK.S3.dll</HintPath>
<HintPath>..\packages\AWSSDK.S3.3.7.0\lib\net45\AWSSDK.S3.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
Expand Down Expand Up @@ -121,7 +121,7 @@
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<Analyzer Include="..\packages\AWSSDK.S3.3.5.0.4\analyzers\dotnet\cs\AWSSDK.S3.CodeAnalysis.dll" />
<Analyzer Include="..\packages\AWSSDK.S3.3.7.0\analyzers\dotnet\cs\AWSSDK.S3.CodeAnalysis.dll" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
4 changes: 2 additions & 2 deletions WatchFolderService/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AWSSDK.Core" version="3.5.1.2" targetFramework="net472" />
<package id="AWSSDK.S3" version="3.5.0.4" targetFramework="net472" />
<package id="AWSSDK.Core" version="3.7.104.5" targetFramework="net472" />
<package id="AWSSDK.S3" version="3.7.0" targetFramework="net472" />
</packages>