Esempio n. 1
0
        public async Task <ZwinnyCRUD.Common.Models.File> Upload(string FileName, long Length, Stream Content, int id)
        {
            var Project = await _projectContext.FindOrDefault(id);

            var dirPath  = Path.Combine(_targetFilePath, Convert.ToString(Project.Id));
            var filePath = Path.Combine(dirPath, FileName);

            if (!Directory.Exists(dirPath))
            {
                Directory.CreateDirectory(dirPath);
            }

            if (System.IO.File.Exists(filePath))
            {
                return(null);
            }

            var myFile = new Common.Models.File
            {
                FilePath    = filePath,
                Name        = FileName,
                SizeinBytes = Length,
                Uploaded    = DateTimeOffset.UtcNow,
                ProjectId   = Project.Id
            };

            using (var fileStream = System.IO.File.Create(filePath))
            {
                await Content.CopyToAsync(fileStream);
            }

            return(myFile);
        }
Esempio n. 2
0
        public (bool isSuccessed, string[] logs) UnpackArchive(string folder, Common.Models.File file)
        {
            var logs = new List <string>
            {
                $"Start unpacking file {file.FileName} to foler: {folder}."
            };

            try
            {
                using var stream  = new MemoryStream(file.Body.ToArray());
                using var archive = new ZipArchive(stream, ZipArchiveMode.Read);
                archive.ExtractToDirectory(folder, true);

                logs.Add($"{file.FileName} unpacked.");

                return(true, logs.ToArray());
            }
            catch (Exception e)
            {
                logs.Add($"Failed to unpack {file.FileName} because of error:");
                logs.Add(e.Message);
                logs.Add(e.StackTrace);
                // ToDo: add inner exceptions to logs
                return(false, logs.ToArray());
            }
        }