Ejemplo n.º 1
0
        public async Task <string> SaveUniqueFileAsync(
            Stream stream,
            string fileName,
            string path)
        {
            var parts     = fileName.Split('.');
            var extension = parts[parts.Length - 1];

            if (string.IsNullOrEmpty(extension))
            {
                throw new Exception("Could not obtain a fie extension!");
            }

            if (extension.Length > 6)
            {
                throw new Exception("The file extension is not valid!");
            }

            string fullPath;

            if (!path.EndsWith("\\"))
            {
                path = path + "\\";
            }

            do
            {
                fileName = System.Guid.NewGuid().ToString();
                fileName = fileName.Substring(0, ByMaxFileNameLength - extension.Length - 1);
                fileName = fileName + "." + extension;
                fullPath = path + fileName;
            } while (_fileSystem.FileExists(fullPath));

            if (_logger.IsEnabled(LogLevel.Information))
            {
                _logger.LogInformation($"Attempting to save unique file to {fullPath}.");
            }

            // write the file
            await _fileSystem.CreateFileAsync(fullPath, stream);

            if (_logger.IsEnabled(LogLevel.Information))
            {
                _logger.LogInformation($"Successfully saved unique file at {fullPath}.");
            }

            return(fileName);
        }
Ejemplo n.º 2
0
 public bool FileExists(string path)
 {
     return(_fileSystem.FileExists(path));
 }