예제 #1
0
파일: Form1.cs 프로젝트: uvbs/mmp
        private void ToOss3(DataTable dt)
        {
            StringBuilder sb = new StringBuilder();

            foreach (DataRow dr in dt.Rows)
            {
                try
                {
                    string htmlField = dr[txt2].ToString();
                    if (string.IsNullOrWhiteSpace(htmlField))
                    {
                        continue;
                    }
                    List <string> srcList = MyRegex.GetPadImg(htmlField);
                    if (srcList.Count == 0)
                    {
                        continue;
                    }

                    foreach (var item in srcList)
                    {
                        string oldFile  = MyRegex.GetPadSrcUrl(item);
                        string oldFile1 = "";
                        if (oldFile.StartsWith(OssHelper.GetDomain()))
                        {
                            SetTextErrorMessage("已经在Oss服务器:" + oldFile);
                            continue;
                        }
                        else if (oldFile.StartsWith("/"))
                        {
                            oldFile1 = oldFile;
                        }
                        else
                        {
                            bool   haveLocalSrc = false;
                            string nowWebDomain = "";
                            for (int i = 0; i < txtLikeWebDomain.Length; i++)
                            {
                                if (oldFile.IndexOf(txtLikeWebDomain[i]) > 0)
                                {
                                    haveLocalSrc = true;
                                    nowWebDomain = txtLikeWebDomain[i];
                                    break;
                                }
                            }
                            if (!haveLocalSrc)
                            {
                                SetTextErrorMessage("不是本站图片:" + oldFile);
                                continue;
                            }
                            oldFile1 = oldFile.Substring(oldFile.IndexOf(nowWebDomain) + nowWebDomain.Length - 1);
                        }
                        SetTextErrorMessage(oldFile1);
                        string  url = "";
                        DataSet ds  = DbHelperSQL.Query(string.Format("SELECT TOP 1 OldPath,NewPath FROM {0} WHERE OldPath='{1}' ", "FileToOssLog", oldFile1));
                        if (ds != null && ds.Tables.Count != 0 && ds.Tables[0].Rows.Count == 1)
                        {
                            url = ds.Tables[0].Rows[0]["NewPath"].ToString();
                        }
                        if (string.IsNullOrWhiteSpace(url))
                        {
                            ds = DbHelperSQL.Query(string.Format("SELECT TOP 1 OldPath,NewPath FROM {0} WHERE OldPath='{1}' ", "FileToOssLog", oldFile));
                            if (ds != null && ds.Tables.Count != 0 && ds.Tables[0].Rows.Count == 1)
                            {
                                url = ds.Tables[0].Rows[0]["NewPath"].ToString();
                            }
                        }
                        if (string.IsNullOrWhiteSpace(url))
                        {
                            string LocalPath = txt5 + oldFile1.Replace("/", @"\");
                            string extension = Path.GetExtension(oldFile1).ToLower();
                            if (!File.Exists(LocalPath))
                            {
                                SetTextErrorMessage("文件不存在:" + LocalPath);
                                continue;
                            }
                            byte[] fileByte = File.ReadAllBytes(LocalPath);
                            if (fileByte.Length == 0)
                            {
                                SetTextErrorMessage("文件大小为空:" + LocalPath);
                                continue;
                            }
                            url = OssHelper.UploadFileFromByte(OssHelper.GetBucket(""), baseDir + oldFile1, fileByte, extension);
                        }

                        DataSet ds1 = DbHelperSQL.Query(
                            string.Format("SELECT 1 FROM {0} WHERE [TableKeyID]='{1}' AND [TableName]='{2}' AND [FieldName]='{3}' AND [OldPath]='{4}'"
                                          , "FileToOssLog", dr[txt4].ToString(), txt1, txt2, oldFile));

                        if (ds1 != null && ds1.Tables.Count != 0 && ds1.Tables[0].Rows.Count == 0)
                        {
                            sb.AppendFormat("INSERT INTO {0} ([TableKeyID],[TableName],[FieldName],[OldPath],[NewPath]) ", "FileToOssLog");
                            sb.AppendFormat("VALUES ('{0}','{1}','{2}','{3}','{4}') ;", dr[txt4].ToString(), txt1, txt2, oldFile, url);
                        }
                    }
                }
                catch (Exception ex)
                {
                    SetTextErrorMessage(ex.Message);
                }
                finally
                {
                    now++;
                    SetTextMessage();
                }
            }
            try
            {
                if (!string.IsNullOrWhiteSpace(sb.ToString()))
                {
                    DbHelperSQL.ExecuteSql(sb.ToString());
                }
            }
            catch (Exception ex)
            {
                SetTextErrorMessage(ex.Message);
            }
        }