Skip to content
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
2 changes: 1 addition & 1 deletion APIClient.IntegrationTests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ public void CreateDefectWithAttachment()
newDefect.SetAttributeValue(nameAttribute, name);
services.Save(newDefect);

services.SaveAttachment(file, newDefect, "Test Attachment on " + newDefect.Oid);
services.SaveAttachment(file, newDefect, "Test Attachment on " + newDefect.Oid, true);

var query = new Query(newDefect.Oid.Momentless);
query.Selection.Add(attachmentsAttribute);
Expand Down
10 changes: 10 additions & 0 deletions APIClient/Services/IServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ public interface IServices
/// <returns>Oid</returns>
Oid SaveAttachment(string filePath, Asset asset, string attachmentName);

/// <summary>
/// Saves an attachment to the specified asset.
/// </summary>
/// <param name="filePath">Path and name of the attachment file.</param>
/// <param name="asset">Asset to save the attachment to.</param>
/// <param name="attachmentName">The name of the attachment.</param>
/// <param name="useFileNameAsFileName">Sets file name as the file name rather than full path.</param>
/// <returns>Oid</returns>
Oid SaveAttachment(string filePath, Asset asset, string attachmentName, bool useFileNameAsFileName);

/// <summary>
/// Saves an embedded image to the specified asset.
/// </summary>
Expand Down
14 changes: 13 additions & 1 deletion APIClient/Services/Services.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,24 @@ public string ExecutePassThroughQuery(string query)
}

public Oid SaveAttachment(string filePath, Asset asset, string attachmentName)
{
return SaveAttachment(filePath, asset, attachmentName, false);
}

public Oid SaveAttachment(string filePath, Asset asset, string attachmentName, bool useFileNameAsFileName)
{
if (string.IsNullOrWhiteSpace(filePath))
throw new ArgumentNullException(nameof(filePath));
if (!File.Exists(filePath))
throw new APIException($"File \"{filePath}\" does not exist.");

string fileName;

if (useFileNameAsFileName)
fileName = Path.GetFileName(filePath);
else
fileName = filePath;

var mimeType = MimeType.Resolve(filePath);
var attachmentType = Meta.GetAssetType("Attachment");
var attachmentAssetDef = attachmentType.GetAttributeDefinition("Asset");
Expand All @@ -377,7 +389,7 @@ public Oid SaveAttachment(string filePath, Asset asset, string attachmentName)
var attachmentNameAttr = attachmentType.GetAttributeDefinition("Name");
var attachment = New(attachmentType, Oid.Null);
attachment.SetAttributeValue(attachmentNameAttr, attachmentName);
attachment.SetAttributeValue(attachmentFileName, filePath);
attachment.SetAttributeValue(attachmentFileName, fileName);
attachment.SetAttributeValue(attachmentContentType, mimeType);
attachment.SetAttributeValue(attachmentContent, string.Empty);
attachment.SetAttributeValue(attachmentAssetDef, asset.Oid);
Expand Down