Exemplo n.º 1
0
        /// <summary>
        /// Upload image to hosting.
        /// </summary>
        /// <param name="db">DataContext</param>
        /// <param name="objectId">Object ID</param>
        /// <param name="objectType">Object type</param>
        /// <param name="imageInfoEntity">Image info entity</param>
        /// <returns>Ok: true, Fail: false</returns>
        public static bool UploadImage(AloaiDataContext db, decimal objectId, ImageType objectType, List <ImageInfoEntity> imageInfoEntity)
        {
            if (imageInfoEntity == null || imageInfoEntity.Count == 0)
            {
                if (!RemoveImage(db, objectId, objectType))
                {
                    return(false);
                }

                return(true);
            }

            int index = 0;

            var delQuery = from d in db.M_IMAGE_DETAILS
                           where d.OBJECT_ID == objectId &&
                           d.OBJECT_TYPE == (int)objectType
                           select d;

            // Delete image not used.
            if (delQuery.Any())
            {
                List <M_IMAGE_DETAIL> list = delQuery.ToList();

                foreach (M_IMAGE_DETAIL del in list)
                {
                    var exists = from d in imageInfoEntity
                                 where Path.GetFileName(d.path.Trim('\"')) == del.IMAGE_NAME
                                 select d;

                    if (!exists.Any())
                    {
                        string path     = $"Upload/";
                        string fileName = Path.GetFileName(del.IMAGE_PATH);

                        string directoryName = AppDomain.CurrentDomain.BaseDirectory + path;
                        string ext           = Path.GetExtension(del.IMAGE_PATH);

                        fileName = System.IO.Path.Combine(directoryName, fileName) + ext;

                        // Check if file exists with its full path
                        if (File.Exists(fileName))
                        {
                            // If file found, delete it.
                            File.Delete(fileName);
                        }
                    }

                    db.M_IMAGE_DETAILS.Remove(del);
                }
            }

            foreach (ImageInfoEntity imageInfo in imageInfoEntity)
            {
                if (!string.IsNullOrEmpty(imageInfo.path))
                {
                    continue;
                }

                index++;
                var thisFileName = Path.GetFileName(imageInfo.path.Trim('\"'));

                M_IMAGE_DETAIL image = new M_IMAGE_DETAIL();
                image.OBJECT_ID   = objectId;
                image.OBJECT_TYPE = (int)objectType;
                image.ROW_NO      = index;
                image.IMAGE_NAME  = Path.GetFileName(imageInfo.path);
                image.IMAGE_PATH  = imageInfo.path;

                db.M_IMAGE_DETAILS.Add(image);
            }

            db.SaveChanges();
            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Upload image to hosting.
        /// </summary>
        /// <param name="db">DataContext</param>
        /// <param name="objectId">Object ID</param>
        /// <param name="objectType">Object type</param>
        /// <param name="imageInfoEntity">Image info entity</param>
        /// <returns>Ok: true, Fail: false</returns>
        public static bool UploadAvatar(AloaiDataContext db, decimal objectId, ImageInfoEntity avatar, string imagePath, out string avartaPath)
        {
            ImageType objectType = ImageType.Avatar;

            avartaPath = avatar.path;

            if (avatar != null && !string.IsNullOrEmpty(avatar.path))
            {
                string path     = $"Upload/";
                string fileName = Path.GetFileName(avatar.path);

                string directoryName = AppDomain.CurrentDomain.BaseDirectory + path;
                string ext           = Path.GetExtension(avatar.path);

                fileName = System.IO.Path.Combine(directoryName, fileName) + ext;

                if (File.Exists(fileName))
                {
                    var delQuery = from d in db.M_IMAGE_DETAILS
                                   where d.OBJECT_ID == objectId &&
                                   d.OBJECT_TYPE == (int)objectType
                                   select d;

                    // Delete image not used.
                    if (delQuery.Any())
                    {
                        M_IMAGE_DETAIL list = delQuery.Single();

                        if (avatar.path == list.IMAGE_PATH)
                        {
                            return(true);
                        }

                        // Check if file exists with its full path
                        if (File.Exists(list.IMAGE_PATH))
                        {
                            // If file found, delete it.
                            File.Delete(list.IMAGE_PATH);
                        }

                        db.M_IMAGE_DETAILS.Remove(list);
                    }

                    if (avatar.image != null)
                    {
                        var thisFileName = Path.GetFileName(avatar.path.Trim('\"'));

                        M_IMAGE_DETAIL image = new M_IMAGE_DETAIL();
                        image.OBJECT_ID   = objectId;
                        image.OBJECT_TYPE = (int)objectType;
                        image.ROW_NO      = 1;
                        image.IMAGE_NAME  = Path.GetFileName(thisFileName);
                        image.IMAGE_PATH  = avatar.path;
                        avartaPath        = avatar.path;
                        db.M_IMAGE_DETAILS.Add(image);
                    }

                    db.SaveChanges();
                }
            }

            return(true);
        }