Пример #1
0
        public async Task <FileOperationStatus> AddFileAsync(string sectionName, string filename, Stream content)
        {
            if (string.IsNullOrWhiteSpace(filename))
            {
                return(FileOperationStatus.BadParameters);
            }
            var section = FileSections.FirstOrDefault(s => s.Name == sectionName);

            if (section == null)
            {
                LogDebug($"Section {sectionName} not found");
                return(FileOperationStatus.NotFound);
            }
            if (content == null || !content.CanRead)
            {
                return(FileOperationStatus.BadParameters);
            }

            if (filename.Contains("_CHANGED_"))
            {
                return(FileOperationStatus.BadParameters);
            }
            var filePath = FilesLocation + Path.DirectorySeparatorChar + section.Folder + Path.DirectorySeparatorChar + filename;

            if (File.Exists(filePath))
            {
                return(FileOperationStatus.Conflict);
            }

            try
            {
                using (var file = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    await content.CopyToAsync(file).ConfigureAwait(false);

                    await file.FlushAsync();
                }
            }
            catch (Exception ex)
            {
                LogException(LogLevel.Warning, ex);
                return(FileOperationStatus.Error);
            }

            if (FilesPreprocessingEnabled)
            {
                return(FilePreprocessController.EnqueueFile(filePath)
                    ? FileOperationStatus.JobQueued
                    : FileOperationStatus.Ok);
            }
            return(FileOperationStatus.Ok);
        }
Пример #2
0
        public Stream GetFileStream(string sectionName, string filename)
        {
            if (string.IsNullOrWhiteSpace(filename))
            {
                return(null);
            }
            var section = FileSections.FirstOrDefault(s => s.Name == sectionName);

            if (section == null)
            {
                LogDebug($"Section {sectionName} not found");
                return(null);
            }

            var filePath = FilesLocation + Path.DirectorySeparatorChar + section.Folder + Path.DirectorySeparatorChar + filename;

            return(File.Exists(filePath) ? new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) : null);
        }
Пример #3
0
        public bool FileExists(string sectionName, string filename)
        {
            if (string.IsNullOrWhiteSpace(filename))
            {
                return(false);
            }
            var section = FileSections.FirstOrDefault(s => s.Name == sectionName);

            if (section == null)
            {
                LogDebug($"Section {sectionName} not found");
                return(false);
            }

            var filePath = FilesLocation + Path.DirectorySeparatorChar + section.Folder + Path.DirectorySeparatorChar + filename;

            return(File.Exists(filePath));
        }
Пример #4
0
 private FileSection ExtractFileSection(string localPath)
 {
     if (localPath == null)
     {
         return(null);
     }
     try
     {
         var sectionName    = localPath.Trim('/');
         var slashIndex     = sectionName.IndexOf("/", StringComparison.Ordinal);
         var lastSlashIndex = sectionName.LastIndexOf("/", StringComparison.Ordinal);
         sectionName = sectionName.Substring(slashIndex + 1, lastSlashIndex - slashIndex - 1);
         LogDebug($"Section requested: {sectionName}");
         return(FileSections.FirstOrDefault(s => s.Folder == sectionName));
     }
     catch (Exception)
     {
         return(null);
     }
 }
Пример #5
0
        public FileOperationStatus DeleteFile(string sectionName, string filename)
        {
            var section = FileSections.FirstOrDefault(s => s.Name == sectionName);

            if (section == null)
            {
                LogDebug($"Section {sectionName} not found");
                return(FileOperationStatus.NotFound);
            }

            if (filename.Contains("_CHANGED_"))
            {
                return(FileOperationStatus.BadParameters);
            }
            var filePath = FilesLocation + Path.DirectorySeparatorChar + section.Folder + Path.DirectorySeparatorChar + filename;

            if (!File.Exists(filePath))
            {
                LogDebug($"File {filePath} does not exist and cannot be deleted");
                return(FileOperationStatus.NotFound);
            }

            var compressed = IoHelper.LoadAllChanged(filePath);

            try
            {
                File.Delete(filePath);
                compressed.ForEach(File.Delete);
                return(FileOperationStatus.Ok);
            }
            catch (Exception ex)
            {
                LogException(LogLevel.Warning, ex);
                return(FileOperationStatus.Error);
            }
        }