private void FileInsertParamAdd(ref DbCommand cmd, FileUploadRequest request)
        {
            DbParameter param = _dataBaseAccess.CreateParameter();
            param.ParameterName = "@StoreTableName";
            param.DbType = DbType.String;
            param.Value = request.StoreTableName;
            cmd.Parameters.Add(param);

            param = _dataBaseAccess.CreateParameter();
            param.ParameterName = "@CategoryId";
            param.DbType = DbType.Int32;
            param.Value = request.CategoryId;
            cmd.Parameters.Add(param);

            param = _dataBaseAccess.CreateParameter();
            param.ParameterName = "@FileServerId";
            param.DbType = DbType.Int32;
            param.Value = request.FileServerId;
            cmd.Parameters.Add(param);

            string thumbFileName = Path.GetFileNameWithoutExtension(request.FileName) + ".jpg";

            param = _dataBaseAccess.CreateParameter();
            param.ParameterName = "@ThumbRelativeFilePath";
            param.DbType = DbType.String;
            param.Value = "/Small" + request.CategoryRelativePath + "sm_" + thumbFileName;
            cmd.Parameters.Add(param);

            param = _dataBaseAccess.CreateParameter();
            param.ParameterName = "@PreViewRelativeFilePath";
            param.DbType = DbType.String;
            param.Value = "/Big" + request.CategoryRelativePath + "big_" + thumbFileName;
            cmd.Parameters.Add(param);

            string originalRelativeFilePath = string.Empty;
            if (request.IsVector)
            {
                string[] vectorFileNames = request.VectorFileName.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string vectorFileName in vectorFileNames)
                {
                    originalRelativeFilePath += string.Format("/Big{0}{1}|", request.CategoryRelativePath, vectorFileName);
                }
                originalRelativeFilePath = originalRelativeFilePath.Substring(0, originalRelativeFilePath.Length - 1);
            }
            else
            {
                originalRelativeFilePath = string.Format("/Big{0}{1}", request.CategoryRelativePath, request.FileName);
            }

            param = _dataBaseAccess.CreateParameter();
            param.ParameterName = "@OriginalRelativeFilePath";
            param.DbType = DbType.String;
            param.Value = originalRelativeFilePath;
            cmd.Parameters.Add(param);

            param = _dataBaseAccess.CreateParameter();
            param.ParameterName = "@IsVector";
            param.DbType = DbType.Boolean;
            param.Value = request.IsVector;
            cmd.Parameters.Add(param);
        }
Beispiel #2
0
        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--;
                    }
                }
            }
        }
        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;
        }