public string Insert(cms_Merchant_Folder_Info item)
 {
     using (IDbConnection db = Helpers.OrmliteConnection.openConn())
     {
         using (var dbTrans = db.OpenTransaction(IsolationLevel.ReadCommitted))
         {
             try
             {
                 var checkID = db.FirstOrDefault <cms_Merchant_Folder_Info>(s => s.ten_thu_muc == item.ten_thu_muc && s.ma_gian_hang == item.ma_gian_hang);
                 if (checkID != null)
                 {
                     return("Folder was exists");
                 }
                 else
                 {
                     item.ngay_tao = DateTime.Now;
                     db.Insert(item);
                 }
                 dbTrans.Commit();
                 return("true");
             }
             catch (Exception e)
             {
                 dbTrans.Rollback();
                 return(e.Message.ToString());
             }
         }
     }
 }
Esempio n. 2
0
        public string UploadImageToFolder(string FolderName, HttpPostedFileBase file, string UserName, string ma_website)
        {
            using (IDbConnection db = Helpers.OrmliteConnection.openConn())
            {
                string duong_dan_day_du = string.Empty;
                if (FolderName != "" || FolderName != null)
                {
                    var checkID = db.FirstOrDefault <cms_Merchant_Folder_Info>(s => s.ten_thu_muc == FolderName);
                    if (checkID == null)
                    {
                        cms_Merchant_Folder_Info itemcms = new cms_Merchant_Folder_Info();
                        itemcms.ma_gian_hang   = "All";
                        itemcms.ten_thu_muc    = FolderName;
                        itemcms.nguoi_cap_nhat = itemcms.nguoi_tao = UserName;
                        itemcms.ngay_tao       = itemcms.ngay_cap_nhat = DateTime.Now;
                        new cms_Merchant_Folder_Info_DAO().Insert(itemcms);
                    }
                }
                List <string>     listPath = new List <string>();
                List <Image_Info> list     = new List <Image_Info>();
                string            idref    = UserName + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff");
                var fileName = Path.GetFileNameWithoutExtension(file.FileName);
                var img      = Image.FromStream(file.InputStream, true, true);

                var listType      = new List <Code_Master>();
                var listExtension = listType;
                listType      = db.Select <Code_Master>(s => s.Type == "IMGResizeType");
                listExtension = db.Select <Code_Master>(s => s.Type == "IMGResizeFormat");
                //if (listType.Count == 0)
                //    return "Không tìm thấy cấu hình";
                foreach (Code_Master type in listType)
                {
                    var        refix = idref;
                    Image_Info item  = new Image_Info();
                    img = AutoResize(img, type.Value);
                    //if (img == null)
                    //    return "Không tìm thấy cấu hình";

                    var    i               = 0;
                    string FolderPath      = System.Web.HttpContext.Current.Server.MapPath("~/Images/Merchant_Image_Info/All" + "/" + FolderName + "/");
                    var    destinationPath = Path.Combine(FolderPath, refix);

                    if (!Directory.Exists(FolderPath))
                    {
                        Directory.CreateDirectory(FolderPath);
                    }
                    var    lastRefix = "_" + img.Width.ToString() + "x" + img.Height.ToString();
                    string extension = ".jpg";
                    foreach (var ex in listExtension)
                    {
                        if (ex.Value == type.Value)
                        {
                            extension = ex.Name;
                            break;
                        }
                    }
                    while (System.IO.File.Exists(destinationPath + i.ToString() + lastRefix + extension))
                    {
                        i++;
                    }
                    destinationPath += i.ToString() + lastRefix + extension;
                    img.Save(destinationPath);

                    listPath.Add(destinationPath);
                    item.url              = refix + i.ToString() + lastRefix + extension;
                    item.ma_anh_goc       = idref;
                    item.chieu_rong       = img.Width;
                    item.chieu_cao        = img.Height;
                    item.dung_luong       = new FileInfo(destinationPath).Length;
                    item.thu_muc          = "All" + "/" + FolderName + "/";
                    item.duong_dan_day_du = new AzureHelper().UploadFile("All", item.url, destinationPath);
                    item.loai             = Int16.Parse(type.Value);
                    item.trang_thai       = "Active";
                    item.trang_thai_duyet = "New";
                    item.ten_anh          = fileName;
                    item.mo_ta            = item.mo_ta_khong_dau = "";
                    item.ma_website       = ma_website;
                    list.Add(item);

                    if (type.Name == "Small")
                    {
                        duong_dan_day_du = item.duong_dan_day_du;
                    }
                }

                string result = new Image_Info().UpSert(list, UserName);

                if (result != "true")
                {
                    foreach (var path in listPath)
                    {
                        if (System.IO.File.Exists(path))
                        {
                            System.IO.File.Delete(path);
                        }
                    }
                }
                return(duong_dan_day_du);
            }
        }