Exemplo n.º 1
0
        public IEnumerable <PhotoInfoModel> GetPhotosByCategory(int id)
        {
            Category category = Database.CategoryManager.Read(id);

            if (category == null || category.Photos == null)
            {
                return(null);
            }
            List <PhotoInfoModel> photoInfos = new List <PhotoInfoModel>();

            foreach (var photo in category.Photos)
            {
                PhotoInfoModel photoInfo = new PhotoInfoModel()
                {
                    Id             = photo.Id,
                    Description    = photo.Description,
                    IsPrivate      = photo.IsPrivate,
                    Type           = photo.Type,
                    PublishingDate = photo.PublishingDate,
                    LikesCount     = photo.Likes.Count(),
                    CommentsCount  = photo.Comments.Count(),
                    CategoriesIds  = photo.Categories.Select(c => c.Id).ToList(),
                    UserId         = photo.UserProfile.Id
                };
                photoInfos.Add(photoInfo);
            }
            return(photoInfos);
        }
Exemplo n.º 2
0
        public IEnumerable <PhotoInfoModel> GetAllPhotosInfo()
        {
            var photos = Database.PhotoManager.GetInfoAll();

            if (photos == null)
            {
                return(null);
            }
            List <PhotoInfoModel> photoInfoModels = new List <PhotoInfoModel>();

            foreach (var item in photos)
            {
                PhotoInfoModel photoInfo = new PhotoInfoModel()
                {
                    Id             = item.Id,
                    Description    = item.Description,
                    IsPrivate      = item.IsPrivate,
                    Type           = item.Type,
                    PublishingDate = item.PublishingDate,
                    LikesCount     = item.Likes.Count(),
                    CommentsCount  = item.Comments.Count(),
                    CategoriesIds  = item.Categories.Select(c => c.Id).ToList(),
                    UserId         = item.UserProfile.Id
                };
                photoInfoModels.Add(photoInfo);
            }
            return(photoInfoModels);
        }
Exemplo n.º 3
0
        public HttpResponseMessage GetImage(int id)
        {
            byte[]         arr       = PhotoService.GetImage(id);
            PhotoInfoModel photoInfo = PhotoService.GetPhotoInfo(id);

            if (arr != null && photoInfo != null)
            {
                HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new ByteArrayContent(arr)
                };
                response.Content.Headers.ContentType = new MediaTypeHeaderValue(photoInfo.Type);
                return(response);
            }
            else
            {
                return(new HttpResponseMessage(HttpStatusCode.NotFound));
            }
        }
Exemplo n.º 4
0
        public PhotoInfoModel GetPhotoInfo(int id)
        {
            Photo photo = Database.PhotoManager.GetInfo(id);

            if (photo == null)
            {
                return(null);
            }
            PhotoInfoModel photoInfo = new PhotoInfoModel()
            {
                Id             = photo.Id,
                Description    = photo.Description,
                IsPrivate      = photo.IsPrivate,
                Type           = photo.Type,
                PublishingDate = photo.PublishingDate,
                LikesCount     = photo.Likes.Count(),
                CommentsCount  = photo.Comments.Count(),
                CategoriesIds  = photo.Categories.Select(c => c.Id).ToList(),
                UserId         = photo.UserProfile.Id
            };

            return(photoInfo);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 製作上傳圖片的縮圖
        /// </summary>
        /// <returns></returns>
        public ActionResult GetPhotoInfo()
        {
            string[] rtn = new string[] { "", "", "" };

            if (Request.HttpMethod == "POST")
            {
                var uaParser = JsonConvert.DeserializeObject <UAParserModel>(Request["UAParser"]);
                var files    = Request.Files;
                var fileLen  = files.AllKeys.Count();

                if (fileLen == 0)
                {
                    rtn[0] = "無任何上傳檔案 !";
                    return(this.ToJsonNet(rtn));
                }

                var now       = DateTime.Now;
                var t         = new PhotoInfoModel();
                var sw        = new Stopwatch();
                var photoList = new List <int>();
                sw.Start();
                for (var i = 0; i < fileLen; i++)
                {
                    using (var scope = new TransactionScope()) {
                        using (var db = new SqlConnection(this.SettingsContext.GetValue("Test"))) {
                            var file = files[i];
                            t.FileExt  = Path.GetExtension(file.FileName);
                            t.FileName = Path.GetFileNameWithoutExtension(file.FileName);

                            Stream fs          = file.InputStream;
                            Image  img         = null;
                            var    guid        = Guid.NewGuid();
                            var    maxPix      = AppConfig.ThumbnailPixel;
                            var    path        = Server.MapPath($"~/Temp/{guid}.jpg");
                            var    thbPath     = Server.MapPath($"~/Temp/{guid}_{maxPix}.jpg");
                            var    thb1920Path = Server.MapPath($"~/Temp/{guid}_1920.jpg");

                            try {
                                using (Stream stream = new FileStream(path, FileMode.Create)) {
                                    if (t.FileExt.ToLower() == ".heic")
                                    {
                                        // heic to jpg
                                        using (MagickImage mag = new MagickImage(fs)) {
                                            mag.Write(stream, MagickFormat.Jpg);
                                        }
                                        img = Image.FromStream(stream, true, true);
                                    }
                                    else
                                    {
                                        img = Image.FromStream(fs, true, true);
                                    }

                                    PhotoUtility.PhotoExif(ref img, ref t);
                                    // Origin
                                    img.Save(stream, ImageFormat.Jpeg);
                                    // Thumbnail
                                    PhotoUtility.MakeThumbnail(stream, thbPath, maxPix.FixInt());
                                    // Thumbnail
                                    PhotoUtility.MakeThumbnail(stream, thb1920Path, 1920);
                                }

                                // 主檔
                                var p = new PhotoModel();
                                p.CreateDateTime    = now;
                                p.FileDesc          = "";
                                p.FileExt           = t.FileExt;
                                p.FileName          = t.FileName;
                                p.OrgCreateDateTime = t.OrgCreatDateTime;
                                p.OrgModifyDateTime = t.OrgModifyDateTime;
                                p.ModifyDateTime    = t.OrgModifyDateTime;
                                p.Orientation       = t.Orientation;
                                p.Location          = t.Location;
                                p.Width             = t.Width;
                                p.Height            = t.Height;
                                p.Person            = "";
                                p.CreateDateTime    = now;
                                p.ModifyDateTime    = now;
                                db.Insert(p);

                                // 原圖
                                var orgPhoto = new PhotoDetailModel();
                                orgPhoto.PhotoDetailSN  = p.PhotoSN;
                                orgPhoto.CreateDateTime = now;
                                orgPhoto.Photo          = Files.ReadBinaryFile(path);
                                orgPhoto.Type           = "Origin";
                                db.Insert(orgPhoto);
                                Files.RenameFile(path, path.Replace(guid.ToString(), p.PhotoSN.ToString()));
                                path = path.Replace(guid.ToString(), p.PhotoSN.ToString());

                                // 縮圖
                                var thumbPhoto = new PhotoDetailModel();
                                thumbPhoto.PhotoDetailSN  = p.PhotoSN;
                                thumbPhoto.CreateDateTime = now;
                                thumbPhoto.Photo          = Files.ReadBinaryFile(thbPath);
                                thumbPhoto.Type           = "Thumbnail";
                                db.Insert(thumbPhoto);
                                Files.RenameFile(thbPath, thbPath.Replace(guid.ToString(), p.PhotoSN.ToString()));
                                thbPath = thbPath.Replace(guid.ToString(), p.PhotoSN.ToString());

                                // 縮圖 1920
                                var thumbPhoto1920 = new PhotoDetailModel();
                                thumbPhoto1920.PhotoDetailSN  = p.PhotoSN;
                                thumbPhoto1920.CreateDateTime = now;
                                thumbPhoto1920.Photo          = Files.ReadBinaryFile(thb1920Path);
                                thumbPhoto1920.Type           = "Thumbnail";
                                db.Insert(thumbPhoto1920);
                                Files.RenameFile(thb1920Path, thb1920Path.Replace(guid.ToString(), p.PhotoSN.ToString()));
                                thb1920Path = thb1920Path.Replace(guid.ToString(), p.PhotoSN.ToString());

                                //t.Url = "data:image/" + t.FileExt.Replace(".", "") + ";base64," + Convert.ToBase64String(Files.ReadBinaryFile(path));
                                t.Thumbnail = "data:image/" + t.FileExt.Replace(".", "") + ";base64," + Convert.ToBase64String(thumbPhoto1920.Photo);

                                var percent = ((double)(i + 1) / (double)fileLen * 100);
                                percent = percent > 99 ? 99 : percent;
                                ProgressHub.SendMessage(Request["Selector"], percent, "", Request["ConnId"], i, t.Thumbnail);

                                photoList.Add(p.PhotoSN);
                            } catch (Exception e) {
                                this.LogContext.LogRepoistory.SysLog(e);
                                rtn[0] += $"{t.FileName} - {e.Message} <br>";
                                Files.DelBulkFiles(new string[] { path, thbPath, thb1920Path });
                                ProgressHub.SendMessage(Request["Selector"], 0, e.Message, Request["ConnId"], i, "");
                                continue;
                            } finally {
                                if (img != null)
                                {
                                    img.Dispose();
                                }
                                img = null;
                            }
                        }
                        scope.Complete();
                    }
                }

                if (rtn[0] == "")
                {
                    // 上傳記錄
                    var log = new UploadLog();
                    log.Browser        = $"{uaParser.BrowserName}  {uaParser.BrowserVersion}";
                    log.Device         = $"{uaParser.DeviceType}  {uaParser.DeviceVendor}";
                    log.OS             = $"{uaParser.OsName}  {uaParser.OsVersion}";
                    log.LocalIP        = "";
                    log.TotalNum       = fileLen;
                    log.Type           = "Photo";
                    log.CompletedTime  = sw.Elapsed.ToString("mm\\:ss\\.ff");
                    log.CreateDateTime = now;
                    log.PhotoSNList    = photoList.ToArray().Join(",");

                    using (var db = new SqlConnection(this.SettingsContext.GetValue("Test"))) {
                        db.Insert(log);
                    }
                }

                ProgressHub.SendMessage(Request["Selector"], 100, "", Request["ConnId"], fileLen, "");
            }

            return(this.ToJsonNet(rtn));
        }
Exemplo n.º 6
0
        /// <summary>
        /// 取得Exif, 檢查ios圖片長寬問題
        /// </summary>
        /// <param name="fs"></param>
        /// <param name="fileName"></param>
        /// <param name="fileExt"></param>
        /// <returns></returns>
        public static void PhotoExif(ref Image image, ref PhotoInfoModel t)
        {
            Encoding ascii = Encoding.ASCII;
            string   picDate;
            short    orientation = 0;
            var      now         = DateTime.Now;

            t.OrgModifyDateTime = now;
            t.OrgCreatDateTime  = now;
            t.Location          = "";

            try {
                var items = image.PropertyItems.Where(w => new int[] { 1, 2, 3, 4, 36867, 306, 274, 5029 }.Contains(w.Id)).Select(s => s);
                if (items.Count() > 0)
                {
                    var latitude  = ExifGpsToDouble(items.Where(w => w.Id == 1).FirstOrDefault(), items.Where(w => w.Id == 2).FirstOrDefault());
                    var longitude = ExifGpsToDouble(items.Where(w => w.Id == 3).FirstOrDefault(), items.Where(w => w.Id == 4).FirstOrDefault());
                    t.Location = $"{latitude},{longitude}";

                    var p = items.Where(w => w.Id == 306).FirstOrDefault();

                    // 拍摄更新日期
                    if (p != null)
                    {
                        picDate = ascii.GetString(p.Value);
                        if ((!"".Equals(picDate)) && picDate.Length >= 10)
                        {
                            picDate             = Regex.Replace(picDate.Substring(0, 10), "[:,-]", "/");
                            t.OrgModifyDateTime = Convert.ToDateTime(picDate);
                        }
                    }

                    p = items.Where(w => w.Id == 36867).FirstOrDefault();
                    if (p != null)
                    {
                        // 拍摄建立日期
                        picDate = ascii.GetString(p.Value);
                        if ((!"".Equals(picDate)) && picDate.Length >= 10)
                        {
                            picDate             = Regex.Replace(picDate.Substring(0, 10), "[:,-]", "/");
                            t.OrgCreatDateTime  = Convert.ToDateTime(picDate);
                            t.OrgModifyDateTime = Convert.ToDateTime(picDate);
                        }
                    }

                    p = items.Where(w => w.Id == 274).FirstOrDefault();
                    if (p != null)
                    {
                        orientation = BitConverter.ToInt16(p.Value, 0);
                        switch (orientation)
                        {
                        case 1:
                            image.RotateFlip(RotateFlipType.RotateNoneFlipNone);
                            break;

                        case 2:
                            image.RotateFlip(RotateFlipType.RotateNoneFlipX);    //horizontal flip
                            break;

                        case 3:
                            image.RotateFlip(RotateFlipType.Rotate180FlipNone);    //right-top
                            break;

                        case 4:
                            image.RotateFlip(RotateFlipType.RotateNoneFlipY);    //vertical flip
                            break;

                        case 5:
                            image.RotateFlip(RotateFlipType.Rotate90FlipX);
                            break;

                        case 6:
                            image.RotateFlip(RotateFlipType.Rotate90FlipNone);    //right-top
                            break;

                        case 7:
                            image.RotateFlip(RotateFlipType.Rotate270FlipX);
                            break;

                        case 8:
                            image.RotateFlip(RotateFlipType.Rotate270FlipNone);    //left-bottom
                            break;
                        }
                        p.Value[0] = 1;
                        image.SetPropertyItem(p);
                    }
                }

                t.Width       = image.Width;
                t.Height      = image.Height;
                t.Orientation = orientation;
            } catch (Exception e) {
                throw e;
            }
        }