Пример #1
0
        public ActionResult BatchUpload()
        {
            bool isSavedSuccessfully = true;
            int count = 0;
            string msg = "";

            string fileName = "";
            string fileExtension = "";
            string filePath = "";
            string fileNewName = "";

            int albumId = string.IsNullOrEmpty(Request.Params["hidAlbumId"]) ?
                0 : int.Parse(Request.Params["hidAlbumId"]);

            try
            {
                string directoryPath = Server.MapPath("~/Content/photos");
                if (!Directory.Exists(directoryPath))
                    Directory.CreateDirectory(directoryPath);

                foreach (string f in Request.Files)
                {
                    HttpPostedFileBase file = Request.Files[f];

                    if (file != null && file.ContentLength > 0)
                    {
                        fileName = file.FileName;
                        fileExtension = Path.GetExtension(fileName);
                        fileNewName = Guid.NewGuid().ToString() + fileExtension;
                        filePath = Path.Combine(directoryPath, fileNewName);
                        file.SaveAs(filePath);

                        Photo model = new Photo();
                        model.Title =Path.GetFileNameWithoutExtension(fileName);
                        model.PhotoPath = fileNewName;
                        model.CreateTime = DateTime.Now;
                        model.AlbumId = albumId;
                        unitOfWork.PhotoRepository.Insert(model);

                        count++;
                    }
                }

                unitOfWork.Save();
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                isSavedSuccessfully = false;
            }

            return Json(new
            {
                Result = isSavedSuccessfully,
                Count = count,
                Message = msg
            });
        }
        public ActionResult Create(Photo photo)
        {
            if (ModelState.IsValid)
            {
                photo.CreateTime = DateTime.Now;
                unitOfWork.PhotoRepository.Insert(photo);
                unitOfWork.Save();
                return RedirectToAction("Index");
            }

            ViewBag.AlbumId = new SelectList(
                unitOfWork.AlbumRepository.Get(),
                "AlbumId",
                "Name",
                photo.AlbumId);
            return View(photo);
        }
Пример #3
0
        public bool Append(Photo photo, string uploadingPath, HttpPostedFileBase file)
        {
            if (photo == null)
                throw new ArgumentNullException("photo");
            if (file == null)
                throw new ArgumentNullException("file");

            photo.FileName = InitializeSavePath(uploadingPath, Path.GetFileName(file.FileName));

            var path = Path.Combine(uploadingPath, photo.FileName);

            // ファイルをローカルに保存
            // ※非同期でクラウドに転送するよ
            file.SaveAs(path);

            photo.TakenAt = ImageResizer.GetExifTakenAt(path);
            photo.EntryAt = DateTime.Now;
            photo.ContentType = file.ContentType;
            photo.Length = file.ContentLength;
            photo.Description = (photo.Description ?? "").Trim();
            photo.DeleteCode = GetStringHash(photo.DeleteCode);

            _repository.Append(photo);
            return _repository.SaveChanges() != 0;
        }
Пример #4
0
        public Stream GetImage(Photo photo, ResizeMode type, int size)
        {
            // クラウドに送り込んだ?
            if (!photo.IsUploaded)
                return null;

            // 処理中?
            //if (!_processing.Lock(photo.Id))
            //{
            //    var dump = string.Format("[id = {0}, type = {1}, size = {2}]", photo.Id, type, size);
            //    Trace.WriteLine(" can't get lock - " + dump + " : " + DateTime.Now);
            //    return null;
            //}

            // ファイルがあるなら即返す
            var loadName = GenerateFileName(photo.FileName, type, size);
            var loadPath = CacheFullPath(loadName);
            var entry = new StorageEntry(photo);
            var resizer = new ImageResizer();
            if (!File.Exists(loadPath))
            {
                // キャッシュになければStorageから取り出す。
                var cacheName = RetrieveCache(photo.FileName);
                if (string.IsNullOrEmpty(cacheName))
                    return null;

                // 最適化して回転する
                var optName = GenerateFileName(photo.FileName, ResizeMode.Optimize);
                var optPath = CacheFullPath(optName);
                resizer.Optimize(cacheName, optPath);

                // ローカルキャッシュにリサイズしたものを取得
                GenerateResize(optPath, loadName, type, size);
            }
            //_processing.Unlock(photo.Id);

            var bytes = entry.Load(CachePath, loadName);

            return bytes == null ? null : new MemoryStream(bytes);
        }
 public ActionResult Edit(Photo photo)
 {
     if (ModelState.IsValid)
     {
         unitOfWork.PhotoRepository.Update(photo);
         unitOfWork.Save();
         return RedirectToAction("Index");
     }
     ViewBag.AlbumId = new SelectList(
         unitOfWork.AlbumRepository.Get(),
         "AlbumId",
         "Name",
         photo.AlbumId);
     return View(photo);
 }
Пример #6
0
        private static Photo InternalSaveMultipart(string uploadingPath, Multipart multipart, string name)
        {
            var photo = new Photo
                            {
                                FileName = PhotosService.InitializeSavePath(uploadingPath, name)
                            };

            var path = Path.Combine(uploadingPath, photo.FileName);

            multipart.SaveToFile(path);

            photo.TakenAt = ImageResizer.GetExifTakenAt(path);
            photo.EntryAt = DateTime.Now;
            photo.ContentType = multipart.Body.ContentType;
            photo.Length = multipart.Length;

            return photo;
        }
Пример #7
0
        public ActionResult UploadPhoto(Photo model)
        {
            if (ModelState.IsValid)
            {
                string name = SavePhoto();

                if (ModelState.IsValid)
                {
                    model.CreateTime = DateTime.Now;
                    model.PhotoPath = name;
                    unitOfWork.PhotoRepository.Insert(model);
                    unitOfWork.Save();

                    return RedirectToAction("Index", "PhotoManager");
                }
            }

            return View(model);
        }
Пример #8
0
 public StorageEntry(Photo photo)
 {
     Name = photo.FileName;
     ContentType = photo.ContentType;
 }