Пример #1
0
        public async Task <bool> SendAndManageZipRequest(string zipFileName,
                                                         DateTimeRange timeRange, string[] fullCategories)
        {
            zipFileName = zipFileName.Trim();
            // append .zip if the string does not end with it
            if (!zipFileName.ToLower().EndsWith(".zip"))
            {
                zipFileName += ".zip";
            }

            // create and send request
            var request    = new ZipRequestMessage(zipFileName, timeRange, fullCategories);
            var resDecoded = await SendRequestAwaitResponse(request);

            // process response
            if (resDecoded.ResCode == ResponseCode.Ok)
            {
                new FileOperationProgressForm(
                    new ZipOperationUpdateReceiver(client)).ShowDialog();
                return(true);
            }
            else if (resDecoded.ResCode == ResponseCode.Error)
            {
                MessageBox.Show("Your request was rejected by the server.\n" +
                                "Reason: " + resDecoded.ErrorMessage,
                                "Request Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return(false);
        }
Пример #2
0
        public void HandleZipRequest(ZipRequestMessage request)
        {
            string zipPath = Path.Combine(logsDestPath, request.ZipFileName);

            string[] filePaths = FileOperations.GetFilteredFilePaths(logsSourcePath,
                                                                     request.TimeRangeLocal, request.FullCategories);
            if (filePaths.Length == 0)
            {
                SendResponseMessage(ResponseCode.Error,
                                    "No files found that match request");
                return;
            }
            if (File.Exists(zipPath))
            {
                SendResponseMessage(ResponseCode.Error,
                                    $"Zip archive with the name {request.ZipFileName} already exists.");
                return;
            }
            // send response
            SendResponseMessage(ResponseCode.Ok);
            // Open zip archive if it does not already exist
            logger.Log("Creating zip archive: " + zipPath);
            using (ZipArchive archive = FileOperations.OpenZipArchive(zipPath))
            {
                // track number of files that could not be processed
                int numSkipped = 0;
                logger.Log("Beginning zip operation...");
                // Loop through the input files and zip one by one. Report progress to client through writer.
                for (int i = 0; i < filePaths.Length; i++)
                {
                    string path = filePaths[i];
                    try
                    {
                        if (!File.Exists(path) ||
                            request.TimeRangeLocal.IsAfterRange(File.GetCreationTime(path)))
                        {
                            // file was deleted while processing and/or recreated
                            logger.Log($"File {path} was deleted during the operation. Skipping");
                            numSkipped += 1;
                        }
                        else
                        {
                            logger.Log("Zipping file: " + path);
                            archive.CreateEntryFromFile(path, Path.GetFileName(path));
                        }
                        // report progress to the client
                        var message = new ZipOperationMessage(
                            i + 1 - numSkipped, filePaths.Length - numSkipped);
                        client.Writer.Write(message);
                    }
                    catch (Exception e)
                    {
                        logger.Log($"Error while zipping {path}. Zip operation cancelled");
                        logger.Log(e.ToString());
                        File.Delete(zipPath);
                        break;
                    }
                }
                logger.Log("Zip operation complete");
            }
        }