public FlushAsync ( CancellationToken cancellationToken ) : Task | ||
cancellationToken | CancellationToken | |
return | Task |
public async Task SaveFile(string name, Stream stream) { var fullPath = GetFullPath(name); using (var fs = new FileStream(fullPath, FileMode.OpenOrCreate, FileAccess.Write)) { await stream.CopyToAsync(fs).ConfigureAwait(false); await stream.FlushAsync().ConfigureAwait(false); await fs.FlushAsync().ConfigureAwait(false); } }
public async Task <IActionResult> OnPostUploadAsync(IFormFile file) { if (file is null) { return(StatusCode(400)); } User user = userManager.GetUser(HttpContext, database); if (user is null) { return(Unauthorized()); } System.IO.FileStream stream = await fileManager.CreateFile(user, (int)file.Length, file.FileName, "/Web", out string identifier); await file.CopyToAsync(stream); await stream.FlushAsync(); stream.Close(); string response = identifier + ";" + Path.GetFileName(file.FileName).Replace(";", ""); return(Content(response)); }
public Task FlushAsync() { if (YetaWFManager.IsSync()) { Stream.Flush(); return(Task.CompletedTask); } else { return(Stream.FlushAsync()); } }
public async Task DownloadAsync(string url, IDictionary<string, string> headers, string method, string saveAs) { // Download the data var data = await this.DownloadAsync(url, headers, method); // Write the file using (var memStream = new MemoryStream(data)) { using (var stream = new FileStream(saveAs, FileMode.CreateNew, FileAccess.Write, FileShare.None, (int)1024, true)) { await memStream.CopyToAsync(stream); await stream.FlushAsync(); } } }
public override async Task FlushAsync() { await this.writeLock.WaitAsync(); try { await fs.FlushAsync(); } catch { this.ReOpen(); } finally { this.writeLock.Release(); } }
static public int FlushAsync(IntPtr l) { try { System.IO.FileStream self = (System.IO.FileStream)checkSelf(l); System.Threading.CancellationToken a1; checkValueType(l, 2, out a1); var ret = self.FlushAsync(a1); pushValue(l, true); pushValue(l, ret); return(2); } catch (Exception e) { return(error(l, e)); } }
public async Task <ActionResult> FileUpload() { string filePath = System.IO.Path.Combine(_hostingEnvironment.WebRootPath, _filePath); if (!System.IO.Directory.Exists(filePath)) { System.IO.Directory.CreateDirectory(filePath); } var file = Request.Form.Files.FirstOrDefault(); if (file == null) { throw new Abp.UI.UserFriendlyException(417, "没有图片"); } string fileType = file.FileName.Split(".").LastOrDefault(); string[] fileTypeArr = new string[] { "jpg", "gif", "png", "jpeg" }; if (string.IsNullOrEmpty(fileType) || !fileTypeArr.Contains(fileType.ToLower())) { throw new Abp.UI.UserFriendlyException(415, "非图片文件"); } string fileName = DateTime.Now.ToString("yyyyMMddHHmm_") + file.FileName; string fullFilePath = System.IO.Path.Combine(filePath, fileName); using (System.IO.FileStream fs = System.IO.File.Create(fullFilePath)) { await file.CopyToAsync(fs); await fs.FlushAsync(); } var savedFile = new System.IO.FileInfo(fullFilePath); var optimizer = new ImageOptimizer(); optimizer.Compress(savedFile); savedFile.Refresh(); return(Json(new { StatusCode = 200, Info = "图片上传成功!", Data = new { url = string.Format("{0}{1}/{2}{3}", "Http://", Request.Host, _filePath, fileName), name = fileName, } })); }
/// <summary> /// An async FileLog constructor. /// </summary> /// <param name="path">The path to the log file.</param> /// <param name="useAsync">Instruct the underlying IO subsystem to optimize for async I/O.</param> /// <returns>An <see cref="FileLog"/> instance.</returns> public static async Task<FileLog> Create(string path, bool useAsync) { if (path == null) throw new ArgumentNullException("path"); path = string.Intern(Path.GetFullPath(path)); var writer = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read, 4096, useAsync); var buf = new byte[LHDR_SIZE]; // check the file's version number if file exists, else write it out and initialize the log long next; Version vers; if (writer.Length < LHDR_SIZE) { next = LHDR_SIZE; vers = VERSION; buf.Write(vers.Major, LHDR_MAJOR); buf.Write(vers.Minor, LHDR_MINOR); buf.Write(vers.Revision, LHDR_REV); buf.Write(next, LHDR_TX); await writer.WriteAsync(buf, 0, LHDR_SIZE); await writer.FlushAsync(); } else { await writer.ReadAsync(buf, 0, LHDR_SIZE); var major = buf.ReadInt32(LHDR_MAJOR); var minor = buf.ReadInt32(LHDR_MINOR); var rev = buf.ReadInt32(LHDR_REV); vers = new Version(major, minor, 0, rev); if (vers != VERSION) { writer.Dispose(); throw new NotSupportedException(string.Format("File log expects version {0} but found version {1}", VERSION, vers)); } next = buf.ReadInt32(LHDR_TX); } return new FileLog(path, writer, next); }
public async Task WriteFromStream(Stream sourceStream, FileInfo[] targets) { string targetPath = ResolveFileFullPath(targets.First()); // take first - path is based only on hash // write to files using (var fileStream = new FileStream(targetPath, FileMode.Create)) using (var gzip = new GZipStream(fileStream, CompressionMode.Compress)) { await sourceStream.CopyToAsync(gzip); await fileStream.FlushAsync(); } // update index await UpdateFilesList(filesToAddOrUpdate: targets); }
public void ProcessRequest(HttpContext context) { try { context.Response.ContentType = "image/jpeg"; int iconid = Convert.ToInt32(context.Request.QueryString["Id"]); if (File.Exists(@".\Data\Icons\" + iconid.ToString() + ".jpg")) { using (MemoryStream ms = new MemoryStream()) { File.Open(HttpContext.Current.Server.MapPath("~") + @"\Data\Icons\" + iconid.ToString() + ".jpg", FileMode.Open).CopyTo(ms); context.Response.BinaryWrite(ms.ToArray()); } } else { int id = Convert.ToInt32(iconid); RZService oSVC = new RZService(); var oSW = oSVC.GetSWDetail(id); if (oSW.Image != null) { byte[] image = oSW.Image; MemoryStream ms = new MemoryStream(image); try { var sIcon = new System.IO.FileStream(HttpContext.Current.Server.MapPath("~") + @"\Data\Icons\" + iconid.ToString() + ".jpg", FileMode.Create); ms.CopyTo(sIcon); sIcon.FlushAsync(); } catch { } context.Response.BinaryWrite(image); } } } catch { } /*try * { * int id = Convert.ToInt32(context.Request.QueryString["Id"]); * * string cmdText = "SELECT Icon FROM [ProductVersion] WHERE Id = " + id; * string myConnection = "Data Source=server.database.windows.net;Initial Catalog=xxxx;User ID=xxxx;Password=xxxxx"; * SqlConnection connection = new SqlConnection(myConnection); * SqlCommand command = new SqlCommand(cmdText, connection); * try * { * context.Response.ContentType = "image/jpeg"; * * * connection.Open(); * * SqlDataReader reader = command.ExecuteReader(); * reader.Read(); * byte[] image = (byte[])reader.GetValue(0); * context.Response.BinaryWrite(image); * reader.Close(); * } * catch { } * finally * { * connection.Close(); * } * } * catch { }*/ }
/// <summary> /// _work /// \_update /// \bin /// \externals /// \run.sh /// \run.cmd /// \package.zip //temp download .zip/.tar.gz /// </summary> /// <param name="token"></param> /// <returns></returns> private async Task<string> DownloadLatestAgent(CancellationToken token) { var agentServer = HostContext.GetService<IAgentServer>(); string latestAgentDirectory = IOUtil.GetUpdatePath(HostContext); IOUtil.DeleteDirectory(latestAgentDirectory, token); Directory.CreateDirectory(latestAgentDirectory); string archiveFile = Path.Combine(latestAgentDirectory, $"{new Uri(_latestPackage.DownloadUrl).Segments.Last()}"); try { using (var httpClient = new HttpClient()) { //open zip stream in async mode using (FileStream fs = new FileStream(archiveFile, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize: 4096, useAsync: true)) { using (Stream result = await httpClient.GetStreamAsync(_latestPackage.DownloadUrl)) { //81920 is the default used by System.IO.Stream.CopyTo and is under the large object heap threshold (85k). await result.CopyToAsync(fs, 81920, token); await fs.FlushAsync(token); } } } if (archiveFile.EndsWith(".zip", StringComparison.OrdinalIgnoreCase)) { ZipFile.ExtractToDirectory(archiveFile, latestAgentDirectory); } else if (archiveFile.EndsWith(".tar.gz", StringComparison.OrdinalIgnoreCase)) { var whichUtil = HostContext.GetService<IWhichUtil>(); string tar = whichUtil.Which("tar"); if (string.IsNullOrEmpty(tar)) { throw new NotSupportedException($"tar -xzf"); } // tar -xzf using (var processInvoker = HostContext.CreateService<IProcessInvoker>()) { processInvoker.OutputDataReceived += new EventHandler<ProcessDataReceivedEventArgs>((sender, args) => { if (!string.IsNullOrEmpty(args.Data)) { Trace.Info(args.Data); } }); processInvoker.ErrorDataReceived += new EventHandler<ProcessDataReceivedEventArgs>((sender, args) => { if (!string.IsNullOrEmpty(args.Data)) { Trace.Error(args.Data); } }); int exitCode = await processInvoker.ExecuteAsync(latestAgentDirectory, tar, $"-xzf {archiveFile}", null, token); if (exitCode != 0) { throw new NotSupportedException($"Can't use 'tar -xzf' extract archive file: {archiveFile}. return code: {exitCode}."); } } } else { throw new NotSupportedException($"{archiveFile}"); } Trace.Info($"Finished getting latest agent package at: {latestAgentDirectory}."); } finally { try { // delete .zip file if (!string.IsNullOrEmpty(archiveFile) && File.Exists(archiveFile)) { Trace.Verbose("Deleting latest agent package zip: {0}", archiveFile); IOUtil.DeleteFile(archiveFile); } } catch (Exception ex) { //it is not critical if we fail to delete the temp folder Trace.Warning("Failed to delete agent package zip '{0}'. Exception: {1}", archiveFile, ex); } } return latestAgentDirectory; }
public async Task SaveUpkFile(DomainHeader Header, string Filename) { if (Header == null) return; foreach(DomainExportTableEntry export in Header.ExportTable.Where(export => export.DomainObject == null)) await export.ParseDomainObject(Header, false, false); FileStream stream = new FileStream(Filename, FileMode.Create); int headerSize = Header.GetBuilderSize(); ByteArrayWriter writer = ByteArrayWriter.CreateNew(headerSize); await Header.WriteBuffer(writer, 0); await stream.WriteAsync(writer.GetBytes(), 0, headerSize); foreach(DomainExportTableEntry export in Header.ExportTable) { ByteArrayWriter objectWriter = await export.WriteObjectBuffer(); await stream.WriteAsync(objectWriter.GetBytes(), 0, objectWriter.Index); } await stream.FlushAsync(); stream.Close(); }
public async Task WriteFromStream(Stream sourceStream, FileInfo[] targets) { // create copy if multiple targets if(targets.Length > 1) { var copy = new MemoryStream(); await sourceStream.CopyToAsync(copy); sourceStream = copy; } foreach (var target in targets) { target.EnsureParentDirectoryExists(Path); string targetPath = target.ResolveFullPath(Path); using (var fileStream = new FileStream(targetPath, FileMode.Create)) { await sourceStream.CopyToAsync(fileStream); await fileStream.FlushAsync(); } } }
private async Task<string> CopyAssets () { try { Android.Content.Res.AssetManager assetManager = _context.Assets; string[] files = assetManager.List ("tessdata"); File file = _context.GetExternalFilesDir (null); var tessdata = new File (_context.GetExternalFilesDir (null), "tessdata"); if (!tessdata.Exists ()) { tessdata.Mkdir (); } else { var packageInfo = _context.PackageManager.GetPackageInfo (_context.PackageName, 0); var version = packageInfo.VersionName; var versionFile = new File (tessdata, "version"); if (versionFile.Exists ()) { var fileVersion = System.IO.File.ReadAllText (versionFile.AbsolutePath); if (version == fileVersion) { Log.Debug ("[TesseractApi]", "Application version didn't change, skipping copying assets"); return file.AbsolutePath; } versionFile.Delete (); } System.IO.File.WriteAllText (versionFile.AbsolutePath, version); } Log.Debug ("[TesseractApi]", "Copy assets to " + file.AbsolutePath); foreach (var filename in files) { using (var inStream = assetManager.Open ("tessdata/" + filename)) { var outFile = new File (tessdata, filename); if (outFile.Exists ()) { outFile.Delete (); } using (var outStream = new FileStream (outFile.AbsolutePath, FileMode.Create)) { await inStream.CopyToAsync (outStream); await outStream.FlushAsync (); } } } return file.AbsolutePath; } catch (Exception ex) { Log.Error ("[TesseractApi]", ex.Message); } return null; }
public override Task FlushAsync(CancellationToken cancellationToken) { return(tempStream.FlushAsync(cancellationToken)); }
/// <summary> /// Asynchronously deletes the specified file. /// </summary> /// /// <param name="path">The name of the file to be deleted. Wildcard characters are not supported.</param> /// <remarks> /// <para> /// Specify a file name with any relative or absolute path information for the <paramref name="path"/> parameter. /// Wildcard characters cannot be included. Relative path information is interpreted as relative to the current working directory. /// To obtain the current working directory, see <see cref="Directory.GetCurrentDirectory"/>. /// </para> /// <para>If the file to be deleted does not exist, no exception is thrown.</para> /// </remarks> /// /// <exception cref="ArgumentException"><paramref name="path"/> is a zero-length string, contains only white space, or contains one more invalid characters defined by the <see cref="Path.GetInvalidPathChars"/> method.</exception> /// <exception cref="ArgumentNullException"><paramref name="path"/> is <c>null</c>.</exception> /// <exception cref="DirectoryNotFoundException">The path specified in <paramref name="path"/> is invalid (for example, it is on an unmapped drive).</exception> /// <exception cref="IOException"> /// <para>The specified file is in use.</para> /// <para>-or-</para> /// <para>There is an open handle on the file, and the operating system is Windows XP or earlier. This open handle can result from enumerating directories and files.</para> /// </exception> /// <exception cref="NotSupportedException"><paramref name="path"/> is in an invalid format.</exception> /// <exception cref="PathTooLongException"> /// The specified path, file name, or both exceed the system-defined maximum length. /// For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters. /// </exception> /// <exception cref="SecurityException">The caller does not have the required permission.</exception> /// <exception cref="UnauthorizedAccessException"><paramref name="path"/> specifies a read-only file. /// <para>-or-</para> /// <para>The caller does not have the required permission.</para> /// <para>-or-</para> /// <para>The file is an executable file that is in use.</para> /// <para>-or-</para> /// <para><paramref name="path"/> is a directory.</para> /// </exception> /// /// <returns>Task that represents asynchronous operation.</returns> public static async Task DeleteAsync(string path) { PathValidator.EnsureCorrectFileSystemPath(path); if (File.Exists(path)) { const int bufferSize = 4096; using (var fileStream = new FileStream(path, FileMode.Truncate, FileAccess.Write, FileShare.Delete, bufferSize, true)) { await fileStream.FlushAsync(); File.Delete(path); } } }
private async Task DownloadAsync(IExecutionContext executionContext, TaskReference task) { Trace.Entering(); ArgUtil.NotNull(executionContext, nameof(executionContext)); ArgUtil.NotNull(task, nameof(task)); ArgUtil.NotNullOrEmpty(task.Version, nameof(task.Version)); var taskServer = HostContext.GetService<ITaskServer>(); // first check to see if we already have the task string destDirectory = GetDirectory(task); Trace.Info($"Ensuring task exists: ID '{task.Id}', version '{task.Version}', name '{task.Name}', directory '{destDirectory}'."); if (Directory.Exists(destDirectory)) { Trace.Info("Task already downloaded."); return; } Trace.Info("Getting task."); string zipFile; var version = new TaskVersion(task.Version); //download and extract task in a temp folder and rename it on success string tempDirectory = Path.Combine(IOUtil.GetTasksPath(HostContext), "_temp_" + Guid.NewGuid()); try { Directory.CreateDirectory(tempDirectory); zipFile = Path.Combine(tempDirectory, string.Format("{0}.zip", Guid.NewGuid())); //open zip stream in async mode using (FileStream fs = new FileStream(zipFile, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize: 4096, useAsync: true)) { using (Stream result = await taskServer.GetTaskContentZipAsync(task.Id, version, executionContext.CancellationToken)) { //81920 is the default used by System.IO.Stream.CopyTo and is under the large object heap threshold (85k). await result.CopyToAsync(fs, 81920, executionContext.CancellationToken); await fs.FlushAsync(executionContext.CancellationToken); } } ZipFile.ExtractToDirectory(zipFile, tempDirectory); File.Delete(zipFile); Directory.CreateDirectory(Path.GetDirectoryName(destDirectory)); Directory.Move(tempDirectory, destDirectory); Trace.Info("Finished getting task."); } finally { try { //if the temp folder wasn't moved -> wipe it if (Directory.Exists(tempDirectory)) { Trace.Verbose("Deleting task temp folder: {0}", tempDirectory); IOUtil.DeleteDirectory(tempDirectory, CancellationToken.None); // Don't cancel this cleanup and should be pretty fast. } } catch (Exception ex) { //it is not critical if we fail to delete the temp folder Trace.Warning("Failed to delete temp folder '{0}'. Exception: {1}", tempDirectory, ex); executionContext.Warning(StringUtil.Loc("FailedDeletingTempDirectory0Message1", tempDirectory, ex.Message)); } } }
internal async Task<HttpSourceResult> GetAsync(string uri, string cacheKey, TimeSpan cacheAgeLimit, bool ignoreNotFounds, CancellationToken cancellationToken) { var sw = new Stopwatch(); sw.Start(); var result = await TryCache(uri, cacheKey, cacheAgeLimit); if (result.Stream != null) { Logger.LogVerbose(string.Format(CultureInfo.InvariantCulture, " {0} {1}", "CACHE".Green(), uri)); return result; } Logger.LogVerbose(string.Format(CultureInfo.InvariantCulture, " {0} {1}.", "GET".Yellow(), uri)); var request = new HttpRequestMessage(HttpMethod.Get, uri); if (_userName != null) { var token = Convert.ToBase64String(Encoding.UTF8.GetBytes(_userName + ":" + _password)); request.Headers.Authorization = new AuthenticationHeaderValue("Basic", token); } ; #if DNXCORE50 if (_proxyUserName != null) { var proxyToken = Convert.ToBase64String(Encoding.UTF8.GetBytes(_proxyUserName + ":" + _proxyPassword)); request.Headers.ProxyAuthorization = new AuthenticationHeaderValue("Basic", proxyToken); } #endif var response = await _client.SendAsync(request, cancellationToken); if (ignoreNotFounds && response.StatusCode == HttpStatusCode.NotFound) { Logger.LogInformation(string.Format(CultureInfo.InvariantCulture, " {1} {0} {2}ms", uri, response.StatusCode.ToString().Green(), sw.ElapsedMilliseconds.ToString().Bold())); return new HttpSourceResult(); } response.EnsureSuccessStatusCode(); var newFile = result.CacheFileName + "-new"; // Zero value of TTL means we always download the latest package // So we write to a temp file instead of cache if (cacheAgeLimit.Equals(TimeSpan.Zero)) { result.CacheFileName = Path.GetTempFileName(); newFile = Path.GetTempFileName(); } // The update of a cached file is divided into two steps: // 1) Delete the old file. 2) Create a new file with the same name. // To prevent race condition among multiple processes, here we use a lock to make the update atomic. await ConcurrencyUtilities.ExecuteWithFileLocked(result.CacheFileName, async _ => { using (var stream = new FileStream( newFile, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete, BufferSize, useAsync: true)) { await response.Content.CopyToAsync(stream); await stream.FlushAsync(cancellationToken); } if (File.Exists(result.CacheFileName)) { // Process B can perform deletion on an opened file if the file is opened by process A // with FileShare.Delete flag. However, the file won't be actually deleted until A close it. // This special feature can cause race condition, so we never delete an opened file. if (!IsFileAlreadyOpen(result.CacheFileName)) { File.Delete(result.CacheFileName); } } // If the destination file doesn't exist, we can safely perform moving operation. // Otherwise, moving operation will fail. if (!File.Exists(result.CacheFileName)) { File.Move( newFile, result.CacheFileName); } // Even the file deletion operation above succeeds but the file is not actually deleted, // we can still safely read it because it means that some other process just updated it // and we don't need to update it with the same content again. result.Stream = new FileStream( result.CacheFileName, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete, BufferSize, useAsync: true); return 0; }); Logger.LogVerbose(string.Format(CultureInfo.InvariantCulture, " {1} {0} {2}ms", uri, response.StatusCode.ToString().Green(), sw.ElapsedMilliseconds.ToString().Bold())); return result; }