示例#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);
        }
示例#2
0
文件: Files.cs 项目: mtm9999/kilogram
        public async Task <string> GetFile(FileLocation fileLocation)
        {
            logger.debug("Getting file {0}", fileLocation);
            if (fileLocation.Constructor == Constructor.fileLocation)
            {
                FileLocationConstructor location = (FileLocationConstructor)fileLocation;
                string filePath = FileLocationToCachePath(location);
                using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication()) {
                    if (!storage.DirectoryExists("cache"))
                    {
                        storage.CreateDirectory("cache");
                    }

                    if (storage.FileExists(filePath))
                    {
                        logger.debug("Getting file {0} from cache", filePath);
                        return(filePath);
                    }

                    TLApi api = await session.GetFileSession(location.dc_id);

                    logger.debug("Got file session for dc {0}", location.dc_id);

                    Upload_fileConstructor file = (Upload_fileConstructor)await api.upload_getFile(TL.inputFileLocation(location.volume_id, location.local_id, location.secret), 0, int.MaxValue);

                    logger.debug("File constructor found");

                    using (Stream fileStream = new IsolatedStorageFileStream(filePath, FileMode.OpenOrCreate,
                                                                             FileAccess.Write, storage)) {
                        await fileStream.WriteAsync(file.bytes, 0, file.bytes.Length);
                    }

                    logger.debug("File saved successfully");
                    return(filePath);
                }
            }
            else
            {
                throw new MTProtoFileUnavailableException();
            }
        }
示例#3
0
        //Limit req: value % 1024
        public async Task <Upload_fileConstructor> GetFile(InputFileLocation ifl, int size, int limit = 1024)
        {
            var result = new Upload_fileConstructor();

            result.bytes = new byte[size];
            GetFileRequest req;

            for (int recieved = 0; recieved < size; recieved += req.bytes.Length)
            {
                req = new GetFileRequest(ifl, recieved, limit);
                await _sender.Send(req);

                await _sender.Recieve(req);

                result.type  = req.type;
                result.mtime = req.mtime;

                req.bytes.CopyTo(result.bytes, recieved);
            }
            return(result);
        }