예제 #1
0
        public async Task <StreamIdentifierFileUploadResult> StartUpload(FileInfo <TId> fileInfo, Stream streamForRead, long startPosition, long length, Action <long> wrotePositionAction = null, bool trowException = false)
        {
            try
            {
                string folderPath = GetFolderPath(fileInfo.Id);

                if (!CrossDirectoryInfo.Current.Exists(folderPath))
                {
                    CrossDirectoryInfo.Current.CreateDirectory(folderPath);
                }
                string filePath = Path.Combine(folderPath, "file");
                string dataPath = Path.Combine(folderPath, "data");
                SaveFileInfoToFile(fileInfo, dataPath, true);
                using (FileStream fileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    fileStream.Seek(startPosition, SeekOrigin.Begin);
                    byte[] readBytes = new byte[1024 * 512];
                    long   writed    = 0;
                    while (true)
                    {
                        int readCount = 0;
                        try
                        {
                            if (readBytes.Length > length - writed)
                            {
                                readBytes = new byte[length - writed];
                            }
                            readCount = await streamForRead.ReadAsync(readBytes, 0, readBytes.Length);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("read exception : " + ex.ToString());
                            break;
                        }
                        if (readCount <= 0)
                        {
                            break;
                        }
                        fileStream.Write(readBytes, 0, readCount);
                        writed += readCount;
                        wrotePositionAction?.Invoke(fileStream.Position);
                        if (writed == length)
                        {
                            break;
                        }
                    }
                    if (fileStream.Length > fileInfo.FileSize)
                    {
                        fileStream.SetLength(fileInfo.FileSize);
                    }
                    if (fileStream.Length == fileInfo.FileSize)
                    {
                        fileStream.Seek(0, SeekOrigin.Begin);
                        if (fileInfo.FileChecksumInfo != null)
                        {
                            FileChecksumInfo checkSum = FileChecksumMaker.GetFileCheckSum(fileStream);
                            if (checkSum != null)
                            {
                                List <CheckSumItemInfo> errors = FileChecksumMaker.GetErrorsFromTwoCheckSum(checkSum, fileInfo.FileChecksumInfo);
                                if (errors.Count == 0)
                                {
                                    fileInfo.IsComplete = true;
                                    if (!IgnoreLastUpdateDateTime)
                                    {
                                        fileInfo.LastUpdateDateTime = DateTime.Now;
                                    }
                                    SaveFileInfoToFile(fileInfo, dataPath, false);
                                    return(StreamIdentifierFileUploadResult.Success);
                                }
                                else
                                {
                                    return(StreamIdentifierFileUploadResult.FileCheckSumIsWrong);
                                }
                            }
                            else
                            {
                                fileInfo.IsComplete = true;
                                if (!IgnoreLastUpdateDateTime)
                                {
                                    fileInfo.LastUpdateDateTime = DateTime.Now;
                                }
                                SaveFileInfoToFile(fileInfo, dataPath, false);
                                return(StreamIdentifierFileUploadResult.Success);
                            }
                        }
                        else
                        {
                            fileInfo.IsComplete = true;
                            if (!IgnoreLastUpdateDateTime)
                            {
                                fileInfo.LastUpdateDateTime = DateTime.Now;
                            }
                            SaveFileInfoToFile(fileInfo, dataPath, false);
                            return(StreamIdentifierFileUploadResult.Success);
                        }
                    }
                    else
                    {
                        return(StreamIdentifierFileUploadResult.FileSizeIsWrong);
                    }
                }
            }
            catch (Exception ex)
            {
                if (trowException)
                {
                    throw;
                }
                return(StreamIdentifierFileUploadResult.Exception);
            }
            finally
            {
            }
        }
예제 #2
0
        public StreamIdentifierFileUploadResult StartUpload(FileInfoCache fileInfoCache, Stream streamToSave, long startPosition, long length)
        {
            try
            {
                CurrentFileInfoCache = fileInfoCache;
                CurrentStream        = streamToSave;
                bool haveToRemove = false;
                StreamIdentifierCache disposeStreamIdentifier = null;
                lock (staticLock)
                {
                    if (UploadingStreamsByGuid.TryGetValue(fileInfoCache.Id, out disposeStreamIdentifier))
                    {
                        haveToRemove = true;
                    }
                    else
                    {
                        UploadingStreamsByGuid.TryAdd(fileInfoCache.Id, this);
                    }
                }
                if (haveToRemove)
                {
                    disposeStreamIdentifier.Dispose();
                }

                var folderPath = GetCachFolderPath(fileInfoCache.Id);

                if (!CrossDirectoryInfo.Current.Exists(folderPath))
                {
                    CrossDirectoryInfo.Current.CreateDirectory(folderPath);
                }
                var filePath = Path.Combine(folderPath, "file");
                var dataPath = Path.Combine(folderPath, "data");
                CrossFileInfo.Current.WriteAllText(dataPath, JsonConvert.SerializeObject(fileInfoCache), Encoding.UTF8);
                using (FileStream fileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                {
                    fileStream.Seek(startPosition, SeekOrigin.Begin);
                    byte[] readBytes = new byte[1024 * 200];
                    long   writed    = 0;
                    while (!isDisposed)
                    {
                        var readCount = streamToSave.Read(readBytes, 0, readBytes.Length);
                        if (readCount <= 0)
                        {
                            break;
                        }
                        fileStream.Write(readBytes, 0, readCount);
                        writed += readCount;
                        if (writed == length)
                        {
                            break;
                        }
                    }
                    if (fileStream.Length > fileInfoCache.FileSize)
                    {
                        fileStream.SetLength(fileInfoCache.FileSize);
                    }
                    if (fileStream.Length == fileInfoCache.FileSize)
                    {
                        fileStream.Seek(0, SeekOrigin.Begin);
                        var checkSum = FileChecksumMaker.GetFileCheckSum(fileStream);
                        var errors   = FileChecksumMaker.GetErrorsFromTwoCheckSum(checkSum, fileInfoCache.FileChecksumInfo);
                        if (errors.Count == 0)
                        {
                            return(StreamIdentifierFileUploadResult.Success);
                        }
                        else
                        {
                            return(StreamIdentifierFileUploadResult.FileCheckSumIsWrong);
                        }
                    }
                    else
                    {
                        return(StreamIdentifierFileUploadResult.FileSizeIsWrong);
                    }
                }
            }
            catch (Exception ex)
            {
                return(StreamIdentifierFileUploadResult.Exception);
            }
            finally
            {
                Dispose();
            }
        }