Skip to content

Commit

Permalink
Merge pull request #63 from ilovepdf/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
teamcrombie authored Oct 2, 2024
2 parents b165641 + 618cec9 commit 19784ef
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 9 deletions.
9 changes: 8 additions & 1 deletion src/ILovePDF/Core/RequestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public UploadTaskResponse UploadFile(Uri serverUrl, Uri url, string taskId)
}
}

public UploadTaskResponse UploadFile(Uri serverUrl, FileInfo file, string taskId)
public UploadTaskResponse UploadFile(Uri serverUrl, FileInfo file, string taskId, BaseExtraUploadParams extraParams = null)
{
var link = GetUri($"{serverUrl}{Settings.V1}/upload");

Expand All @@ -244,6 +244,13 @@ public UploadTaskResponse UploadFile(Uri serverUrl, FileInfo file, string taskId
var uploadRequest = new BaseTaskRequest();
uploadRequest.FormData.Add("file", new FileParameter(fs, file.Name));
uploadRequest.FormData.Add("task", taskId);
if (extraParams != null)
{
foreach (var param in extraParams.GetValues())
{
uploadRequest.FormData.Add(param.Key, param.Value);
}
}

SetMultiPartFormData(uploadRequest.FormData, multipartFormData);

Expand Down
76 changes: 76 additions & 0 deletions src/ILovePDF/Core/UploadTaskResponse.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace iLovePdf.Core
Expand All @@ -13,5 +14,80 @@ public class UploadTaskResponse
/// </summary>
[JsonProperty("server_filename")]
public String ServerFileName { get; set; }
/// <summary>
/// pdf_pages
/// </summary>
[JsonProperty("pdf_pages")]
public String[] PdfPages { get; set; }
/// <summary>
/// pdf_page_number
/// </summary>
[JsonProperty("pdf_page_number")]
public String PdfPageNumber { get; set; }
/// <summary>
/// pdf_forms
/// </summary>
[JsonProperty("pdf_forms")]
public List<Dictionary<string, object>> PdfForms { get; set; }


public void GetPdfFormElement()
{

object x;
var pdfPageInfo = new Dictionary<string, int>();

if (PdfForms == null || PdfForms.Count == 0)
{
return;
}

foreach (var pdfFormElement in PdfForms)
{
pdfFormElement.TryGetValue("page", out x);
var pdfpageinfo = GetPdfPageInfo((int)x);
foreach (var page in pdfpageinfo)
{
pdfPageInfo.Add(page.Key, page.Value);
}
}
}

public Dictionary<string, int> GetPdfPageInfo(int pageNumber)
{
var pdfPages = GetSanitizedPdfPages();
if (pdfPages == null)
{
return null;
}

return pdfPages[pageNumber - 1];
}

public List<Dictionary<string, int>> GetSanitizedPdfPages()
{
var result = new List<Dictionary<string, int>>();

if (PdfPages == null)
{
return null;
}


foreach(var pdfPage in PdfPages)
{
var dimensions = pdfPage.Split('x');
int width = int.Parse(dimensions[0]);
int height = int.Parse(dimensions[1]);

result.Add(new Dictionary<string, int>
{
{ "width", width },
{ "height", height }
});
}

return result;
}
}
}
16 changes: 8 additions & 8 deletions src/ILovePDF/Model/Task/LovePdfTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public void SetServerTaskId(Uri serverUrl, String taskId)
/// <param name="path"></param>
/// <returns>Server file name</returns>
[SuppressMessage("Microsoft.Design", "CA1057:StringUriOverloadsCallSystemUriOverloads")]
public UploadTaskResponse AddFile(String path)
public UploadTaskResponse AddFile(String path, BaseExtraUploadParams extraParams = null)
{
return AddFile(path, TaskId);
return AddFile(path, TaskId, extraParams);
}

/// <summary>
Expand All @@ -64,9 +64,9 @@ public UploadTaskResponse AddFile(String path)
/// <param name="taskId">if no task provided will be used last one from create task method.</param>
/// <returns>Server file name</returns>
[SuppressMessage("Microsoft.Design", "CA1057:StringUriOverloadsCallSystemUriOverloads")]
public UploadTaskResponse AddFile(String path, String taskId)
public UploadTaskResponse AddFile(String path, String taskId, BaseExtraUploadParams extraParams = null)
{
return AddFile(path, taskId, String.Empty);
return AddFile(path, taskId, String.Empty, extraParams);
}

/// <summary>
Expand All @@ -77,9 +77,9 @@ public UploadTaskResponse AddFile(String path, String taskId)
/// <param name="password"></param>
/// <returns>Server file name</returns>
[SuppressMessage("Microsoft.Design", "CA1057:StringUriOverloadsCallSystemUriOverloads")]
public UploadTaskResponse AddFile(String path, String taskId, String password)
public UploadTaskResponse AddFile(String path, String taskId, String password, BaseExtraUploadParams extraParams = null)
{
return AddFile(path, taskId, password, Rotate.Degrees0);
return AddFile(path, taskId, password, Rotate.Degrees0, extraParams);
}

/// <summary>
Expand Down Expand Up @@ -109,12 +109,12 @@ internal void AddFiles(Dictionary<String, String> files)
/// <param name="rotate"></param>
/// <returns>Server file name</returns>
[SuppressMessage("Microsoft.Design", "CA1057:StringUriOverloadsCallSystemUriOverloads")]
public UploadTaskResponse AddFile(String path, String taskId, String password, Rotate rotate)
public UploadTaskResponse AddFile(String path, String taskId, String password, Rotate rotate, BaseExtraUploadParams extraParams = null)
{
var fileInfo = new FileInfo(path);
if (!fileInfo.Exists) throw new FileNotFoundException("File not found", fileInfo.FullName);

var response = RequestHelper.Instance.UploadFile(ServerUrl, fileInfo, taskId);
var response = RequestHelper.Instance.UploadFile(ServerUrl, fileInfo, taskId, extraParams);

Files.Add(new FileModel
{
Expand Down
22 changes: 22 additions & 0 deletions src/ILovePDF/Model/TaskParams/BaseExtraUploadParams.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace iLovePdf.Model.TaskParams
{
// This class is designed so that every programmer adapts it to the additional tools with specific needs and uses the SetValue method
public abstract class BaseExtraUploadParams
{
protected Dictionary<string, string> extraParams = new Dictionary<string, string>();

protected void SetValue(string key, string value)
{
extraParams[key] = value;
}

public Dictionary<string, string> GetValues()
{
return new Dictionary<string, string>(extraParams);
}
}
}
22 changes: 22 additions & 0 deletions src/ILovePDF/Model/TaskParams/SignExtraUploadParams.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace iLovePdf.Model.TaskParams
{
public class SignExtraUploadParams : BaseExtraUploadParams
{
public SignExtraUploadParams SetPdfInfo(bool activate = true)
{
extraParams["pdfinfo"] = activate ? "1" : "0";
return this;
}

public SignExtraUploadParams SetPdfForms(bool activate = true)
{
SetPdfInfo(true);
extraParams["pdfforms"] = activate ? "1" : "0";
return this;
}
}
}

0 comments on commit 19784ef

Please sign in to comment.