예제 #1
0
        public void Test_PrefixJustUnc_2()
        {
            var src  = @"\\server\share-name\dir\file.txt";
            var scan = new FilepathScanner(src);

            Assert.IsTrue(Unc.TryParse(scan, out var prefix));
            Assert.AreEqual("server", prefix !.Server);
            Assert.AreEqual("share-name", prefix !.Share);
        }
예제 #2
0
        public void Test_PrefixJustUnc_1()
        {
            var src  = @"\\server\C$";
            var scan = new FilepathScanner(src);

            Assert.IsTrue(Unc.TryParse(scan, out var prefix));
            Assert.AreEqual("server", prefix !.Server);
            Assert.AreEqual("C$", prefix !.Share);
        }
예제 #3
0
        public void ConcatPaths()
        {
            Assert.AreEqual(
                @"C:\Program Files (x86)\Microsoft Visual Studio 2017",
                (Drive.C / "Program Files (x86)" / "Microsoft Visual Studio 2017").Value);

            Assert.AreEqual(
                @"\\somemachine\someshare\subpath\file.txt",
                (Unc.Host("somemachine").Share("someshare") / "subpath" / "file.txt").Value);
        }
예제 #4
0
        public static Common.Core.OMSFile.Model.AttachmentItem Download(int attachmentItemId, bool useBrinary, TacketBase tacketBase, out bool fileUnExists)
        {
            fileUnExists = false;

            if (attachmentItemId <= 0)
            {
                throw new ArgumentOutOfRangeException("attachmentId");
            }

            if (tacketBase == null)
            {
                throw new ArgumentNullException("tacketBase", "tacketBase must be not null,please fix error");
            }

            IAttachmentService attachmentService = ObjectContainer.ResolveService <IAttachmentService>();

            if (attachmentService == null)
            {
                throw new ArgumentNullException("IAttachmentService反射加载失败");
            }

            var attachmentItem = attachmentService.GetAttachmentItemById(attachmentItemId, true);

            switch (attachmentItem.UploadMode)
            {
            case UploadMode.FTP:

                FtpTacket ftpTacket = tacketBase as FtpTacket;

                if (ftpTacket == null)
                {
                    throw new ArgumentNullException("tacketBase");
                }

                using (Ftp ftp = new Ftp(ftpTacket.Host, ftpTacket.UserName, ftpTacket.PassWord, ftpTacket.Port.ToString()))
                {
                    ftp.Directory = attachmentItem.FilePath.Replace(Path.GetFileName(attachmentItem.FilePath), string.Empty);
                    ftp.FileName  = Path.GetFileName(attachmentItem.FilePath);

                    if (attachmentItem.Content == null)
                    {
                        attachmentItem.Content = new Common.Core.OMSFile.Model.AttachmentContent();
                    }
                    attachmentItem.Content.Content = ftp.Download();
                }

                break;

            case UploadMode.UNC:

                using (Unc unc = new Unc(tacketBase.UserName, tacketBase.PassWord, tacketBase.Host))
                {
                    string uncFullPath = tacketBase.Host + "\\" + attachmentItem.FilePath.TrimStart('/').Replace('/', '\\');

                    EnsureDirectory(uncFullPath);

                    if (!File.Exists(uncFullPath))
                    {
                        fileUnExists = true;
                        return(null);
                    }

                    if (attachmentItem.Content == null)
                    {
                        attachmentItem.Content = new Common.Core.OMSFile.Model.AttachmentContent();
                    }
                    attachmentItem.Content.Content = File.ReadAllBytes(uncFullPath);
                }

                break;

            case UploadMode.LOC:
            default:
                string localFullPath = tacketBase.Host + "\\" + attachmentItem.FilePath.TrimStart('/').Replace('/', '\\');

                EnsureDirectory(localFullPath);

                if (!File.Exists(localFullPath))
                {
                    fileUnExists = true;
                    return(null);
                }

                if (attachmentItem.Content == null)
                {
                    attachmentItem.Content = new Common.Core.OMSFile.Model.AttachmentContent();
                }
                attachmentItem.Content.Content = File.ReadAllBytes(localFullPath);
                break;
            }

            return(attachmentItem);
        }
예제 #5
0
        public static Common.Core.OMSFile.Model.AttachmentInfo Upload(Common.Core.OMSFile.Model.AttachmentInfo info, bool isMutiple, TacketBase tacketBase, bool isUpdateModel = false)
        {
            IAttachmentService attachmentService = ObjectContainer.ResolveService <IAttachmentService>();

            if (info == null)
            {
                throw new ArgumentNullException("attachmentInfo", "attachmentInfo must be not null,please fix error");
            }

            if (tacketBase == null)
            {
                throw new ArgumentNullException("tacketBase", "tacketBase must be not null,please fix error");
            }

            if (info.Items != null)
            {
                if (info.Items == null)
                {
                    throw new ArgumentNullException("attachmentInfo.Items must be not null,please fix error");
                }

                foreach (var item in info.Items)
                {
                    if (item.Id.Equals(0) && (item.Content == null || item.Content.Content == null))
                    {
                        throw new ArgumentNullException("item.Content must be not null,please fix error");
                    }

                    item.UploadMode = tacketBase.Mode;
                }
            }

            var attachmentInfo = attachmentService.SaveOrUpdate(info, true, isMutiple, isUpdateModel);

            #region switch case

            if (attachmentInfo.Items == null || !attachmentInfo.Items.Any())
            {
                return(attachmentInfo);
            }

            switch (tacketBase.Mode)
            {
            case UploadMode.FTP:

                FtpTacket ftpTacket = tacketBase as FtpTacket;

                if (ftpTacket == null)
                {
                    throw new ArgumentNullException("tacketBase");
                }


                using (
                    Ftp ftp = new Ftp(ftpTacket.Host, ftpTacket.UserName, ftpTacket.PassWord,
                                      ftpTacket.Port.ToString()))
                {
                    foreach (var item in attachmentInfo.Items)
                    {
                        if (item.IsOld)
                        {
                            continue;
                        }

                        ftp.Directory = item.FilePath.Replace(Path.GetFileName(item.FilePath), string.Empty);
                        ftp.FileName  = Path.GetFileName(item.FilePath);

                        ftp.Upload(item.Content.Content);
                    }
                }

                break;

            case UploadMode.UNC:

                using (
                    Unc unc = new Unc(tacketBase.UserName, tacketBase.PassWord, tacketBase.Host))
                {
                    foreach (var item in attachmentInfo.Items)
                    {
                        //string uncDirectory = this.tacketBase.Host + "\\" + item.FilePath.Replace(Path.GetFileName(item.FilePath), string.Empty).TrimStart('/').Replace('/', '\\');
                        if (item.IsOld)
                        {
                            continue;
                        }

                        string uncFullPath = tacketBase.Host + "\\" +
                                             item.FilePath.TrimStart('/').Replace('/', '\\');

                        EnsureDirectory(uncFullPath);

                        File.WriteAllBytes(uncFullPath, item.Content.Content);
                    }
                }

                break;

            //case UploadMode.LOC:
            default:

                foreach (var item in attachmentInfo.Items)
                {
                    //string localDirectory = this.tacketBase.Host + "\\" + item.FilePath.Replace(Path.GetFileName(item.FilePath), string.Empty).TrimStart('/').Replace('/', '\\');
                    if (item.IsOld)
                    {
                        continue;
                    }

                    string localFullPath = tacketBase.Host + "\\" +
                                           item.FilePath.TrimStart('/').Replace('/', '\\');

                    EnsureDirectory(localFullPath);

                    File.WriteAllBytes(localFullPath, item.Content.Content);
                }
                break;
            }

            #endregion

            return(attachmentInfo);
        }