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
Binary file added Content/icons/web_services/ms-word-icon-64x64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions Fr8Infrastructure.NET/Data/Control/Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,9 @@ public TextBlock()

public class FilePicker : ControlDefinitionDTO
{
[JsonProperty("fileextensions")]
public string FileExtensions { get; set; }

public FilePicker()
{
Type = ControlTypes.FilePicker;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,20 @@ public async Task Terminal_Google_Discover()

Assert.IsNotNull(googleTerminalDiscoveryResponse, "Terminal Google discovery did not happen.");
Assert.IsNotNull(googleTerminalDiscoveryResponse.Activities, "Google terminal does not have actions.");
Assert.AreEqual(4, googleTerminalDiscoveryResponse.Activities.Count, "Google terminal expected 4 actions.");
Assert.AreEqual(5, googleTerminalDiscoveryResponse.Activities.Count, "Google terminal expected 5 actions.");
Assert.AreEqual("terminalGoogle", googleTerminalDiscoveryResponse.Definition.Name);
Assert.AreEqual("Google", googleTerminalDiscoveryResponse.Definition.Label);
Assert.AreEqual(googleTerminalDiscoveryResponse.Activities.Any(a => a.Name == "Get_Google_Sheet_Data"), true, "Action Get_Google_Sheet_Data was not loaded");
Assert.AreEqual(googleTerminalDiscoveryResponse.Activities.Any(a => a.Name == "Monitor_Form_Responses"), true, "Action Monitor_Form_Responses was not loaded");
Assert.AreEqual(googleTerminalDiscoveryResponse.Activities.Any(a => a.Name == "Save_To_Google_Sheet"), true, "Action Save_To_Google_Sheet was not loaded");
Assert.AreEqual(googleTerminalDiscoveryResponse.Activities.Any(a => a.Name == "Monitor_Gmail_Inbox"), true, "Action Monitor_Gmail_Inbox was not loaded");
Assert.AreEqual(googleTerminalDiscoveryResponse.Activities.Any(a => a.Name == "Create_Google_Doc"), true, "Action Create_Google_Doc was not loaded");
//Check Activities Categories
Assert.True(googleTerminalDiscoveryResponse.Activities.Single(a => a.Name == "Get_Google_Sheet_Data").Categories.Any(x => x.Id == ActivityCategories.ReceiveId), "Activity Get_Google_Sheet_Data is not of Category Receivers");
Assert.True(googleTerminalDiscoveryResponse.Activities.Single(a => a.Name == "Monitor_Form_Responses").Categories.Any(x => x.Id == ActivityCategories.MonitorId), "Activity Monitor_Form_Responses is not of Category Monitors");
Assert.True(googleTerminalDiscoveryResponse.Activities.Single(a => a.Name == "Save_To_Google_Sheet").Categories.Any(x => x.Id == ActivityCategories.ForwardId), "Activity Save_To_Google_Sheet is not of Category Forwarders");
Assert.True(googleTerminalDiscoveryResponse.Activities.Single(a => a.Name == "Monitor_Gmail_Inbox").Categories.Any(x => x.Id == ActivityCategories.MonitorId), "Activity Monitor_Gmail_Inbox is not of Category Monitors");
Assert.True(googleTerminalDiscoveryResponse.Activities.Single(a => a.Name == "Create_Google_Doc").Categories.Any(x => x.Id == ActivityCategories.ForwardId), "Activity Create_Google_Doc is not of Category Forwarders");
}
}
}
2 changes: 1 addition & 1 deletion Views/AngularTemplate/FilePicker.cshtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="filepicker"> <!--File upload control main container-->
<span class='btn btn-upload' ngf-select="OnFileSelect($file)" name='pca__chk__{{field.name}}' id='pca__chk__{{field.name}}'
accept='.xls,.xlsx' ngf-max-size='20MB' ngf-min-height='100'>Select File</span>
accept="{{field.fileextensions}}" ngf-max-size='20MB' ngf-min-height='100'>Select File</span>
<span class="file-info">{{field.value == null ? 'No file selected' : field.value|formatInput}}</span>
</div>
1 change: 1 addition & 0 deletions terminalExcel/Activities/Load_Excel_File_v1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public ActivityUi()
{
FilePicker = new FilePicker
{
FileExtensions = ".xls,.xlsx",
Label = "Select an Excel file",
Name = nameof(FilePicker),
Required = true,
Expand Down
254 changes: 254 additions & 0 deletions terminalGoogle/Activities/Create_Google_Doc_v1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Fr8.Infrastructure.Data.Control;
using Fr8.Infrastructure.Data.Crates;
using Fr8.Infrastructure.Data.DataTransferObjects;
using Fr8.Infrastructure.Data.Managers;
using Fr8.Infrastructure.Data.Manifests;
using Fr8.Infrastructure.Data.Manifests.Helpers;
using Fr8.Infrastructure.Data.States;
using Fr8.TerminalBase.Errors;
using Fr8.TerminalBase.Infrastructure;
using Google.GData.Client;
using Newtonsoft.Json;
using terminalGoogle.Actions;
using terminalGoogle.DataTransferObjects;
using terminalGoogle.Interfaces;
using Fr8.TerminalBase.Services;
using System.Globalization;
using System.Text;
using Fr8.Infrastructure.Data.Constants;
using System.IO;

namespace terminalGoogle.Activities
{
public class Create_Google_Doc_v1 : BaseGoogleTerminalActivity<Create_Google_Doc_v1.ActivityUi>
{
public static ActivityTemplateDTO ActivityTemplateDTO = new ActivityTemplateDTO
{
Id = new Guid("DAFD6823-CF54-4DD4-A618-D89A967EB885"),
Name = "Create_Google_Doc",
Label = "Create Google Doc",
Version = "1",
Terminal = TerminalData.TerminalDTO,
NeedsAuthentication = true,
MinPaneWidth = 300,
Categories = new[]
{
ActivityCategories.Forward,
TerminalData.GooogleActivityCategoryDTO
}
};
protected override ActivityTemplateDTO MyTemplate => ActivityTemplateDTO;

public class ActivityUi : StandardConfigurationControlsCM
{
public CrateChooser UpstreamCrateChooser { get; set; }

public DropDownList ExistingFilesList { get; set; }

public RadioButtonOption UseIncomingDataOption { get; set; }

public RadioButtonOption UseStoredFileOption { get; set; }

public RadioButtonGroup ContentSelectionGroup { get; set; }

public TextSource NewFileName { get; set; }

public ActivityUi(UiBuilder uiBuilder)
{
NewFileName = uiBuilder.CreateSpecificOrUpstreamValueChooser("Title", nameof(NewFileName), addRequestConfigEvent: true, requestUpstream: true, availability: AvailabilityType.RunTime);
NewFileName.IsCollapsed = false;
Controls.Add(NewFileName);

UpstreamCrateChooser = new CrateChooser
{
Label = "Crate to store",
Name = nameof(UpstreamCrateChooser),
Required = true,
RequestUpstream = true,
SingleManifestOnly = true,
};

ExistingFilesList = new DropDownList
{
Name = nameof(ExistingFilesList),
Events = new List<ControlEvent> { ControlEvent.RequestConfig }
};

UseIncomingDataOption = new RadioButtonOption
{
Selected = true,
Name = nameof(UseIncomingDataOption),
Value = "Use Incoming Data",
Controls = new List<ControlDefinitionDTO> { UpstreamCrateChooser }
};

UseStoredFileOption = new RadioButtonOption()
{
Selected = false,
Name = nameof(UseStoredFileOption),
Value = "Use Excisting File",
Controls = new List<ControlDefinitionDTO> { ExistingFilesList }
};

ContentSelectionGroup = new RadioButtonGroup
{
GroupName = nameof(ContentSelectionGroup),
Name = nameof(ContentSelectionGroup),
Events = new List<ControlEvent> { ControlEvent.RequestConfig },
Radios = new List<RadioButtonOption>
{
UseIncomingDataOption,
UseStoredFileOption
}
};

Controls.Add(ContentSelectionGroup);
}
}

private const string SelectedDocCrateLabel = "Selected Doc";

private readonly IGoogleDrive _googleDrive;

public Create_Google_Doc_v1(ICrateManager crateManager, IGoogleIntegration googleIntegration, IGoogleDrive googleDrive)
: base(crateManager, googleIntegration)
{
_googleDrive = googleDrive;
}

private GoogleAuthDTO GetGoogleAuthToken()
{
return JsonConvert.DeserializeObject<GoogleAuthDTO>(AuthorizationToken.Token);
}

public override async Task Initialize()
{
ActivityUI.ExistingFilesList.ListItems = await GetCurrentUsersFiles(".doc",".docx");
}

public override async Task FollowUp()
{
}

protected override Task Validate()
{
ValidationManager.ValidateTextSourceNotEmpty(ActivityUI.NewFileName, "Title for Document must be specified");

if(ActivityUI.UseIncomingDataOption.Selected == true)
{
ValidationManager.ValidateCrateChooserNotEmpty(ActivityUI.UpstreamCrateChooser, "File must be specified");
}

if(ActivityUI.UseStoredFileOption.Selected == true)
{
ValidationManager.ValidateDropDownListNotEmpty(ActivityUI.ExistingFilesList, "File must be specified");
}

return Task.FromResult(0);
}

public override async Task Run()
{
byte[] body = null;
string fileName = null;

if(ActivityUI.UseIncomingDataOption.Selected == true)
{
var crateToProcess = FindCrateToProcess();
if (crateToProcess == null)
{
throw new ActivityExecutionException($"Failed to run {ActivityPayload.Name} because specified upstream crate was not found in payload");
}

var content = crateToProcess.Get<StandardFileDescriptionCM>();
body = Convert.FromBase64String(content.TextRepresentation);
fileName = content.Filename;
}
else if(ActivityUI.UseStoredFileOption.Selected == true)
{
var fileSelector = ActivityUI.ExistingFilesList;
if (string.IsNullOrEmpty(fileSelector.Value))
{
RaiseError("No File was selected on design time", ActivityErrorCode.DESIGN_TIME_DATA_MISSING);
return;
}
//let's download this file
var file = await HubCommunicator.DownloadFile(int.Parse(fileSelector.Value));
if (file == null || file.Length < 1)
{
RaiseError("Unable to download file from Hub");
return;
}
fileName = fileSelector.selectedKey;

using (var reader = new MemoryStream())
{
file.CopyTo(reader);
body = reader.ToArray();
}
}

try
{
var file = await _googleDrive.CreateFile(ActivityUI.NewFileName.TextValue,
body, GetMimeType(fileName), GetGoogleAuthToken());

}
catch (GDataRequestException ex)
{
if (ex.InnerException.Message.IndexOf("(401) Unauthorized") > -1)
{
throw new AuthorizationTokenExpiredOrInvalidException();
}

throw;
}
}

private static string GetMimeType(string fileName)
{
string mimeType = "application/unknown";
string ext = System.IO.Path.GetExtension(fileName).ToLower();
Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if (regKey != null && regKey.GetValue("Content Type") != null)
mimeType = regKey.GetValue("Content Type").ToString();
return mimeType;
}

private async Task<List<ListItem>> GetCurrentUsersFiles(params string[] extensions)
{
var curAccountFileList = await HubCommunicator.GetFiles();
if(extensions != null)
{
curAccountFileList = curAccountFileList.Where(f =>
{
bool result = true;
foreach (var ext in extensions)
{
if (f.OriginalFileName.EndsWith(ext, StringComparison.InvariantCultureIgnoreCase))
{
result = true;
}
else
{
result = false;
}
}
return result;
});
}

return curAccountFileList.Select(c => new ListItem() { Key = c.OriginalFileName, Value = c.Id.ToString(CultureInfo.InvariantCulture) }).ToList();
}

private Crate FindCrateToProcess()
{
var desiredCrateDescription = ActivityUI.UpstreamCrateChooser.CrateDescriptions.Single(x => x.Selected);
return Payload.FirstOrDefault(x => x.Label == desiredCrateDescription.Label && x.ManifestType.Type == desiredCrateDescription.ManifestType);
}
}
}
4 changes: 3 additions & 1 deletion terminalGoogle/Interfaces/IGoogleDrive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ public interface IGoogleDrive
Task<DriveService> CreateDriveService(GoogleAuthDTO authDTO);
bool FileExist(DriveService driveService, string filename, out string link);
Task<Dictionary<string, string>> GetGoogleForms(GoogleAuthDTO authDTO);
}
Task<Google.Apis.Drive.v2.Data.File> CreateFile(string title, byte[] body, string mimeType, GoogleAuthDTO authDTO);

}
}
14 changes: 14 additions & 0 deletions terminalGoogle/Services/GoogleDrive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ public async Task<string> DownloadFile(string fileId, GoogleAuthDTO authDTO)
return fileContent;
}

public async Task<Google.Apis.Drive.v2.Data.File> CreateFile(string title, byte[] body, string mimeType, GoogleAuthDTO authDTO)
{
var driveService = await CreateDriveService(authDTO);
var doc = new Google.Apis.Drive.v2.Data.File();
doc.Title = title;
doc.MimeType = mimeType;
var stream = new System.IO.MemoryStream(body);

var request = driveService.Files.Insert(doc, stream, mimeType);
await request.UploadAsync();
return request.ResponseBody;
}


public async Task<Dictionary<string, string>> GetGoogleForms(GoogleAuthDTO authDTO)
{
var driveService = await CreateDriveService(authDTO);
Expand Down
1 change: 1 addition & 0 deletions terminalGoogle/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ protected override void RegisterActivities()
ActivityStore.RegisterActivity<Monitor_Form_Responses_v1>(Monitor_Form_Responses_v1.ActivityTemplateDTO);
ActivityStore.RegisterActivity<Save_To_Google_Sheet_v1>(Save_To_Google_Sheet_v1.ActivityTemplateDTO);
ActivityStore.RegisterActivity<Monitor_Gmail_Inbox_v1>(Monitor_Gmail_Inbox_v1.ActivityTemplateDTO);
ActivityStore.RegisterActivity<Create_Google_Doc_v1>(Create_Google_Doc_v1.ActivityTemplateDTO);
}
}
}
1 change: 1 addition & 0 deletions terminalGoogle/terminalGoogle.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Activities\BaseGoogleTerminalActivity.cs" />
<Compile Include="Activities\Create_Google_Doc_v1.cs" />
<Compile Include="Activities\Get_Google_Sheet_Data_v1.cs" />
<Compile Include="Activities\Monitor_Form_Responses_v1.cs" />
<Compile Include="Activities\Monitor_Gmail_Inbox_v1.cs" />
Expand Down
16 changes: 6 additions & 10 deletions terminalUtilities/Excel/ExcelUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@
using OfficeOpenXml;
using StructureMap;
using RestSharp.Extensions;
using terminalUtilities.Files;

namespace terminalUtilities.Excel
{
public class ExcelUtils
{
private readonly IRestfulServiceClient _restfulServiceClient;
private readonly ICrateManager _crateManager;
private readonly FileUtils _fileUtils;

public ExcelUtils(IRestfulServiceClient restfulServiceClient, ICrateManager crateManager)
public ExcelUtils(IRestfulServiceClient restfulServiceClient, ICrateManager crateManager, FileUtils fileUtils)
{
_restfulServiceClient = restfulServiceClient;
_crateManager = crateManager;
_fileUtils = fileUtils;
}

public static void ConvertToCsv(string pathToExcel, string pathToCsv)
Expand Down Expand Up @@ -263,9 +266,7 @@ public static Dictionary<string, List<Tuple<string, string>>> GetTabularData(byt

public async Task<byte[]> GetExcelFileAsByteArray(string selectedFilePath)
{
var fileAsByteArray = await RetrieveFile(selectedFilePath);
fileAsByteArray.Position = 0;
return fileAsByteArray.ReadAsBytes();
return await _fileUtils.GetFileAsByteArray(selectedFilePath, ".xls", ".xlsx");
}

public async Task<StandardTableDataCM> GetExcelFile(string selectedFilePath, bool isFirstRowAsColumnNames = true)
Expand Down Expand Up @@ -334,12 +335,7 @@ public StandardTableDataCM GetExcelFile(byte[] fileAsByteArray, string selectedF

private async Task<Stream> RetrieveFile(string filePath)
{
var ext = Path.GetExtension(filePath);
if (ext != ".xls" && ext != ".xlsx")
{
throw new ArgumentException("Expected '.xls' or '.xlsx'", "selectedFile");
}
return await _restfulServiceClient.DownloadAsync(new Uri(filePath));
return await _fileUtils.RetrieveFile(filePath, ".xls", ".xlsx");
}

public static List<TableRowDTO> CreateTableCellPayloadObjects(Dictionary<string, List<Tuple<string, string>>> rowsDictionary, string[] headersArray = null, bool includeHeadersAsFirstRow = false)
Expand Down
Loading