コード例 #1
0
        public IDictionary <string, BackupFileInfo> GetFileIndex()
        {
            IDictionary <string, BackupFileInfo> result = new Dictionary <string, BackupFileInfo>();

            foreach (var directoryInfo in _directoriesInfo)
            {
                string host       = Environment.MachineName;
                string rootPath   = directoryInfo.FullName;
                int    rootLength = rootPath.Length + 1;
                foreach (var file in directoryInfo.GetFiles("*", SearchOption.AllDirectories))
                {
                    string   relativePath = file.FullName.Substring(rootLength);
                    PathInfo filePathInfo = new PathInfo
                    {
                        Host          = host,
                        RootPath      = rootPath,
                        RelativePath  = relativePath,
                        AddedDateTime = DateTime.UtcNow,
                        DeletedDate   = null
                    };
                    BackupFileInfo backupFileInfo = new BackupFileInfo();
                    using (var fileStream = file.OpenRead())
                    {
                        backupFileInfo.FileId = _fileHasher.CalculateHash(fileStream);
                    }
                    if (result.ContainsKey(backupFileInfo.FileId))
                    {
                        backupFileInfo = result[backupFileInfo.FileId];
                    }
                    else
                    {
                        backupFileInfo.Paths          = new List <PathInfo>();
                        result[backupFileInfo.FileId] = backupFileInfo;
                    }
                    backupFileInfo.Paths.Add(filePathInfo);
                }
            }
            return(result);
        }
コード例 #2
0
        public async Task <IActionResult> Post(IFormFile file)
        {
            try
            {
                _logger.Information("Received Post request {fileName} length:{fileLength}kb user:{user}", file.FileName, file.Length, User.Identity.Name);
                long size = file.Length;

                if (file.Length <= 0)
                {
                    return(BadRequest("file must not be empty"));
                }

                var rand         = new Random();
                var tempFilePath = $"{_appSettingsOptions.TempFileDirectory}{rand.Next()}.tmp";
                using (var tempFileStream = _tempFileHelper.Create(tempFilePath))
                {
                    await file.CopyToAsync(tempFileStream);
                }

                var(fileTypeIsOk, fileType) = _fileTypeScanner.CheckFileType(tempFilePath);
                if (!fileTypeIsOk)
                {
                    _logger.Warning("Detected file type {detectedFileType} is not allowed, original filename is {fileName}", fileType, file.Name);

                    _tempFileHelper.Delete(tempFilePath);

                    return(BadRequest($"File type {fileType} is not allowed"));
                }

                if (!file.FileName.ToLower().EndsWith(fileType.ToLower()))
                {
                    _logger.Warning("Detected file type {detectedFileType} does not match supplied filename {fileName}", fileType, file.Name);

                    _tempFileHelper.Delete(tempFilePath);

                    return(BadRequest($"File type {fileType} is not allowed"));
                }

                var(fileContainsVirus, detail) = _fileVirusScanner.ScanFile(tempFilePath);
                if (fileContainsVirus)
                {
                    _logger.Warning("Detected vulnerability in file {fileType} {fileName} {detail}", fileType, file.Name, detail);

                    _tempFileHelper.Delete(tempFilePath);

                    return(BadRequest("File is corrupt"));
                }

                var    hash = _fileHasher.CalculateHash(tempFilePath);
                string blobId;
                bool   created;
                using (var stream = new FileStream(tempFilePath, FileMode.Open))
                {
                    var postResult = _dataRepository.PostBlob(stream, file.FileName, hash);
                    blobId  = postResult.BlobId;
                    created = postResult.Created;
                }

                _tempFileHelper.Delete(tempFilePath);

                var uri = GenerateGetUri(blobId, "Upload");

                if (created)
                {
                    return(Created(uri, new { uri, blobId, size, hash }));
                }

                return(Ok(new { uri, blobId, size, hash }));
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "Exception occured in Post {@Exception}", ex);
                return(new StatusCodeResult(500));
            }
        }