예제 #1
0
        public async Task <IActionResult> AddMusic([FromForm] AddMusicModel model)
        {
            User user = await db.Users.FindAsync(UserId);

            string dateTimeNow = $"{DateTime.Now.Day}.{DateTime.Now.Month}.{DateTime.Now.Year} {DateTime.Now.Hour}:{DateTime.Now.Minute}:{DateTime.Now.Second}";

            if (await db.Musics.Where(m => m.UserId == user.UserId && m.MusicName == model.MusicName).FirstOrDefaultAsync() != null)
            {
                return(Ok(new { msg = $"У вас уже есть запись с названием {model.MusicName}" }));
            }
            string musicFileName    = $"{user.Login}_{dateTimeNow}_" + model.MusicFile.FileName;
            string sharingLinkMusic = "";
            string sharingLinkImage = "";

            try
            {
                if (await cloud.IfFileExists("", musicFileName))
                {
                    return(Ok(new { msg = $"В вашем хранилище уже есть файл {model.MusicFile.FileName}" }));
                }
                if (model.MusicImageFile != null)
                {
                    if (await cloud.IfFileExists("", $"{user.Login}_music_{dateTimeNow}_" + model.MusicImageFile.FileName))
                    {
                        return(Ok(new { msg = $"В вашем хранилище уже есть файл {model.MusicImageFile.FileName}" }));
                    }
                    sharingLinkImage = await cloud.AddFile("", $"{user.Login}_music_{dateTimeNow}_" + model.MusicImageFile.FileName, model.MusicImageFile.OpenReadStream());
                }
                sharingLinkMusic = await cloud.AddFile("", musicFileName, model.MusicFile.OpenReadStream());

                db.Musics.Add(new Music
                {
                    MusicName         = model.MusicName,
                    MusicFileName     = musicFileName,
                    MusicUrl          = sharingLinkMusic,
                    MusicImageName    = model.MusicImageFile == null ? "default.png" : $"{user.Login}_music_{dateTimeNow}_" + model.MusicImageFile.FileName,
                    MusicImageUrl     = model.MusicImageFile == null ? "https://www.dropbox.com/s/jattf04mjk4x903/default.png?dl=1" : sharingLinkImage,
                    UserId            = user.UserId,
                    DateOfPublication = DateTime.Now.Date,
                    MusicGenreId      = model.MusicGenreId
                });
                await db.SaveChangesAsync();

                return(Ok(new { msg = "" }));
            }
            catch
            {
                return(StatusCode(500));
            }
        }