Esempio n. 1
0
        /// <summary>
        /// Creates the specified file.
        /// </summary>
        /// <param name="file">The file.</param>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        public Entities.File Create(Entities.File file, byte[] data)
        {
            var id           = Guid.NewGuid();
            var subFolder    = (file.FileType.ToLower() == "image" ? "img/" : "doc/") + (file.AccountId.HasValue ? file.AccountId.ToString() : "common");
            var fullFolder   = Path.Combine(_path, subFolder);
            var extName      = Path.GetExtension(file.FileName)?.ToLower();
            var filePath     = Path.Combine(subFolder, id + extName);
            var fullFilePath = Path.Combine(fullFolder, id + extName);

            if (!Directory.Exists(fullFolder))
            {
                Directory.CreateDirectory(fullFolder);
            }

            using (var stream = File.Create(fullFilePath))
            {
                stream.Write(data, 0, data.Length);
                stream.Flush();
                stream.Close();
            }

            // Data access component declarations.
            var fileDAC = new FileDAC();

            // Step 1 - Calling Create on FileDAC.
            file.Id       = id;
            file.FilePath = filePath;
            file.MimeType = FileContentType.GetMimeType(extName);

            fileDAC.Create(file);
            return(file);
        }
        public FileStreamResult Get(string id)
        {
            var fileInfo = _dfs.DownloadFile(id);
            var fileName = fileInfo.FileName;
            var minetype = FileContentType.GetMimeType(Path.GetExtension(fileName));

            return(File(fileInfo.Stream, minetype, fileName));
        }
Esempio n. 3
0
        public IActionResult GetFileByClientName(string clientName, string fileName)
        {
            var buffer    = logService.GetBuffer(clientName, fileName);
            var extension = Path.GetExtension(fileName);

            if (buffer == null)
            {
                return(null);
            }
            return(File(buffer, FileContentType.GetMimeType(extension), fileName));
        }
Esempio n. 4
0
        public File Compress(byte[] data, File file)
        {
            var id           = Guid.NewGuid();
            var subFolder    = CreateSubFolder(file.FileType);
            var fullFolder   = Path.Combine(_path, subFolder);
            var extName      = Path.GetExtension(file.FileName)?.ToLower();
            var filePath     = Path.Combine(subFolder, id + extName);
            var fullFilePath = Path.Combine(fullFolder, id + extName);

            if (!Directory.Exists(fullFolder))
            {
                Directory.CreateDirectory(fullFolder);
            }

            var compressFile = new File
            {
                Id        = id,
                FilePath  = filePath,
                FileType  = file.FileType,
                FileName  = file.FileName,
                MimeType  = FileContentType.GetMimeType(extName),
                Timestamp = DateTime.Now
            };

            using (var inStream = new MemoryStream(data))
            {
                using (var outStream = new MemoryStream())
                {
                    // Initialize the ImageFactory using the overload to preserve EXIF metadata.
                    using (var imageFactory = new ImageFactory(preserveExifData: true))
                    {
                        // Load, resize, set the format and quality and save an image.
                        imageFactory.Load(inStream)
                        .Quality(30)
                        .Save(outStream);
                    }
                    using (var fileStream = new FileStream(fullFilePath, FileMode.Create, FileAccess.Write))
                    {
                        outStream.CopyTo(fileStream);

                        var compressData = new byte[outStream.Length];
                        outStream.Read(compressData, 0, compressData.Length);
                        outStream.Seek(0, SeekOrigin.Begin);
                        compressFile.Md5 = GetMD5HashFromByte(compressData);
                        return(compressFile);
                    }
                }
            }
        }
Esempio n. 5
0
        public Guid[] CreateWithThumbnail(File file, byte[] data)
        {
            var originId           = Guid.NewGuid();
            var subFolder          = CreateSubFolder(file.FileType);
            var fullFolder         = Path.Combine(_path, subFolder);
            var extName            = Path.GetExtension(file.FileName)?.ToLower();
            var originFilePath     = Path.Combine(subFolder, originId + extName);
            var originfullFilePath = Path.Combine(fullFolder, originId + extName);


            if (!Directory.Exists(fullFolder))
            {
                Directory.CreateDirectory(fullFolder);
            }

            using (var stream = System.IO.File.Create(originfullFilePath))
            {
                stream.Write(data, 0, data.Length);
                stream.Flush();
                stream.Close();
            }

            var compressFile = Compress(data, file);

            var fileDAC = new FileDAC();

            file.Id       = originId;
            file.FilePath = originFilePath;
            file.MimeType = FileContentType.GetMimeType(extName);
            file.Md5      = GetMD5HashFromByte(data);

            using (var scope = new TransactionScope(TransactionScopeOption.Required, new TimeSpan(0, 0, 1, 30)))
            {
                fileDAC.Create(file);
                fileDAC.Create(compressFile);

                scope.Complete();
            }

            return(new Guid[] { file.Id, compressFile.Id });
        }
Esempio n. 6
0
        /// <summary>
        /// Creates the specified file.
        /// </summary>
        /// <param name="file">The file.</param>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        public File Create(File file, byte[] data)
        {
            var fileMd5 = GetMD5HashFromByte(data);

            var fileDAC = new FileDAC();
            var oldFile = fileDAC.SelectByMd5(fileMd5);

            var id           = Guid.NewGuid();
            var subFolder    = CreateSubFolder(file.FileType);
            var fullFolder   = Path.Combine(_path, subFolder);
            var extName      = Path.GetExtension(file.FileName)?.ToLower();
            var fullFilePath = Path.Combine(fullFolder, id + extName);

            if (!Directory.Exists(fullFolder))
            {
                Directory.CreateDirectory(fullFolder);
            }

            if (oldFile == null)
            {
                file.FilePath = Path.Combine(subFolder, id + extName);
                using (var stream = System.IO.File.Create(fullFilePath))
                {
                    stream.Write(data, 0, data.Length);
                    stream.Flush();
                    stream.Close();
                }
            }
            else
            {
                file.FilePath = oldFile.FilePath;
            }

            file.Id       = id;
            file.MimeType = FileContentType.GetMimeType(extName);
            file.Md5      = fileMd5;
            fileDAC.Create(file);

            return(file);
        }
Esempio n. 7
0
        private static void TemplateTransfer_forAliyun(AliyunOpenStorageService aos, AliyunOpenStorageService aosTarget)
        {
            var rootpath        = Directory.GetCurrentDirectory();
            var sourceFile      = rootpath + "//app_data//Transfer_Ali_SourceSiteIDs.txt";
            var FinishedIdsFile = rootpath + "//app_data//Transfer_Ali_FinishedIDs.txt";
            var ErrorFile       = rootpath + "//app_data//Transfer_Ali_Errors.txt";

            Console.WriteLine("\n\n欢迎使用阿里云站点文件迁移工具.");
            if (!File.Exists(sourceFile))
            {
                Console.WriteLine("未能找到Transfer_Ali_SourceSiteIDs.txt(源头云需要迁移的siteid集合文件)");
                return;
            }
            //本次需要上传的文件里面的id集合
            string[] sourceids = File.ReadAllText(sourceFile).Split(',');
            for (int i = 0; i < sourceids.Length; i++)
            {
                string tempsorceid = sourceids[i].Replace("\r", "").Replace("\n", "");
                sourceids[i] = tempsorceid;
            }
            Console.WriteLine("\n文件中共有{0}个站点, 系统将会从日志中匹配未迁移过的站点.", sourceids.Length);



            var list           = new List <string>(); //获取云端所有文件, 并在下载的时候下载成功一个移除一个项目
            var uplistkey      = new List <string>(); //list副本, 上传的时候读取
            var uplistFullName = new List <string>();


            for (int i = 0; i < sourceids.Length; i++)
            {  //json日志文件
                var FinishedList = JsonConvert.DeserializeAnonymousType(File.ReadAllText(FinishedIdsFile),
                                                                        new[] { new { SiteID = 0, FinishedDatetime = "" } }).ToList();

                bool   thisSiteHasError = false;      //该站点在迁移过程全程是否发生过问题
                string ErrorMessage     = "";         //如果有则用这个记录

                list.Clear(); uplistkey.Clear();      //清掉

                int tempsiteid = 0, targetSiteID = 0; //香港迁移美国的规则id一致
                if (!int.TryParse(sourceids[i], out tempsiteid))
                {
                    continue;
                }
                targetSiteID = tempsiteid;
                var tempobj = FinishedList.Where(x => x.SiteID == tempsiteid).FirstOrDefault();
                if (tempobj != null)
                {
                    continue;
                }

                #region 开始读取SiteID: {0} ,往list里面写入这个站点所有的文件

                var index = 1;
                Aliyun.OpenServices.OpenStorageService.ObjectListing result = null;
                string nextMarker = string.Empty;
                Console.WriteLine("开始读取SiteID: {0}", tempsiteid);
                try
                {
                    do
                    {
                        #region 往list里面写入这个站点所有的文件key

                        var listObjectsRequest = new Aliyun.OpenServices.OpenStorageService.ListObjectsRequest(aos.OssConfig.BucketName)
                        {
                            Marker  = nextMarker,
                            MaxKeys = 100
                            ,
                            Prefix = "content/sitefiles/" + tempsiteid.ToString() + "/"//只有图片的话不需要sitefiles分组
                        };
                        result = aos.ListObjects(listObjectsRequest);


                        foreach (var summary in result.ObjectSummaries)
                        {
                            var tempkey  = summary.Key;
                            var tempkeys = tempkey.Split('/');
                            var filename = tempkeys[tempkeys.Length - 1];
                            if (filename.Split('.').Length != 2) //如果没有后缀名. 那就不管了
                            {
                                continue;
                            }
                            list.Add(summary.Key);
                            //Console.WriteLine("{1}--Name:{0}", summary.Key, index);
                            index++;
                        }
                        nextMarker = result.NextMarker;

                        #endregion
                    } while (result.IsTruncated);//(false);//
                }
                catch (Exception ex)
                {
                    thisSiteHasError = true;
                    ErrorMessage    += "读取云端文件key到集合list的时候出现错误" + ex.Message;
                }
                #region 读取的过程中发生错误的话, 就判断这个站点读取失败, 记录到日志里面, continue循环, 读取下一个站点
                if (thisSiteHasError)
                {
                    var ErrorList = JsonConvert.DeserializeAnonymousType(File.ReadAllText(ErrorFile),
                                                                         new[] { new { SiteID = 0, FinishedDatetime = "", Message = "" } }).ToList();
                    ErrorList.Add(new { SiteID = tempsiteid, FinishedDatetime = DateTime.Now.ToLongTimeString(), Message = ErrorMessage });
                    string strtemperror = JsonConvert.SerializeObject(ErrorList);
                    File.WriteAllText(ErrorFile, strtemperror);
                    continue;
                }
                #endregion

                Console.WriteLine("\n\n Siteid:{1}, 共{0}个文件, 现在开始下载...", list.Count, tempsiteid);
                uplistkey = list.ToArray().ToList();

                #endregion

                #region  载到本地 目标是程序所在文件夹
                while (list.Count > 0)
                {
                    var curr = list[0];
                    try
                    {
                        using (Aliyun.OpenServices.OpenStorageService.OssObject obj = aos.GetObject(aos.OssConfig.BucketName, curr))
                        {
                            uplistFullName.Add(CreateFile(obj));
                            list.Remove(curr);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("siteid:{1}, 出现错误, 信息是{0}", ex.Message, tempsiteid);
                        Console.WriteLine("重试中......");
                    }
                }

                Console.WriteLine("下载完成");
                #endregion

                #region   到目标云
                Console.WriteLine("开始上传");
                while (uplistkey.Count > 0)
                {
                    bool tempuploaderror = false;        //记录上传的过程中是否有错误
                    var  sourceKey       = uplistkey[0]; //下载到本地的keys
                    var  targetKey       = uplistkey[0]; //上传到百度的keys
                    #region 处理key
                    var keys = targetKey.Split('/');
                    keys[2]   = targetSiteID.ToString();
                    targetKey = "";
                    keys.ToList().ForEach(x => targetKey += x + "/");
                    targetKey = targetKey.Remove(targetKey.Length - 1);
                    #endregion
                    //var fullname = Environment.CurrentDirectory + "/" + key;
                    using (FileStream fs = new FileStream(sourceKey, FileMode.Open))
                    {
                        //获得后缀名
                        var    temp = targetKey.Split('.');
                        string ext  = temp.Length > 1 ? temp[temp.Length - 1] : "";
                        try
                        {
                            Aliyun.OpenServices.OpenStorageService.ObjectMetadata metadata = new Aliyun.OpenServices.OpenStorageService.ObjectMetadata();
                            metadata.ContentLength = fs.Length;
                            metadata.ContentType   = FileContentType.GetMimeType(ext);
                            aosTarget.PutObject(aosTarget.OssConfig.BucketName, targetKey, fs, metadata);

                            uplistkey.Remove(sourceKey);
                            index++;
                        }
                        catch (Exception ex)
                        {
                            tempuploaderror = true;
                            Console.WriteLine("上传中出错,siteid:{0},  message:{1}", tempsiteid, ex.Message);
                            System.Threading.Thread.Sleep(2000);
                        }
                    }
                    if (!tempuploaderror)//上传成功, 删除本地文件
                    {
                        File.Delete(sourceKey);
                    }
                }
                Console.WriteLine("siteid:{0}上传完成", tempsiteid);
                #endregion

                //该站点完成, 写入日志
                FinishedList.Add(new { SiteID = tempsiteid, FinishedDatetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") });

                Console.WriteLine("siteid:{0}写入日志\n\n", tempsiteid);
                string strtemp = JsonConvert.SerializeObject(FinishedList);
                File.WriteAllText(FinishedIdsFile, strtemp);
            }

            Console.WriteLine("\n\n\n\n任务全部完成, 请查看日志");
            Console.ReadLine();
        }
Esempio n. 8
0
        private static void TemplateTransfer(BosOpenStorageService bos, AliyunOpenStorageService aos)
        {
            Console.WriteLine("目前程序支持阿里云迁移到百度云");

            int siteid = 0, targetSiteID = 0;

            #region 解析siteid
            while (true)
            {
                Console.WriteLine("请输入阿里云的SiteID");
                var sourceSiteID = Console.ReadLine();
                if (int.TryParse(sourceSiteID, out siteid))
                {
                    break;
                }
            }
            while (true)
            {
                Console.WriteLine("请输入百度云的SiteID");
                var baiduyunsiteid = Console.ReadLine();
                if (int.TryParse(baiduyunsiteid, out targetSiteID))
                {
                    break;
                }
            }
            //Console.WriteLine("解析siteid中......");
            //var isError = true;
            //while (isError)
            //{
            //    Console.WriteLine("请输入源模板的二级域名, 格式为 889117024.wezhan.cn");
            //    var domain = Console.ReadLine();
            //    try
            //    {
            //        domain = "http://" + domain + "/home/index";
            //        var htmls = RequestUtility.HttpGet(domain, null);

            //        string pattern = "content/sitefiles/\\d{2,7}/images";
            //        System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex(pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            //        var temp = re.Match(htmls);
            //        siteid = int.Parse(temp.Value.Replace("content/sitefiles/", "").Replace("/images", ""));
            //        Console.WriteLine("解析完毕, 即将列出所有的问题......");
            //        isError = false;//跳出循环
            //    }
            //    catch (Exception ex)
            //    {
            //        Console.WriteLine("出现错误, {0}", ex.Message);
            //        //Console.WriteLine("程序正在尝试二次解析siteid");
            //        isError = true;
            //    }
            //}
            #endregion

            var list           = new List <string>(); //获取云端所有文件
            var uplistkey      = new List <string>(); //list副本, 上传的时候读取
            var uplistFullName = new List <string>();
            #region
            Console.WriteLine("开始从阿里云读取文件"); System.Threading.Thread.Sleep(1000);
            var index = 1;
            Aliyun.OpenServices.OpenStorageService.ObjectListing result = null;
            string nextMarker = string.Empty;
            do
            {
                var listObjectsRequest = new Aliyun.OpenServices.OpenStorageService.ListObjectsRequest(aos.OssConfig.BucketName)
                {
                    Marker  = nextMarker,
                    MaxKeys = 100
                    ,
                    Prefix = "content/sitefiles/" + siteid.ToString() + "/"//只有图片的话不需要sitefiles分组
                };
                result = aos.ListObjects(listObjectsRequest);

                Console.WriteLine("File:");
                foreach (var summary in result.ObjectSummaries)
                {
                    var tempkey  = summary.Key;
                    var tempkeys = tempkey.Split('/');
                    var filename = tempkeys[tempkeys.Length - 1];
                    if (filename.Split('.').Length != 2) //如果没有后缀名. 那就不管了
                    {
                        continue;
                    }
                    list.Add(summary.Key);
                    Console.WriteLine("{1}--Name:{0}", summary.Key, index);
                    index++;
                }
                nextMarker = result.NextMarker;
            } while (result.IsTruncated);//(false);//
            Console.WriteLine("\n\n 共{0}个文件, 现在开始下载...", list.Count);
            uplistkey = list.ToArray().ToList();
            #endregion

            #region  载到本地 目标是程序所在文件夹
            //var errorlist = new List<string>();
            //var latestError = "";

            //for (int i = 0; i < list.Count; i++)
            //{
            //    try
            //    {
            //        using (Aliyun.OpenServices.OpenStorageService.OssObject obj = aos.GetObject(aos.OssConfig.BucketName, list[i]))
            //        {
            //            CreateFile(obj);
            //            Console.WriteLine("{0}下载成功--{1}", i + 1, obj.Key);
            //        }
            //    }catch(Exception ex){
            //        errorlist.Add(list[i]);
            //        latestError = ex.Message;
            //    }
            //}
            System.Threading.Thread.Sleep(2000);
            index = 1;
            while (list.Count > 0)
            {
                var curr = list[0];
                try
                {
                    using (Aliyun.OpenServices.OpenStorageService.OssObject obj = aos.GetObject(aos.OssConfig.BucketName, curr))
                    {
                        uplistFullName.Add(CreateFile(obj));
                        Console.WriteLine("{0}下载成功--{1}", index, obj.Key);
                        index++;
                        list.Remove(curr);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("出现错误, 信息是{0}", ex.Message);
                    Console.WriteLine("重试中......");
                }
            }

            Console.WriteLine("下载完成");
            #endregion

            Console.WriteLine("开始上传到百度云\n\n");
            #region   到百度云
            index = 1;
            while (uplistkey.Count > 0)
            {
                var sourceKey = uplistkey[0]; //下载到本地的keys
                var targetKey = uplistkey[0]; //上传到百度的keys
                #region 处理key
                var keys = targetKey.Split('/');
                keys[2]   = targetSiteID.ToString();
                targetKey = "";
                keys.ToList().ForEach(x => targetKey += x + "/");
                targetKey = targetKey.Remove(targetKey.Length - 1);
                #endregion
                //var fullname = Environment.CurrentDirectory + "/" + key;
                using (FileStream fs = new FileStream(sourceKey, FileMode.Open))
                {
                    //获得后缀名
                    var    temp = targetKey.Split('.');
                    string ext  = temp.Length > 1 ? temp[temp.Length - 1] : "";
                    try
                    {
                        ObjectMetadata metadata = new ObjectMetadata();
                        metadata.ContentLength = fs.Length;
                        metadata.ContentType   = FileContentType.GetMimeType(ext);
                        bos.PutObject(bos.OssConfig.BucketName, targetKey, fs, metadata);

                        Console.WriteLine("完成上传{0}----{1}", index, targetKey);
                        File.Delete(sourceKey);//成功之后就删除本地sourcekey
                        uplistkey.Remove(sourceKey);
                        index++;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("处理{0}遇到问题, 代码是{1}, 当前进度{2}/{3}", sourceKey, ex.Message, index, uplistkey.Count);
                        Console.WriteLine("重试中");
                    }
                }
            }
            #endregion


            Console.WriteLine("\n\n完成上传");

            Console.ReadLine();
        }
Esempio n. 9
0
        private static DateTime RunUpload(DateTime updateDate, string FolderPath, string Pre_key, BosOpenStorageService bos)
        {
            #region 读取用户输入
            bool flag = true;
            while (flag)
            {
                Console.WriteLine("使用默认更新的时间点 {0} 则输入y, 或者输入时间,格式为:2015-12-05", updateDate.ToString("yyyy-MM-dd"));
                string P_time = Console.ReadLine();
                if (P_time.ToLower() != "y")
                {
                    DateTime temptime;
                    if (DateTime.TryParse(P_time, out temptime))
                    {
                        flag       = false;
                        updateDate = temptime;
                    }
                }
                if (P_time.ToLower() == "y")
                {
                    flag = false;
                }
            }
            #endregion

            Console.WriteLine("启动完毕, 更换文件时间节点是 " + updateDate.ToLongDateString());
            writeCurrentRunDay();//把updateDate设置为今天

            LogHelper.CreateErrorLogTxt(string.Format("-----------------------{0}-开始发布----------------------", DateTime.Now.ToLongDateString()));

            AllFiles      allFiles = new AllFiles();
            List <String> list     = allFiles.FindFile(FolderPath, updateDate); //所有文件的物理地址
            List <String> list2    = new List <string>();                       //文件上传到云盘上的key集合
            #region 文件上传到云盘上的key集合
            foreach (var item in list)
            {
                //Console.WriteLine(item);
                string tempItem = item;
                tempItem = item.Replace(FolderPath, "").Replace('\\', '/');
                tempItem = Pre_key + tempItem;
                list2.Add(tempItem);
            }
            #endregion

            LogHelper.CreateErrorLogTxt(string.Format("读取完毕所有的文件, 共计 {0}", list.Count));

            var isError = false;
            for (int i = 0; i < list2.Count; i++)
            {
                string key = list2[i];
                #region   到云
                using (FileStream fs = new FileStream(list[i], FileMode.Open))
                {
                    //获得后缀名
                    var    temp = list[i].Split('.');
                    string ext  = temp.Length > 1 ? temp[temp.Length - 1] : "";
                    try
                    {
                        //if (i > 28)
                        //    throw new Exception();
                        //---------------上传到bos
                        ObjectMetadata metadata = new ObjectMetadata();
                        metadata.ContentLength = fs.Length;
                        metadata.ContentType   = FileContentType.GetMimeType(ext);
                        bos.PutObject(bos.OssConfig.BucketName, key, fs, metadata);

                        Console.WriteLine("完成上传{0}----{1}", i, key);
                        File.Delete(list[i]);//成功之后就删除
                    }
                    catch (Exception ex)
                    {
                        isError = true;
                        Console.WriteLine("处理{0}遇到问题, 代码是{1}, 当前进度{2}/{3}, 继续请按y", key, ex.Message, i + 1, list2.Count);
                        var ifcontinue = Console.ReadLine();
                        if (ifcontinue.ToLower().Trim() != "y")
                        {
                            break;
                        }

                        LogHelper.CreateErrorLogTxt(string.Format("失败 {0},  {1}", key, ex.Message));
                    }
                }
                #endregion
            }
            LogHelper.CreateErrorLogTxt(string.Format("-----------------------{0}-完成----------------------\n\n\n", DateTime.Now.ToLongDateString()));
            if (!isError)
            {
                Console.WriteLine("任务完成, 没有出现任何问题,");
            }
            else
            {
                Console.WriteLine("上传出现问题, 请查看日志");
                deletecurrentday();//重置上次成功的上传时间
            }
            return(updateDate);
        }