public static string EnsureDependencies([NotNull] Command command) { __workingDir ??= PathHelper.AddDirectorySeparator(AssemblyHelper.GetEntryAssembly()?.GetDirectoryPath() ?? Directory.GetCurrentDirectory()); if (string.IsNullOrEmpty(command.ExecutableName)) { return(null); } string exePath = __workingDir + command.ExecutableName; if (!File.Exists(exePath)) { throw new FileNotFoundException("Dependency not found.", exePath); } foreach (string dependency in command.Dependencies.SkipNullOrEmptyTrim()) { if (File.Exists(__workingDir + dependency)) { continue; } throw new FileNotFoundException("Dependency not found.", dependency); } return(exePath); }
static DbMigration() { RootPath = PathHelper.AddDirectorySeparator(AppDomain.CurrentDomain.BaseDirectory); MigrationsPath = string.Concat(RootPath, DIR_MIGRATIONS); BuildMigrationsPath = string.Concat(RootPath, DIR_RELATIVE, DIR_RELATIVE, DIR_MIGRATIONS); SQLPath = string.Concat(MigrationsPath, DIR_SQL); BuildSQLPath = string.Concat(BuildMigrationsPath, DIR_SQL); }
public static string GetDirectoryPath([NotNull] this Assembly thisValue) { string path = GetPath(thisValue); return(string.IsNullOrEmpty(path) ? null : PathHelper.AddDirectorySeparator(Path.GetDirectoryName(path))); }
public static IEnumerable <string> ExtractEmbeddedResources([NotNull] this Assembly thisValue, string directoryPath, string resourceLocation, bool skipExisting, params string[] resourceNames) { directoryPath = PathHelper.Trim(directoryPath); if (string.IsNullOrEmpty(directoryPath)) { directoryPath = Directory.GetCurrentDirectory(); } if (resourceNames.Count(string.IsNullOrWhiteSpace) > 1) { throw new ArgumentException("More than one empty resource name encountered. This will save the same resource repeatedly.", nameof(resourceNames)); } resourceLocation = resourceLocation?.Trim(); if (string.IsNullOrEmpty(resourceLocation)) { resourceLocation = thisValue.GetName().Name; } directoryPath = PathHelper.AddDirectorySeparator(directoryPath); if (!DirectoryHelper.Ensure(directoryPath)) { throw new IOException($"Cannot access or create directory '{directoryPath}'"); } if (resourceNames.IsNullOrEmpty()) { resourceNames = new[] { string.Empty } } ; foreach (string resourceName in resourceNames) { string streamName = GetResourceName(thisValue, resourceLocation, resourceName, out string fileName); string fullPath = directoryPath + fileName; if (skipExisting && File.Exists(fullPath)) { continue; } using (Stream stream = thisValue.GetManifestResourceStream(streamName)) { if (stream == null) { continue; } using (FileStream file = new FileStream(fullPath, FileMode.Create, FileAccess.Write)) stream.CopyTo(file); yield return(fullPath); } } }
public AppInfo(Assembly assembly) { _asm = assembly; if (_asm == null) { ExecutablePath = string.Empty; ExecutableName = string.Empty; Directory = string.Empty; AppGuid = string.Empty; Title = string.Empty; Description = string.Empty; ProductName = string.Empty; Company = string.Empty; Copyright = string.Empty; Trademark = string.Empty; Version = string.Empty; FileVersion = string.Empty; Culture = string.Empty; return; } ExecutablePath = _asm.GetPath(); ExecutableName = Path.GetFileNameWithoutExtension(ExecutablePath); Directory = PathHelper.AddDirectorySeparator(Path.GetDirectoryName(ExecutablePath)); AppGuid = _asm.GetCode(); Title = _asm.GetTitle(); Description = _asm.GetDescription(); ProductName = _asm.GetProductName(); Company = _asm.GetCompany(); Copyright = _asm.GetCopyright(); Trademark = _asm.GetTrademark(); Version = _asm.GetVersion(); FileVersion = _asm.GetFileVersion(); Culture = _asm.GetCulture(); }
public static async Task <string[]> UploadMultipartAsync([NotNull] HttpRequest request, [NotNull] UploadSettings uploadSettings) { if (!MultipartRequestHelper.IsMultipartContentType(request.ContentType)) { throw new ArgumentException("Request is not a multipart type."); } CancellationToken token = request.HttpContext.RequestAborted; if (token.IsCancellationRequested) { return(null); } string path = PathHelper.AddDirectorySeparator(uploadSettings.TargetPath); if (string.IsNullOrEmpty(path)) { IHostEnvironment env = request.HttpContext.RequestServices.GetRequiredService <IHostEnvironment>(); path = PathHelper.AddDirectorySeparator(Path.Combine(env.ContentRootPath, "Uploads")); } if (!DirectoryHelper.Ensure(path)) { throw new DirectoryNotFoundException(); } string boundary = MediaTypeHeaderValue.Parse(request.ContentType).GetBoundary(); if (string.IsNullOrEmpty(boundary)) { throw new InvalidDataException("Could not get request boundary."); } IList <string> uploadedFiles = new List <string>(); MultipartReader reader = new MultipartReader(boundary, request.Body); MultipartSection section = await reader.ReadNextSectionAsync(token); while (!token.IsCancellationRequested && section != null) { bool hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out ContentDispositionHeaderValue contentDisposition); if (hasContentDispositionHeader) { if (contentDisposition.HasFileContentDisposition()) { StringSegment fileNameSegment = contentDisposition.FileNameStar.Trim(); if (StringSegment.IsNullOrEmpty(fileNameSegment)) { fileNameSegment = contentDisposition.FileName.Trim(); } if (StringSegment.IsNullOrEmpty(fileNameSegment)) { continue; } string fileName = WebUtility.UrlDecode(fileNameSegment.Value)?.Trim('\"', ' '); if (string.IsNullOrEmpty(fileName)) { continue; } if (Path.IsPathFullyQualified(fileName)) { fileName = Path.GetFileName(fileName); } string fileNameBase = Path.GetFileNameWithoutExtension(fileName); string extension = Path.GetExtension(fileName); string destinationFileName = Path.Combine(path, fileName); if (Directory.Exists(destinationFileName)) { if (!uploadSettings.Rename) { throw new HttpException("A directory with the same name already exists."); } int n = 1; do { destinationFileName = Path.Combine(path, $"{fileNameBase} ({n++}){extension}"); }while (File.Exists(destinationFileName)); // File.Exists tests both file and directory } if (File.Exists(destinationFileName)) { if (uploadSettings.Overwrite) { File.Delete(destinationFileName); } if (!uploadSettings.Rename) { throw new HttpException("A file with the same name already exists."); } int n = 1; do { destinationFileName = Path.Combine(path, $"{fileNameBase} ({n++}){extension}"); }while (File.Exists(destinationFileName)); // File.Exists tests both file and directory } await using (FileStream targetStream = File.Create(destinationFileName)) { await section.Body.CopyToAsync(targetStream, token); } if (token.IsCancellationRequested) { return(null); } uploadedFiles.Add(destinationFileName); } } // Drain any remaining section body that has not been consumed and read the headers for the next section. section = await reader.ReadNextSectionAsync(token); } return(token.IsCancellationRequested ? null : uploadedFiles.ToArray()); }
public static async Task <string[]> UploadAsync([NotNull] IReadOnlyCollection <IFormFile> files, [NotNull] UploadSettings uploadSettings, CancellationToken token = default(CancellationToken)) { if (token.IsCancellationRequested) { return(null); } if (files.Count == 0) { return(Array.Empty <string>()); } string path = PathHelper.AddDirectorySeparator(uploadSettings.TargetPath); if (string.IsNullOrEmpty(path)) { throw new ArgumentException($"{nameof(UploadSettings.TargetPath)} cannot be empty."); } if (!DirectoryHelper.Ensure(path)) { throw new DirectoryNotFoundException(); } IList <string> uploadedFiles = new List <string>(); foreach (IFormFile file in files.TakeWhile(_ => !token.IsCancellationRequested)) { string fileName = WebUtility.UrlDecode(file.FileName).Trim('\"', ' '); if (string.IsNullOrEmpty(fileName)) { continue; } if (Path.IsPathFullyQualified(fileName)) { fileName = Path.GetFileName(fileName); } string fileNameBase = Path.GetFileNameWithoutExtension(fileName); string extension = Path.GetExtension(fileName); string destinationFileName = Path.Combine(path, fileName); if (Directory.Exists(destinationFileName)) { if (!uploadSettings.Rename) { throw new HttpException("A directory with the same name already exists."); } int n = 1; do { destinationFileName = Path.Combine(path, $"{fileNameBase} ({n++}){extension}"); }while (!token.IsCancellationRequested && File.Exists(destinationFileName)); // File.Exists tests both file and directory } if (File.Exists(destinationFileName)) { if (uploadSettings.Overwrite) { File.Delete(destinationFileName); } if (!uploadSettings.Rename) { throw new HttpException("A file with the same name already exists."); } int n = 1; do { destinationFileName = Path.Combine(path, $"{fileNameBase} ({n++}){extension}"); }while (!token.IsCancellationRequested && File.Exists(destinationFileName)); // File.Exists tests both file and directory } await using (Stream sourceStream = file.OpenReadStream()) { await using (FileStream targetStream = File.Create(destinationFileName)) { await sourceStream.CopyToAsync(targetStream, token); } } if (token.IsCancellationRequested) { return(null); } uploadedFiles.Add(destinationFileName); } return(token.IsCancellationRequested ? null : uploadedFiles.ToArray()); }