Exemple #1
0
        /// <summary>
        /// 文件二进制流上传
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public object Post(FileUploadRequest request)
        {
            int code = (int)FS_ErrorCode.Request_Success;

            if (null == request.file_buffer || request.file_buffer.Length == 0)
            {
                LogUtil.Info("Error: request.RequestStream is null.");
                code = (int)FS_ErrorCode.RequestParam_Err;
                return new FileUploadResponse { ret = code, message = ErrorCodeDic.GetInstance().CodeMessage(code), File_guid = string.Empty };
            }
            if (string.IsNullOrEmpty(request.file_name))
            {
                LogUtil.Info("Error: request.FileName is null.");
                code = (int)FS_ErrorCode.RequestParam_Err;
                return new FileUploadResponse { ret = code, message = ErrorCodeDic.GetInstance().CodeMessage(code), File_guid = string.Empty };
            }

            FileUploadResponse response = new FileUploadResponse();

            try
            {
                ICommonFilesService fileSvr = new CommonFilesService();
                response = fileSvr.FileUpload(request.file_name, request.file_size, request.file_buffer);
            }
            catch (Exception e)
            {
                string err = string.Format("CommonFileServiceHost.AsyncFileDownloadRequest error = {0}, request = {1}", e.ToString(), JsonUtil<FileUploadRequest>.ToJson(request));
                LogUtil.Error(err);
            }

            return response;
        }
        public FileUploadResponse FileUpload(string fileName, long file_size, byte[] file_buffer)
        {
            FileUploadResponse response = new FileUploadResponse();
            try
            {
                string filePath = string.Empty;
                string fileExtName = fileName.Split('.')[1];
                string fileGuid = GetShortGuid();

                // 计算MD5值, 若相同则不传物理文件至FDFS,只保存一条文件记录
                String fileMD5 = FileOperationHelper.CalculateChunkMD5(file_buffer);
                FileEntity fileEntity = fileDA_R.GetFileEntityByMD5(fileMD5);

                if (fileEntity != null && fileEntity.file_md5.Equals(fileMD5))  // 文件已存在
                {
                    FileEntity newEntity = new FileEntity
                    {
                        file_guid = fileGuid,
                        file_full_name = fileName,
                        file_path = fileEntity.file_path,
                        file_md5 = fileMD5,
                        file_size = file_size,
                        create_time = StringUtil.GetDateTimeSeconds(DateTime.Now)
                    };
                    fileDA_W.CreateNewFile(newEntity);
                }
                else
                {
                    // 异步上传新文件
                    Stopwatch watch = new Stopwatch();
                    watch.Start();
                    FSManager.BeginUploadFile(file_buffer, fileName, fileExtName, file_size, (uploadResult, uploadEx) =>
                    {
                        if (null != uploadEx)
                        {
                            LogUtil.Error(string.Format("Error in FSManager.BeginUploadFile callback: {0}", uploadEx.ToString()));
                            errorCode = (int)FS_ErrorCode.RequestFile_UploadErr;
                            return;
                        }
                        if (null == uploadResult || string.IsNullOrEmpty(uploadResult.FilePathOnFDFS))
                        {
                            LogUtil.Error(string.Format("Error in FSManager.BeginUploadFile callback:fdfs file Pash is null. FileName={0},fileSize={1}", fileName, file_size));
                            errorCode = (int)FS_ErrorCode.RequestFile_UploadErr;
                            return;
                        }

                        watch.Stop();
                        string info = string.Format("Async upload at server(ProcessId = {0}, ThreadId = {1}, CurrTheadsNum = {2}): file_guid = {3}, file_full_name = {4},"
                                    +" file_path = {5}, file_md5 = {6}, file_size = {7}, invoking time = {8}ms", Process.GetCurrentProcess().Id, Thread.CurrentThread.ManagedThreadId, Process.GetCurrentProcess().Threads.Count, fileGuid, fileName, uploadResult.FilePathOnFDFS, fileMD5, file_size, watch.ElapsedMilliseconds);
                        if (watch.ElapsedMilliseconds > 3000)
                            LogUtil.Error(info);
                        else
                            LogUtil.Info(info);

                        // 保存FileGuid ,filePath 和FileName, FileSize到数据库
                        watch.Start();
                        FileEntity entity = new FileEntity
                        {
                            file_guid = fileGuid,
                            file_full_name = fileName,
                            file_path = uploadResult.FilePathOnFDFS,
                            file_md5 = fileMD5,
                            file_size = file_size,
                            create_time = StringUtil.GetDateTimeSeconds(DateTime.Now)
                        };
                        fileDA_W.CreateNewFile(entity);
                        watch.Stop();
                        info = string.Format("after async upload: Save to mysql db cost {0}ms. file_guid={1}, file_full_name={2}", watch.ElapsedMilliseconds, entity.file_guid, entity.file_full_name);
                        if (watch.ElapsedMilliseconds > 3000)
                            LogUtil.Error(info);
                        else
                            LogUtil.Info(info);
                    });
                }

                response.ret = errorCode;
                response.message = ErrorCodeDic.GetInstance().CodeMessage(errorCode);
                response.File_guid = fileGuid;
            }
            catch (Exception ex)
            {
                LogUtil.Error(string.Format("Exception FSService.Post(): {0}", ex.ToString()));
                errorCode = (int)FS_ErrorCode.RequestParam_Err;
                response.ret = errorCode;
                response.message = ErrorCodeDic.GetInstance().CodeMessage(errorCode);
                response.File_guid = string.Empty;
            }
            return response;
        }