コード例 #1
0
        public async Task <IActionResult> EditMusic([FromForm] EditMusicModel model)
        {
            string musicFileName, imageFileName;
            User   user = await db.Users.FindAsync(UserId);

            Music music = await db.Musics.FindAsync(model.Id);

            if (music.MusicName != model.MusicName && await db.Musics.Where(m => m.UserId == user.UserId && m.MusicName == model.MusicName).FirstOrDefaultAsync() != null)
            {
                return(Ok(new { msg = $"У вас уже есть запись с названием {model.MusicName}" }));
            }
            string dateTimeNow = DateTime.Now.ToString();

            try
            {
                if (CloudStorageAccount.TryParse(storageConfig.Value.ConnectionString, out CloudStorageAccount storageAccount))
                {
                    CloudBlobClient    blobClient = storageAccount.CreateCloudBlobClient();
                    CloudBlobContainer container  = blobClient.GetContainerReference(storageConfig.Value.ContainerName);
                    if (model.MusicFile != null)
                    {
                        musicFileName = $"{user.Login}_{dateTimeNow}_" + model.MusicFile.FileName;
                        CloudBlockBlob musicBlockBlob = container.GetBlockBlobReference(music.MusicFileName);
                        await musicBlockBlob.DeleteIfExistsAsync();

                        musicBlockBlob = container.GetBlockBlobReference(musicFileName);
                        await musicBlockBlob.UploadFromStreamAsync(model.MusicFile.OpenReadStream());

                        music.MusicFileName = musicFileName;
                    }
                    if (model.MusicImageFile != null)
                    {
                        imageFileName = $"{user.Login}_music_{dateTimeNow}_" + model.MusicImageFile.FileName;
                        CloudBlockBlob imageBlockBlob = container.GetBlockBlobReference(music.MusicImageName);
                        await imageBlockBlob.DeleteIfExistsAsync();

                        imageBlockBlob = container.GetBlockBlobReference(imageFileName);
                        await imageBlockBlob.UploadFromStreamAsync(model.MusicImageFile.OpenReadStream());

                        music.MusicImageName = imageFileName;
                    }
                    music.MusicName    = model.MusicName;
                    music.MusicGenreId = model.MusicGenreId;
                    await db.SaveChangesAsync();

                    return(Ok(new { msg = "" }));
                }
            }
            catch { }
            return(StatusCode(500));
        }
コード例 #2
0
        public async Task <IActionResult> EditMusic(EditMusicModel model, int UserId)
        {
            string musicFileName, imageFileName;
            User   user = await db.Users.FindAsync(UserId);

            Music music = await db.Musics.FindAsync(model.Id);

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

            try
            {
                if (model.MusicFile != null)
                {
                    musicFileName  = $"{user.Login}_{dateTimeNow}_" + model.MusicFile.FileName;
                    music.MusicUrl = await cloudService.EditFile("", music.MusicFileName, "", musicFileName, model.MusicFile.OpenReadStream());

                    music.MusicFileName = musicFileName;
                }
                if (model.MusicImageFile != null)
                {
                    imageFileName = $"{user.Login}_music_{dateTimeNow}_" + model.MusicImageFile.FileName;
                    if (music.MusicImageName != $"{options.Value.DefaultMusicImageFile}")
                    {
                        music.MusicImageUrl = await cloudService.EditFile("", music.MusicImageName, "", imageFileName, model.MusicImageFile.OpenReadStream());

                        music.MusicImageName = imageFileName;
                    }
                    else
                    {
                        music.MusicImageUrl = await cloudService.AddFile("", imageFileName, model.MusicImageFile.OpenReadStream());

                        music.MusicImageName = imageFileName;
                    }
                }
                music.MusicName    = model.MusicName;
                music.MusicGenreId = model.MusicGenreId;
                await db.SaveChangesAsync();

                return(new OkResult());
            }
            catch
            {
                return(new StatusCodeResult(500));
            }
        }
コード例 #3
0
ファイル: MusicController.cs プロジェクト: dmitr12/Diplom
        public async Task <IActionResult> EditMusic([FromForm] EditMusicModel model)
        {
            string musicFileName, imageFileName;
            User   user = await db.Users.FindAsync(UserId);

            Music music = await db.Musics.FindAsync(model.Id);

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

            try
            {
                if (model.MusicFile != null)
                {
                    musicFileName  = $"{user.Login}_{dateTimeNow}_" + model.MusicFile.FileName;
                    music.MusicUrl = await cloud.EditFile("", music.MusicFileName, "", musicFileName, model.MusicFile.OpenReadStream());

                    music.MusicFileName = musicFileName;
                }
                if (model.MusicImageFile != null)
                {
                    imageFileName = $"{user.Login}_music_{dateTimeNow}_" + model.MusicImageFile.FileName;
                    if (music.MusicImageName != "default.png")
                    {
                        music.MusicImageUrl = await cloud.EditFile("", music.MusicImageName, "", imageFileName, model.MusicImageFile.OpenReadStream());

                        music.MusicImageName = imageFileName;
                    }
                    else
                    {
                        music.MusicImageUrl = await cloud.AddFile("", imageFileName, model.MusicImageFile.OpenReadStream());

                        music.MusicImageName = imageFileName;
                    }
                }
                music.MusicName    = model.MusicName;
                music.MusicGenreId = model.MusicGenreId;
                await db.SaveChangesAsync();

                return(Ok(new { msg = "" }));
            }
            catch
            {
                return(StatusCode(500));
            }
        }
コード例 #4
0
 public IActionResult EditMusic([FromForm] EditMusicModel model)
 {
     return(musicManager.EditMusic(model, UserId).Result);
 }