Exemplo n.º 1
0
        private async Task <string> BuildFile(File file, EofMessage eofMessage)
        {
            try
            {
                WorkerLog.Instance.Information($"Building {(file.IsNew() ? "" : "delta")} file '{file.Name}'");
                var temppath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), System.IO.Path.GetTempFileName());
                using var sw = System.IO.File.OpenWrite(temppath);

                var toTake = 10;
                var toSkip = 0;
                while (toSkip < eofMessage.AmountOfChunks)
                {
                    //best way is to get the chunks in chunks. If a file exists out of 1000 * 1Mb files and load that into memory, you are gonna have a bad time
                    var chunks = await _fileChunkRepository.GetFileChunkData(eofMessage.FileId, toSkip, toTake, _database);

                    foreach (var chunk in chunks.Data)
                    {
                        await sw.WriteAsync(chunk, 0, chunk.Length);
                    }
                    toSkip  = toTake + 1;
                    toTake += 10;
                }
                WorkerLog.Instance.Information($"File built to '{temppath}'");
                return(temppath);
            }
            catch (Exception ex)
            {
                WorkerLog.Instance.Error(ex, $"Error occured while assembling {(file.IsNew() ? "" : "delta")} file '{file.Name}' to tempfile");
                return(null);
            }
        }