コード例 #1
0
ファイル: FileComponent.cs プロジェクト: BryceStory/BS.FPApp
        /// <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);
        }
コード例 #2
0
ファイル: SearchFile.cs プロジェクト: minfinitum/vcodehunt
            public void SetMatch(long index, byte[] value, byte [] match, FileContentType matchType)
            {
                this.Index = index;
                string vstr = string.Empty;

                for (int idx = 0; idx < value.Count(); idx++)
                {
                    vstr += value[idx].ToString("X2");

                    if (idx < value.Count() - 1)
                    {
                        vstr += " ";
                    }
                }

                string mstr = string.Empty;

                for (int idx = 0; idx < match.Count(); idx++)
                {
                    mstr += match[idx].ToString("X2");
                    if (idx < match.Count() - 1)
                    {
                        mstr += " ";
                    }
                }

                this.Value     = vstr;
                this.Match     = mstr;
                this.MatchType = matchType;
            }
コード例 #3
0
        internal static async Task <StorageFolder> GetFolderFromType(FileContentType type)
        {
            try
            {
                StorageFolder rootFolder = null;

                switch (type)
                {
                case FileContentType.Music:
                    rootFolder = KnownFolders.MusicLibrary;
                    break;

                case FileContentType.Video:
                    rootFolder = KnownFolders.VideosLibrary;
                    break;

                case FileContentType.Image:
                    rootFolder = KnownFolders.SavedPictures;
                    break;

                default:
                    return(await KnownFolders.PicturesLibrary.CreateFolderAsync(DOWNLOADS_OTHER_FOLDER_NAME,
                                                                                CreationCollisionOption.OpenIfExists));
                }

                return(await rootFolder.CreateFolderAsync(DOWNLOADS_FOLDER_NAME,
                                                          CreationCollisionOption.OpenIfExists));
            }
            catch (Exception)
            {
                return(null);
            }
        }
コード例 #4
0
ファイル: FileController.cs プロジェクト: tbajkacz/UploadIt
        public async Task <IActionResult> Download([FromQuery] string fileName)
        {
            var userIdString = User.Identity.Name;

            if (userIdString == null || !int.TryParse(userIdString, out int userId))
            {
                return(BadRequest("Invalid token"));
            }
            var file = await _storage.RetrieveFileAsync(fileName, userIdString);

            if (file == null)
            {
                return(BadRequest("File does not exist"));
            }

            ContentDisposition contentDisposition = new ContentDisposition
            {
                FileName = fileName,
                //inline true tries to display the file in the browser
                Inline = false
            };

            Response.Headers.Add("Content-Disposition", contentDisposition.ToString());
            //exposes the content-disposition header to javascript code
            Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");

            return(File(file, FileContentType.Get(fileName)));;
        }
コード例 #5
0
 /// <summary>
 /// Инициализирует новый экземпляр класса <see cref="TransferOperationErrorEventArgs"/>
 /// с заданной информации об ошибке операции передачи данных.
 /// </summary>
 /// <param name="guid">Уникальный идентификатор операции.</param>
 /// <param name="name">Название операции.</param>
 /// <param name="type">Тип содержимого.</param>
 /// <param name="exception">Произошедшее исключение.</param>
 public TransferOperationErrorEventArgs(Guid guid, string name, FileContentType type, Exception exception)
 {
     OperationGuid = guid;
     Name          = name;
     ContentType   = type;
     Exception     = exception;
 }
コード例 #6
0
ファイル: SearchFile.cs プロジェクト: minfinitum/vcodehunt
 public void SetMatch(long index, string value, string match, FileContentType matchType)
 {
     this.Index     = index;
     this.Value     = value;
     this.Match     = match;
     this.MatchType = matchType;
 }
コード例 #7
0
        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));
        }
コード例 #8
0
ファイル: SearchFile.cs プロジェクト: minfinitum/vcodehunt
            public KeywordMatch(long index, string value, string match, FileContentType matchType)
            {
                this.ContextPre  = new Queue <string>();
                this.ContextPost = new Queue <string>();

                this.Index = index;
                this.Value = value;
                this.Match = match;

                this.MatchType = matchType;
            }
コード例 #9
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));
        }
コード例 #10
0
        public async Task <IActionResult> DownloadImageAuction(int id, int imageId)
        {
            var result = new HttpResponseMessage(HttpStatusCode.OK);

            var images = await _context.AuctionImage
                         .Where(x => x.AuctionID == id && x.ID == imageId)
                         .AsNoTracking()
                         .ToListAsync();

            var path = Path.GetFullPath(images.FirstOrDefault()?.ImageLocation);

            byte[] bytes = System.IO.File.ReadAllBytes(path);
            return(File(bytes, FileContentType.GetContentType(path)));
        }
コード例 #11
0
        /// <summary>
        /// Zkusí uložit databázi do DataPacku
        /// </summary>
        private void TrySaveData()
        {
            if (this._Database == null)
            {
                throw new InvalidOperationException("Nelze uložit databázi, dosud není vytvořena. Počkejte několik sekund...");
            }
            if (!this._Database.IsReady)
            {
                throw new InvalidOperationException("Nelze uložit databázi, dosud není připravena. Počkejte několik sekund...");
            }

            FileContentType[] fileTypes = new FileContentType[] { FileContentType.Structure, FileContentType.Data, FileContentType.DataPack };
            this._Database.SaveStandardData(fileTypes, saveFormat: SaveFormat.Pack, progress: DatabaseShowProgress);
        }
コード例 #12
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);
                    }
                }
            }
        }
コード例 #13
0
    public void TestFileType(string extension, FileContentType fileType)
    {
        _autoMocker
        .Setup <IFileTypeMapper, FileContentType>(m => m.GetFileType(extension))
        .Returns(fileType);
        _autoMocker.Use(false);

        var viewModel = _autoMocker.CreateInstance <FileViewModel>();

        viewModel.Extension = extension;

        var actualType = viewModel.Type;

        Assert.Equal(fileType, actualType);
    }
コード例 #14
0
ファイル: CppLang.cs プロジェクト: zH4x/Unreal-Dumping-Agent
        public string GenerateFileName(FileContentType type, string packageName)
        {
            switch (type)
            {
            case FileContentType.Structs:
                return($"{packageName}_structs.h");

            case FileContentType.Classes:
                return($"{packageName}_classes.h");

            case FileContentType.Functions:
                return($"{packageName}_functions.cpp");

            case FileContentType.FunctionParameters:
                return($"{packageName}_parameters.h");

            default:
                throw new Exception("WHAT IS THIS TYPE .?!!");
            }
        }
コード例 #15
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 });
        }
コード例 #16
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);
        }
コード例 #17
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();
        }
コード例 #18
0
        public async Task <IActionResult> UploadImageAuction([FromForm] List <IFormFile> files, int id)
        {
            long size = files.Sum(f => f.Length);

            foreach (var file in files)
            {
                if (file.Length > 0)
                {
                    var path = Path.GetTempFileName();

                    AuctionImage lastImage = null;

                    if (_context.AuctionImage.Count() > 0)
                    {
                        lastImage = await _context.AuctionImage
                                    .OrderBy(x => x.ID)
                                    .LastAsync();
                    }

                    int lastImageID = 1;
                    if (lastImage != null)
                    {
                        lastImageID = (lastImage.ID + 1);
                    }

                    using (var stream = System.IO.File.Create(path))
                    {
                        await file.CopyToAsync(stream);
                    }

                    var newpath = VeilingConfiguration.AuctionImageLocation + $"/{id}/{lastImageID}{FileContentType.GetExtension(file.ContentType)}";
                    Directory.CreateDirectory(Path.GetDirectoryName(newpath));
                    System.IO.File.Delete(newpath);

                    _context.AuctionImage.Add(new AuctionImage()
                    {
                        AuctionID = id, ImageLocation = newpath, ID = lastImageID, AspectRatio = AuctionImage.CalculateAspectRatio(file.OpenReadStream())
                    });
                    await _context.SaveChangesAsync();

                    System.IO.File.Move(path, newpath);
                    System.IO.File.Delete(path);
                }
            }

            return(Ok(new { count = files.Count, size }));
        }
コード例 #19
0
        /// <summary>
        /// Parses the user-to-groups json file
        /// </summary>
        /// <param name="path">Path to the JSon file.</param>
        /// <returns>A list of User2GroupsMap objects. Returns null in case of errors.</returns>
        public static List <User2GroupsMap> ReadUser2GroupsMapFromFile(string path, InputFileType fileType, FileContentType fileContentType)
        {
            try
            {
                if (!File.Exists(path))
                {
                    return(null);
                }

                if (fileType == InputFileType.JSON)
                {
                    return(JsonConvert.DeserializeObject <List <User2GroupsMap> >(File.ReadAllText(path)));
                }
                else if (fileType == InputFileType.CSV)
                {
                    string[] strAllText = File.ReadAllLines(path);
                    List <User2GroupsMap> user2GroupsMap = new List <User2GroupsMap>();

                    foreach (string strLine in strAllText)
                    {
                        if (!strLine.StartsWith("#") && !string.IsNullOrEmpty(strLine.Trim()))
                        {
                            if (fileContentType == FileContentType.UsersToGroupsMapping)
                            {
                                string[] arr = strLine.Split(User2GroupsAndProjectsSplitter, StringSplitOptions.RemoveEmptyEntries);
                                if (arr.Length < 3)
                                {
                                    FileHelper.Log("Invalid entry in mapping file ->" + strLine);
                                }
                                else
                                {
                                    User2GroupsMap userMap = new User2GroupsMap(arr[0], new List <string>(arr[1].Split(GroupsAndProjectsSplitter)),
                                                                                new List <string>(arr[2].Split(GroupsAndProjectsSplitter)));
                                    user2GroupsMap.Add(userMap);
                                }
                            }
                            else if (fileContentType == FileContentType.UsersList)
                            {
                                string[]       arr  = strLine.Split(User2GroupsAndProjectsSplitter, StringSplitOptions.RemoveEmptyEntries);
                                User2GroupsMap user = null;
                                if (arr.Length > 1)
                                {
                                    string[] teamProjectNames = arr[1].Split(GroupsAndProjectsSplitter);
                                    user = new User2GroupsMap(arr[0], new List <string>(),
                                                              new List <string>(teamProjectNames));
                                }
                                else
                                {
                                    user = new User2GroupsMap(arr[0], new List <string>(), new List <string>());
                                }

                                if (user != null)
                                {
                                    user2GroupsMap.Add(user);
                                }
                            }
                        }
                    }
                    return(user2GroupsMap);
                }
            }
            catch (Exception ex)
            {
                FileHelper.Log(ex.Message);
            }
            return(null);
        }
コード例 #20
0
 public MAMController() : base()
 {
     this._utility = new Utility();
     this._fileContentType = new FileContentType();
     this._errMsg = new ErrorMessage();
 }
コード例 #21
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);
        }
コード例 #22
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();
        }
コード例 #23
0
    public void TestMapping(string extension, FileContentType expectedType)
    {
        var actualType = _fileTypeMapper.GetFileType(extension);

        Assert.Equal(expectedType, actualType);
    }