Ejemplo n.º 1
0
        public NoteFileContent AddFile(string userId, FileWithContent file, string path)
        {
            string name = $"{userId}_" +
                          $"{DateTime.UtcNow:yyyy-MM-dd_h-mm-ss_tt}" +
                          $".{file.File.FileName.Split('.').Last()}";

            Console.WriteLine(name);
            try
            {
                path = GetFilePath(path, file.File.FileType);
            }
            catch (Exception)
            {
                throw new Exception("Incorrect file type.\n");
            }

            DirectoryInfo dirInfo = new DirectoryInfo(path);

            if (!dirInfo.Exists)
            {
                dirInfo.Create();
            }
            File.WriteAllBytes($"{path}/{name}", file.Content);

            file.File.FileName = name;
            return(_noteRepository.AddFile(Guid.Parse(userId), file.File));
        }