/// <summary> /// Update the currently selected item by appending new text. /// </summary> /// <param name="_selectedFileObject">The file selected in the ListBox.</param> /// <param name="fileText">The updated text contents of the file.</param> /// <returns>A Boolean value that indicates whether the text file was successfully updated.</returns> internal async Task <bool> UpdateTextFileAsync(string selectedItemID, string fileText) { Microsoft.Office365.SharePoint.FileServices.File file; byte[] byteArray; bool isSuccess = false; try { // Make sure we have a reference to the SharePoint client var spClient = await AuthenticationHelper.EnsureSharePointClientCreatedAsync("MyFiles"); // Get a handle on the selected item. IItemFetcher thisItemFetcher = spClient.Files.GetById(selectedItemID); IFileFetcher thisFileFetcher = thisItemFetcher.ToFile(); var myFile = await thisFileFetcher.ExecuteAsync(); file = myFile as Microsoft.Office365.SharePoint.FileServices.File; string updateTime = "\n\r\n\rLast update at " + DateTime.Now.ToLocalTime().ToString(); byteArray = Encoding.UTF8.GetBytes(fileText + updateTime); using (MemoryStream stream = new MemoryStream(byteArray)) { // Update the file. This results in a call to the service. await file.UploadAsync(stream); isSuccess = true; // We've updated the file. } } catch (ArgumentException) { isSuccess = false; } return(isSuccess); }
public ViewBuilder(IFileFetcher fileFetcher) { if (fileFetcher == null) { _log.Error("No fileFetcher given to ParsedView."); throw new ArgumentNullException("fileFetcher"); } _fileFetcher = fileFetcher; _propertyBuilderTable = new Dictionary<string, IPropertyBuilder<IProperty>>(); _valueBuilderTable = new Dictionary<string, IFunctionValueBuilder<IFunctionValue>>(); _atRuleBuilders = new Dictionary<string, IAtRuleBuilder<IAtRule>>(); AddPropertyBuilder("apply-pattern",new ApplyPatternPropertyBuilder()); AddPropertyBuilder("text", new TextPropertyBuilder()); AddPropertyBuilder("format-text", new FormatTextPropertyBuilder()); AddPropertyBuilder("attr", new AttributePropertyBuilder()); AddPropertyBuilder("html", new HtmlPropertyBuilder()); AddValueBuilder("if", new IfFunctionValueBuilder()); AddValueBuilder("concat", new ConcatFunctionValueBuilder()); AddValueBuilder("true", new TrueFunctionValueBuilder()); AddValueBuilder("false", new FalseFunctionValueBuilder()); AddValueBuilder("model", new ModelFunctionValueBuilder()); AddAtRuleBuilder("@pattern", new PatternAtRuleBuilder()); AddAtRuleBuilder("@if", new IfAtRuleBuilder()); }
/// <summary> /// Deletes the selected item or folder from the ListBox. /// </summary> /// <returns>A Boolean value that indicates whether the file or folder was successfully deleted.</returns> internal async Task <bool?> DeleteFileOrFolderAsync(string selectedItemID) { bool?isSuccess = false; try { // Make sure we have a reference to the SharePoint client var spClient = await AuthenticationHelper.EnsureSharePointClientCreatedAsync("MyFiles"); // Get the file to be removed from the SharePoint service. This results in a call to the service. IItemFetcher thisItemFetcher = spClient.Files.GetById(selectedItemID); IFileFetcher thisFileFetcher = thisItemFetcher.ToFile(); var thisFile = await thisFileFetcher.ExecuteAsync(); // Delete the file or folder. This results in a call to the service. await thisFile.DeleteAsync(); isSuccess = true; } catch (Microsoft.Data.OData.ODataErrorException) { isSuccess = null; } catch (NullReferenceException) { isSuccess = null; } return(isSuccess); }
private async Task DownloadFileAsync(IFileFetcher fetcher) { var file = await fetcher.GetLatestAsync().ConfigureAwait(false); string filePath = Path.Combine(_settings.ZippedToolsDirectory, file.Name); if (!File.Exists(filePath) || _settings.ReloadTools) { using (var fileStream = new FileStream(filePath, FileMode.Create)) { var contentStream = await file.GetContentAsync().ConfigureAwait(false); await contentStream.CopyToAsync(fileStream).ConfigureAwait(false); } } }
/// <summary> /// Reads the contents of a text file and displays the results in a TextBox. /// </summary> /// <param name="_selectedFileObject">The file selected in the ListBox.</param> /// <returns>A Boolean value that indicates whether the text file was successfully read.</returns> internal async Task <object[]> ReadTextFileAsync(string selectedItemID) { string fileContents = string.Empty; object[] results = new object[] { fileContents, false }; try { // Make sure we have a reference to the SharePoint client var spClient = await AuthenticationHelper.EnsureSharePointClientCreatedAsync("MyFiles"); // Get a handle on the selected item. IItemFetcher thisItemFetcher = spClient.Files.GetById(selectedItemID); IFileFetcher thisFileFetcher = thisItemFetcher.ToFile(); var myFile = await thisFileFetcher.ExecuteAsync(); // Check that the selected item is a .txt file. if (!myFile.Name.EndsWith(".txt") && !myFile.Name.EndsWith(".xml")) { results[0] = string.Empty; results[1] = false; return(results); } Microsoft.Office365.SharePoint.FileServices.File file = myFile as Microsoft.Office365.SharePoint.FileServices.File; // Download the text file and put the results into a string. This results in a call to the service. using (Stream stream = await file.DownloadAsync()) { using (StreamReader reader = new StreamReader(stream)) { results[0] = await reader.ReadToEndAsync(); results[1] = true; } } } catch (NullReferenceException) { results[1] = false; } catch (ArgumentException) { results[1] = false; } return(results); }
private async Task <ToolUpdate> CheckForUpdateAsync( ToolSettings toolSettings, IFileFetcher fetcher, bool useExistingTools) { var latestLocal = ToolUtility.GetLatestToolOrNull(_settings, toolSettings); if (useExistingTools && latestLocal != null) { return(null); } EnableSecurityProtocols(); var latestDownload = await fetcher.GetLatestAsync().ConfigureAwait(false); var fileExtension = ArchiveUtility.GetFileExtension(latestDownload.Format); var fileName = $"{toolSettings.Prefix}{latestDownload.Version}{fileExtension}"; var destinationPath = Path.Combine(_settings.ZippedToolsDirectory, fileName); ToolUpdateStatus status; if (latestLocal == null) { status = ToolUpdateStatus.NoLocalVersion; } else if (!File.Exists(destinationPath)) { status = ToolUpdateStatus.NewerVersionAvailable; } else { status = ToolUpdateStatus.NoUpdateAvailable; } return(new ToolUpdate( status, latestLocal?.Version, destinationPath, latestDownload)); }
public async Task <ActionResult> Download(string fileId, string lmsParams, string nexturl) { if (!Request.IsAuthenticated) { return(RedirectToAction("SignIn", "Account")); } _factory = new ServiceClientFactory(); var sharepointClient = await _factory.CreateSharepointServicesClientWithAsync("MyFiles"); IItemFetcher thisItemFetcher = sharepointClient.Files.GetById(fileId); IFileFetcher thisFileFetcher = thisItemFetcher.ToFile(); var myFile = await thisFileFetcher.ExecuteAsync(); var file = myFile as Microsoft.Office365.SharePoint.FileServices.File; var filename = HttpUtility.UrlEncode(file.Name.Replace(' ', '_')); var generatedFileName = $"{Server.MapPath("/_Tools/O365_OneDrive_Download")}\\{filename}"; if (Debugger.IsAttached) { generatedFileName = $"{Server.MapPath("~/")}Content\\downloads\\{filename}"; } using (Stream stream = await file.DownloadAsync()) { using (Stream saveFile = System.IO.File.Create(generatedFileName)) { CopyStream(stream, saveFile); } } var redirectUrl = $"/{Server.UrlDecode(lmsParams)}{filename}&nexturl={Server.UrlEncode(nexturl)}"; return(Redirect(redirectUrl)); }
internal async Task <bool> DownloadFileAsync(string selectedItemID) { var results = false; try { // Make sure we have a reference to the SharePoint client var spClient = await AuthenticationHelper.EnsureSharePointClientCreatedAsync("MyFiles"); // Get a handle on the selected item. IItemFetcher thisItemFetcher = spClient.Files.GetById(selectedItemID); IFileFetcher thisFileFetcher = thisItemFetcher.ToFile(); var myFile = await thisFileFetcher.ExecuteAsync(); Microsoft.Office365.SharePoint.FileServices.File file = myFile as Microsoft.Office365.SharePoint.FileServices.File; // Download the file and put the results into a string. This results in a call to the service. using (Stream stream = await file.DownloadAsync()) { // TODO: save file to users locker // Create a new file. If the file already exists, it will be overwritten. using (var fileStream = new FileStream(@"C:\hosting\WebApp1\Downloads\" + myFile.Name, FileMode.Create, FileAccess.Write)) { stream.CopyTo(fileStream); } } results = true; } catch (Exception exc) { throw new Exception("Error occured while downloading file: ", exc); } return(results); }
public Synchronizer(Context context, IFileFetcher fileFetcher) { this.context = context; this.fileFetcher = fileFetcher; }