コード例 #1
0
ファイル: Files.cs プロジェクト: mtm9999/kilogram
        public async Task <string> DownloadVideo(Video arg, FileUploadProcessHandler handler)
        {
            if (arg.Constructor == Constructor.videoEmpty)
            {
                return(null);
            }

            VideoConstructor video = (VideoConstructor)arg;
            TLApi            api   = await session.GetFileSession(video.dc_id);

            InputFileLocation inputFile     = TL.inputVideoFileLocation(video.id, video.access_hash);
            string            videoPath     = GetVideoPath(video);
            string            tempVideoPath = videoPath + ".tmp";

            int allSize        = video.size;
            int chunkSize      = 128 * 1024;
            int chunksCount    = allSize / chunkSize;
            int lastChunkSize  = allSize - chunkSize * chunksCount;
            int allChunksCount = chunksCount + (lastChunkSize != 0 ? 1 : 0);

            using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication()) {
                if (storage.FileExists(videoPath))
                {
                    return(videoPath);
                }

                using (Stream stream = new IsolatedStorageFileStream(tempVideoPath, FileMode.OpenOrCreate, FileAccess.Write, storage)) {
                    for (int i = 0; i < chunksCount; i++)
                    {
                        handler((float)i * (float)chunkSize / (float)allSize);
                        Upload_fileConstructor chunk = (Upload_fileConstructor)await api.upload_getFile(inputFile, i *chunkSize, chunkSize);

                        stream.Write(chunk.bytes, 0, chunk.bytes.Length);
                    }

                    if (lastChunkSize != 0)
                    {
                        handler((float)chunksCount * (float)chunkSize / (float)allSize);
                        Upload_fileConstructor lastChunk = (Upload_fileConstructor)await api.upload_getFile(inputFile, chunksCount *chunkSize, lastChunkSize);

                        stream.Write(lastChunk.bytes, 0, lastChunk.bytes.Length);
                    }

                    handler(1.0f);
                }

                if (storage.FileExists(videoPath))
                {
                    storage.DeleteFile(videoPath);
                }

                storage.MoveFile(tempVideoPath, videoPath);
            }

            return(videoPath);
        }