/// <summary>
        /// Retrieve a Stream to download and decrypt the specified Uri
        /// </summary>
        /// <param name="uri">Uri to download</param>
        /// <exception cref="NotSupportedException">Not logged in</exception>
        /// <exception cref="ApiException">Mega.co.nz service reports an error</exception>
        /// <exception cref="ArgumentNullException">uri is null</exception>
        /// <exception cref="ArgumentException">Uri is not valid (id and key are required)</exception>
        /// <exception cref="DownloadException">Checksum is invalid. Downloaded data are corrupted</exception>
        public Stream Download(Uri uri, CancellationToken?cancellationToken = null)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }

            EnsureLoggedIn();

            string id;

            byte[] iv, metaMac, key;
            GetPartsFromUri(uri, out id, out iv, out metaMac, out key);

            // Retrieve download URL
            DownloadUrlRequestFromId downloadRequest  = new DownloadUrlRequestFromId(id);
            DownloadUrlResponse      downloadResponse = Request <DownloadUrlResponse>(downloadRequest);

            Stream dataStream = webClient.GetRequestRaw(new Uri(downloadResponse.Url));

            Stream resultStream = new MegaAesCtrStreamDecrypter(dataStream, downloadResponse.Size, key, iv, metaMac);

            if (cancellationToken.HasValue)
            {
                resultStream = new CancellableStream(resultStream, cancellationToken.Value);
            }

            return(resultStream);
        }
示例#2
0
 internal NodeInfo(string id, DownloadUrlResponse downloadResponse, byte[] key)
 {
     Id         = id;
     Attributes = Crypto.DecryptAttributes(downloadResponse.SerializedAttributes.FromBase64(), key);
     Size       = downloadResponse.Size;
     Type       = NodeType.File;
 }
        /// <summary>
        /// Retrieve public properties of a file from a specified Uri
        /// </summary>
        /// <param name="uri">Uri to retrive properties</param>
        /// <exception cref="NotSupportedException">Not logged in</exception>
        /// <exception cref="ApiException">Mega.co.nz service reports an error</exception>
        /// <exception cref="ArgumentNullException">uri is null</exception>
        /// <exception cref="ArgumentException">Uri is not valid (id and key are required)</exception>
        public INodeInfo GetNodeFromLink(Uri uri)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }

            EnsureLoggedIn();

            string id;

            byte[] iv, metaMac, key;
            GetPartsFromUri(uri, out id, out iv, out metaMac, out key);

            // Retrieve attributes
            DownloadUrlRequestFromId downloadRequest  = new DownloadUrlRequestFromId(id);
            DownloadUrlResponse      downloadResponse = Request <DownloadUrlResponse>(downloadRequest);

            return(new NodeInfo(id, downloadResponse, key));
        }
        /// <summary>
        /// Retrieve a Stream to download and decrypt the specified node
        /// </summary>
        /// <param name="node">Node to download (only <see cref="NodeType.File" /> can be downloaded)</param>
        /// <exception cref="NotSupportedException">Not logged in</exception>
        /// <exception cref="ApiException">Mega.co.nz service reports an error</exception>
        /// <exception cref="ArgumentNullException">node or outputFile is null</exception>
        /// <exception cref="ArgumentException">node is not valid (only <see cref="NodeType.File" /> can be downloaded)</exception>
        /// <exception cref="DownloadException">Checksum is invalid. Downloaded data are corrupted</exception>
        public Stream Download(INode node, CancellationToken?cancellationToken = null)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            if (node.Type != NodeType.File)
            {
                throw new ArgumentException("Invalid node");
            }

            INodeCrypto nodeCrypto = node as INodeCrypto;

            if (nodeCrypto == null)
            {
                throw new ArgumentException("node must implement INodeCrypto");
            }

            EnsureLoggedIn();

            // Retrieve download URL
            DownloadUrlRequest  downloadRequest  = new DownloadUrlRequest(node);
            DownloadUrlResponse downloadResponse = Request <DownloadUrlResponse>(downloadRequest);

            Stream dataStream = webClient.GetRequestRaw(new Uri(downloadResponse.Url));

            Stream resultStream = new MegaAesCtrStreamDecrypter(dataStream, downloadResponse.Size, nodeCrypto.Key, nodeCrypto.Iv, nodeCrypto.MetaMac);

            if (cancellationToken.HasValue)
            {
                resultStream = new CancellableStream(resultStream, cancellationToken.Value);
            }

            return(resultStream);
        }