示例#1
0
 private void BulutaYukle(FotografYazDto dto)
 {
     try
     {
         var dosya         = dto.File;
         var yuklemeSonucu = new ImageUploadResult();
         if (dosya.Length > 0)
         {
             using (var stream = dosya.OpenReadStream())
             {
                 var yuklemePrametreleri = new ImageUploadParams()
                 {
                     File           = new FileDescription(dosya.Name, stream),
                     Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face")
                 };
                 yuklemeSonucu = cloudinary.Upload(yuklemePrametreleri);
             }
         }
         dto.Url      = yuklemeSonucu.Uri.ToString();
         dto.PublicId = yuklemeSonucu.PublicId;
     }
     catch (Exception hata)
     {
     }
 }
        private async Task <string> LokaldeKaydet(FotografYazDto dto)
        {
            var yuklemeYolu = Path.Combine(host.WebRootPath, "Resimler");

            if (!Directory.Exists(yuklemeYolu))
            {
                Directory.CreateDirectory(yuklemeYolu);
            }
            var dosyaAdi  = Guid.NewGuid().ToString() + Path.GetExtension(dto.File.FileName);
            var dosyaYolu = Path.Combine(yuklemeYolu, dosyaAdi);

            using (var stream = new FileStream(dosyaYolu, FileMode.Create))
            {
                await dto.File.CopyToAsync(stream);
            }

            return(dosyaAdi);
        }
示例#3
0
 public static KisiFoto ToEntity(this FotografYazDto dto)
 {
     return(dto == null ? null : Mapper.Map <KisiFoto>(dto));
 }
        public async Task <IActionResult> KullaniciyaFotografEkle(int kullaniciNo, FotografYazDto dto)
        {
            return(await KullaniciVarsaCalistir <IActionResult>(async() =>
            {
                if (dto.File == null)
                {
                    return BadRequest("Fotoğraf yok!");
                }
                if (dto.File.Length == 0)
                {
                    return BadRequest("Fotoğraf dosyası boş!");
                }
                if (dto.File.Length > fotografAyarlari.MaxBytes)
                {
                    return BadRequest($"Dosya çok büyük. En fazla {fotografAyarlari.MaxBytes / (1024 * 1024)} MB resim yükleyebilirsiniz");
                }
                if (!fotografAyarlari.DosyaTipUygunmu(dto.File.FileName))
                {
                    return BadRequest("Dosya tipini yükleme izni yok! Sadece resim dosyalarını yükleyebilirsiniz.");
                }

                var kullanici = await repo.BulAsync(kullaniciNo);
                if (kullanici == null)
                {
                    return BadRequest("Kullanıcı bulunamadı!");
                }

                if (aktifKullaniciNo != kullanici.Id)
                {
                    return Unauthorized();
                }
                string dosyaAdi = string.Empty;
                if (!fotografAyarlari.SadeceBulutaYukle)
                {
                    dosyaAdi = await LokaldeKaydet(dto);
                }

                if (!fotografAyarlari.SadeceLokaldeTut)
                {
                    BulutaYukle(dto);
                }
                try
                {
                    var foto = dto.ToEntity();
                    foto.Kisi = kullanici.Kisi;

                    if (!kullanici.Kisi.Fotograflari.Any(m => m.ProfilFotografi))
                    {
                        foto.ProfilFotografi = true;
                    }

                    foto.DosyaAdi = dosyaAdi;
                    kullanici.Kisi.Fotograflari.Add(foto);
                    if (await repo.KaydetAsync())
                    {
                        return CreatedAtRoute("KullaniciFotografiniAl", new { id = foto.Id }, foto.ToFotoOkuDto());
                    }
                }
                catch (Exception hata)
                {
                    return BadRequest("Fotoğraf eklenemedi!");
                }
                return BadRequest("Fotoğraf eklenemedi!");
            }));
        }