コード例 #1
0
        public CommonResponse DirectoryCreate(DirectoryCreateRequest request)
        {
            CommonResponse response = new CommonResponse();
            response.IsSuccessful = false;

            try
            {
                bool isPermission = Authentication(request.Token, request.FileServerId, request.Account, request.PassWord);
                if (!isPermission)
                {
                    response.IsSuccessful = false;
                    response.ResultMessage = "您没有访问该文件服务器接口的权限!";
                    return response;
                }

                Directory.CreateDirectory(request.OriginalAbsoluteFileDirectory);
                Directory.CreateDirectory(request.ThumbAbsoluteFileDirectory);

                response.IsSuccessful = true;
                response.ResultMessage = string.Empty;
            }
            catch (Exception ex)
            {
                response.IsSuccessful = false;
                response.ResultMessage = ex.ToString();
                Tools.LogWrite(ex.ToString());
            }

            return response;
        }
コード例 #2
0
ファイル: BLLUpload.cs プロジェクト: ufo20020427/FileUpload
        private void FileUpload(object sender)
        {
            _semaphoreTask.WaitOne();

            _fileUploadStartTime = DateTime.Now;

            UploadInfo uploadInfo = null;
            FolderInfo uploadFolderInfo = null;

            try
            {
                uploadInfo = sender as UploadInfo;
                uploadFolderInfo = uploadInfo.FolderInfo;

                string[] paths = uploadFolderInfo.LocalPath.Split(new char[] { '\\' });
                string localDirectory = paths[paths.Length - 1];

                string fileExtenName = Path.GetExtension(uploadInfo.FilePath).ToLower();

                FileUploadRequest request = new FileUploadRequest();
                request.Token = ClientConfig.Token;
                request.Account = ClientConfig.Account;
                request.PassWord = ClientConfig.PassWord;
                request.ThumbPictureWidth = ClientConfig.ThumbPictureWidth;
                request.ThumbPictureHeight = ClientConfig.ThumbPictureHeight;
                request.VectorPictureExtenName = ClientConfig.VectorPictureExtenName;
                request.CategoryType = uploadFolderInfo.CategoryType;

                request.FileServerId = _fileServerInfo.Id;
                request.OriginalFileServerRootDirectory = _fileServerInfo.OriginalFileServerRootDirectory;
                request.ThumbFileServerRootDirectory = _fileServerInfo.ThumbFileServerRootDirectory;
                request.CategoryAbsolutePath = uploadFolderInfo.LevelPath.Replace("|", "\\\\") + "\\\\" + localDirectory + "\\\\";
                request.CategoryRelativePath = uploadFolderInfo.LevelPath.Replace("|", "/") + "/" + localDirectory + "/";
                request.LevelCategoryName = uploadFolderInfo.LevelCategory;
                request.IsVector = ClientConfig.VectorPictureExtenName.Contains(fileExtenName);
                request.IsThumbSquare = uploadFolderInfo.IsThumbSquare;
                request.FileName = Path.GetFileName(uploadInfo.FilePath).ToLower();
                if (request.IsVector)
                {
                    string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(uploadInfo.FilePath).ToLower();
                    request.VectorFileName = uploadFolderInfo.dicVectorFile[fileNameWithoutExtension];
                }

                if (uploadFolderInfo.CategoryType == CategoryType.Gallery)
                {
                    request.CategoryId = uploadFolderInfo.GalleryId;
                    request.StoreTableName = uploadFolderInfo.StoreTableName.Replace("Album", "Photo");
                }
                else
                {
                    request.CategoryId = uploadFolderInfo.CategoryId;
                    request.StoreTableName = uploadFolderInfo.StoreTableName;
                }

                //文件上传时,如果是相册类型StoreName要转化
                CommonResponse response = new CommonResponse();
                using (FileStream fs = new FileStream(uploadInfo.FilePath, FileMode.Open, FileAccess.Read))
                {
                    request.FileData = fs;
                    response = _proxy.FileUpLoad(request);
                }

                if (response.IsSuccessful)
                {
                    uploadFolderInfo.SucessfulUploadFilesCount++;

                    //文件属性置为只读
                    File.SetAttributes(uploadInfo.FilePath, FileAttributes.ReadOnly);
                }
                else
                {
                    string uploadResult = string.Format("{0},{1}", Path.GetFileName(uploadInfo.FilePath), response.ResultMessage);

                    string errorInfo = string.Format("目录:{0}\r\n文件上传失败:{1}", uploadFolderInfo.LocalPath, uploadResult);
                    Tools.LogWrite(errorInfo);

                    uploadFolderInfo.UploadResult.AppendLine(uploadResult);
                }
            }
            catch(Exception ex)
            {
                string uploadResult = string.Format("{0},{1}", Path.GetFileName(uploadInfo.FilePath), ex.ToString());

                string errorInfo = string.Format("目录:{0}\r\n文件上传异常:{1}", uploadFolderInfo.LocalPath, uploadResult);
                Tools.LogWrite(errorInfo);

                uploadFolderInfo.UploadResult.AppendLine(uploadResult);
            }
            finally
            {
                _semaphoreTask.Release();

                lock (_syncLock)
                {
                    if (uploadFolderInfo.WaitUploadFilesCount > 0)
                    {
                        uploadFolderInfo.WaitUploadFilesCount--;
                    }
                }
            }
        }
コード例 #3
0
        public CommonResponse FileBundling(GalleryBundlingRequest request)
        {
            CommonResponse response = new CommonResponse();
            response.IsSuccessful = false;
            string packageSavePath = string.Empty;

            try
            {
                bool isPermission = Authentication(request.Token, request.FileServerId, request.Account, request.PassWord);
                if (!isPermission)
                {
                    response.IsSuccessful = false;
                    response.ResultMessage = "您没有访问该文件服务器接口的权限!";
                    return response;
                }

                if (!Directory.Exists(request.OriginalAbsoluteFileDirectory))
                {
                    response.IsSuccessful = false;
                    response.ResultMessage = string.Format("待打包目录:{0} 不存在!" , request.OriginalAbsoluteFileDirectory);
                    return response;
                }

                packageSavePath = Path.Combine(request.OriginalAbsoluteFileDirectory, "Package.zip");
                if (File.Exists(packageSavePath))
                {
                    File.Delete(packageSavePath);
                }

                string fileName = string.Empty;
                List<string> listFile = new List<string>();
                foreach (string file in Directory.GetFiles(request.OriginalAbsoluteFileDirectory))
                {
                    fileName = Path.GetFileName(file).ToLower();
                    if (fileName != "video.rar")
                    {
                        listFile.Add(file);
                    }
                }

                if (listFile.Count > 0)
                {
                    Tools.FileCompress(listFile, packageSavePath);
                    response.IsSuccessful = true;
                    response.ResultMessage = string.Empty;
                }
                else
                {
                    response.IsSuccessful = false;
                    response.ResultMessage = string.Format("待打包目录:{0} 为空目录!" ,request.OriginalAbsoluteFileDirectory);
                }
            }
            catch (Exception ex)
            {
                response.IsSuccessful = false;
                response.ResultMessage = ex.ToString();
                Tools.LogWrite(ex.ToString());
            }

            return response;
        }
コード例 #4
0
        public CommonResponse GalleryCreate(GalleryCreateRequest request)
        {
            CommonResponse response = new CommonResponse();
            response.IsSuccessful = false;

            try
            {
                bool isPermission = Authentication(request.Token, request.FileServerId, request.Account, request.PassWord);
                if (!isPermission)
                {
                    response.IsSuccessful = false;
                    response.ResultMessage = "您没有访问该文件服务器接口的权限!";
                    return response;
                }

                Directory.CreateDirectory(request.OriginalAbsoluteFileDirectory);
                Directory.CreateDirectory(request.ThumbAbsoluteFileDirectory);

                DbCommand cmd = _dataBaseAccess.CreateCommand();
                GalleryCreateParamAdd(ref cmd, request);

                switch (request.StoreTableName)
                {
                    case "Album_Brand":    //品牌画册
                        {
                            cmd.CommandText = "Proc_Album_Brand_InsertOrUpdate";
                            break;
                        }
                    case "Album_Magazine": //服装杂志
                        {
                            cmd.CommandText = "Proc_Album_Magazine_InsertOrUpdate";
                            break;
                        }
                    case "Album_Book":     //款式书籍
                        {
                            cmd.CommandText = "Proc_Album_Book_InsertOrUpdate";
                            break;
                        }
                    case "Album_Confer":     //发布会
                        {
                            cmd.CommandText = "Proc_Album_Confer_InsertOrUpdate";
                            break;
                        }
                    default:
                        {
                            response.IsSuccessful = false;
                            response.ResultMessage = string.Format("业务逻辑无法识别该表:{0}", request.StoreTableName);
                            return response;
                        }
                }

                _dataBaseAccess.ExecuteCommand(cmd);
                string galleryId = cmd.Parameters["@GalleryId"].Value.ToString();
                string result = cmd.Parameters["@Result"].Value.ToString();

                response.IsSuccessful = string.IsNullOrEmpty(result);
                response.ResultMessage = response.IsSuccessful ? galleryId : result;
            }
            catch (Exception ex)
            {
                response.IsSuccessful = false;
                response.ResultMessage = ex.ToString();
                Tools.LogWrite(ex.ToString());
            }

            return response;
        }
コード例 #5
0
        public CommonResponse FileUpLoad(FileUploadRequest request)
        {
            CommonResponse response = new CommonResponse();
            response.IsSuccessful = false;
            string originalFileSavePath = string.Empty;
            string thumbFileSavePath = string.Empty;

            try
            {
                bool isPermission = Authentication(request.Token, request.FileServerId, request.Account, request.PassWord);
                if (!isPermission)
                {
                    response.IsSuccessful = false;
                    response.ResultMessage = "您没有访问该文件服务器接口的权限!";
                    return response;
                }

                originalFileSavePath = request.OriginalFileServerRootDirectory + request.CategoryAbsolutePath + request.FileName;

                bool isOriginalFileExists = false;
                if (request.IsVector)
                {
                    string[] vectorFileNames = request.VectorFileName.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string vectorFileName in vectorFileNames)
                    {
                        string checkFile = request.OriginalFileServerRootDirectory + request.CategoryAbsolutePath + vectorFileName;
                        if (File.Exists(checkFile))
                        {
                            isOriginalFileExists = true;
                            break;
                        }
                    }
                }
                else
                {
                    isOriginalFileExists = File.Exists(originalFileSavePath);
                }

                if (request.FileName == "cover.jpg") //封面
                {
                    originalFileSavePath = request.ThumbFileServerRootDirectory + request.CategoryAbsolutePath + "cover_Temp.jpg";
                    thumbFileSavePath = request.ThumbFileServerRootDirectory + request.CategoryAbsolutePath + request.FileName;
                    using (FileStream outputStream = new FileStream(originalFileSavePath, FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        request.FileData.CopyTo(outputStream);
                        outputStream.Flush();
                    }

                    if (request.IsThumbSquare)
                    {
                        Tools.CreatePictureThumbFromCenter(originalFileSavePath, thumbFileSavePath);
                    }
                    else
                    {
                        Tools.CreatePictureThumbFromCenter(originalFileSavePath, thumbFileSavePath, request.ThumbPictureWidth, request.ThumbPictureHeight);
                    }

                    if (File.Exists(originalFileSavePath))
                    {
                        File.Delete(originalFileSavePath);
                    }
                }
                else if(request.FileName.Substring(0, 3) == "sm_") //缩略图
                {
                    // 直接复制到缩略图路径
                    thumbFileSavePath = request.ThumbFileServerRootDirectory + request.CategoryAbsolutePath + request.FileName;
                    using (FileStream outputStream = new FileStream(thumbFileSavePath, FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        request.FileData.CopyTo(outputStream);
                        outputStream.Flush();
                    }
                }
                else if (request.FileName == "video.rar" || request.FileName.Substring(0, 4) == "big_") //视频或预览图
                {
                    //直接保存到原始图路径
                    using (FileStream outputStream = new FileStream(originalFileSavePath, FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        request.FileData.CopyTo(outputStream);
                        outputStream.Flush();
                    }
                }
                else  //即要保存原始图,又要生成缩略图
                {
                    using (FileStream outputStream = new FileStream(originalFileSavePath, FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        request.FileData.CopyTo(outputStream);
                        outputStream.Flush();
                    }

                    //不是失量文件,则要生成缩略图
                    if (!request.IsVector)
                    {
                        thumbFileSavePath = request.ThumbFileServerRootDirectory + request.CategoryAbsolutePath + "sm_" + request.FileName;

                        if (request.IsThumbSquare)
                        {
                            Tools.CreatePictureThumbFromCenter(originalFileSavePath, thumbFileSavePath);
                        }
                        else
                        {
                            Tools.CreatePictureThumbFromCenter(originalFileSavePath, thumbFileSavePath, request.ThumbPictureWidth, request.ThumbPictureHeight);
                        }
                    }

                    // if 文件之前不存在,则添加记录到数据库
                    if (!isOriginalFileExists)
                    {
                        if (request.CategoryType == CategoryType.Picture)
                        {
                            DbCommand cmd = _dataBaseAccess.CreateCommand();
                            cmd.CommandText = "Proc_PictureFiles_Insert";
                            FileInsertParamAdd(ref cmd, request);

                            DbParameter param = _dataBaseAccess.CreateParameter();
                            param.ParameterName = "@LevelCategoryName";
                            param.DbType = DbType.String;
                            param.Value = request.LevelCategoryName;
                            cmd.Parameters.Add(param);

                            _dataBaseAccess.ExecuteCommand(cmd);
                        }
                        else if (request.CategoryType == CategoryType.Gallery)
                        {
                            DbCommand cmd = _dataBaseAccess.CreateCommand();
                            cmd.CommandText = "Proc_GalleryFiles_Insert";
                            FileInsertParamAdd(ref cmd, request);

                            DbParameter param = _dataBaseAccess.CreateParameter();
                            param.ParameterName = "@FileName";
                            param.DbType = DbType.String;
                            param.Value = request.FileName;
                            cmd.Parameters.Add(param);

                            _dataBaseAccess.ExecuteCommand(cmd);
                        }
                    }
                }

                response.IsSuccessful = true;
                response.ResultMessage = string.Empty;
            }
            catch (Exception ex)
            {
                Tools.LogWrite(ex.ToString());
                try
                {
                    if (File.Exists(originalFileSavePath))
                    {
                        File.Delete(originalFileSavePath);
                    }

                    if (File.Exists(thumbFileSavePath))
                    {
                        File.Delete(thumbFileSavePath);
                    }
                }
                catch (Exception exception)
                {
                    Tools.LogWrite(exception.ToString());
                }

                response.IsSuccessful = false;
                response.ResultMessage = ex.ToString();
            }
            finally
            {
                request.FileData.Dispose();
            }
            return response;
        }