Esempio n. 1
0
        private async Task SaveDocumentsAsync(string projectId, string branchName, Stream stream)
        {
            var    stringBuilder = new StringBuilder();
            string pathToFile    = GetTempFilePath();

            stringBuilder.AppendLine($"[{DateTime.UtcNow}] Saving documentation ({projectId}/{branchName}) to file: {pathToFile}.");
            SaveDocumentsToTempFile(pathToFile, stream);
            stringBuilder.AppendLine($"[{DateTime.UtcNow}] Documentation saved ({projectId}/{branchName}) to file: {pathToFile}.");

            stringBuilder.AppendLine($"[{DateTime.UtcNow}] Enqueueing uploading documentation job ({projectId}/{branchName}/{pathToFile}).");
            BackgroundJob.Enqueue <IUploaderJob>(x => x.UploadBranchAsync(projectId, branchName, pathToFile));
            stringBuilder.AppendLine($"[{DateTime.UtcNow}] Uploading docuumentation job enqueued ({projectId}/{branchName}/{pathToFile}).");

            await _logsService.AppendLogsAsync(projectId, new LogsDto { Message = stringBuilder.ToString() });
        }
        public async Task <IActionResult> Put(string projectId, [FromBody] LogsDto logs)
        {
            var projectFromStorage = await _projectsService.GetProjectAsync(projectId);

            if (projectFromStorage == null)
            {
                return(NotFound());
            }

            var authorization = await _authorizationService.AuthorizeAsync(User, projectFromStorage, Operations.Update);

            if (!authorization.Succeeded)
            {
                return(Forbid());
            }

            await _logsService.AppendLogsAsync(projectId, logs);

            return(Ok());
        }
        public async Task UploadBranchAsync(string projectId, string branchName, string filePath)
        {
            var logs    = new StringBuilder();
            var logsDto = new LogsDto();

            try
            {
                LogInformation(logs, $"Starting uploading project ({projectId}/{branchName}). Temporary file name: {filePath}.");
                if (!File.Exists(filePath))
                {
                    LogInformation(logs, $"Uploading project stopped ({projectId}/{branchName}). File '{filePath}' not exists.");

                    logsDto.Message = logs.ToString();
                    await _logsService.AppendLogsAsync(projectId, logsDto);

                    return;
                }

                _httpContextHeaders.Headers = new Dictionary <string, StringValues>();
                _httpContextHeaders.Headers.Add("Authorization", $"SecureToken {_applicationParameters.SecureToken}");

                LogInformation(logs, $"Getting branches information ({projectId}/{branchName}).");
                var branches = await _branchService.GetBranchesAsync(projectId);

                LogInformation(logs, $"Branches information retrieved ({projectId}/{branchName}).");

                if (branchName != null && branches.Any(x => x.Name == branchName))
                {
                    LogInformation(logs, $"Deleting branch from storage ({projectId}/{branchName}).");
                    await _branchService.DeleteBranchAsync(projectId, branchName);

                    LogInformation(logs, $"Branch was deleted ({projectId}/{branchName}).");
                }

                LogInformation(logs, $"Uploading branch to storage ({projectId}/{branchName}).");
                LogInformation(logs, $"Reading temporary file ({projectId}/{branchName}) from path: '{filePath}'.");
                using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    LogInformation(logs, $"File '{filePath}' exists and was readed.");
                    await _documentsService.UploadBranchAsync(projectId, branchName, fileStream, logs);
                }
                LogInformation(logs, $"Branch uploaded ({projectId}/{branchName}).");

                LogInformation(logs, $"Getting project information ({projectId}/{branchName}).");
                var project = await _projectsService.GetProjectAsync(projectId);

                if (!project.IsAccessLimited)
                {
                    LogInformation(logs, $"Ordering index refresh ({projectId}/{branchName}).");
                    await _searchService.RefreshIndexAsync(projectId, branchName);

                    LogInformation(logs, $"Index refresh ordered ({projectId}/{branchName}).");
                }

                LogInformation(logs, $"Uploading project ({projectId}/{branchName}) finished successfully.");
            }
            catch (Exception exception)
            {
                LogError(logs, $"During uploadind exception occurs ({projectId}/{branchName}).");
                LogError(logs, $"Exception: {exception.ToString()}.");
                LogError(logs, $"Message: {exception.Message}.");
                LogError(logs, $"Stack trace: {exception.StackTrace}.");

                if (exception.InnerException != null)
                {
                    LogError(logs, $"Inner exception: {exception.InnerException.ToString()}.");
                    LogError(logs, $"Inner exception message: {exception.InnerException.Message}.");
                    LogError(logs, $"Inner exception stack trace: {exception.InnerException.StackTrace}.");
                }

                LogError(logs, $"Uploading project ({projectId}/{branchName}) failed.");
            }
            finally
            {
                LogInformation(logs, $"Deleting temporary file: '{filePath}'.");
                File.Delete(filePath);
                LogInformation(logs, $"Temporary file '{filePath}' was deleted.");
            }

            logsDto.Message = logs.ToString();
            await _logsService.AppendLogsAsync(projectId, logsDto);
        }