Beispiel #1
0
        /// <summary>
        /// Extracts files from multipart MIME format from stram and adds them to repository.
        /// </summary>
        /// <param name="stream">The stream with files in multipart MIME format.</param>
        /// <returns>Collection of orginal file names and corresponding identifiers that are used instead of file name to retrieve files from repository.</returns>
        public IEnumerable <UploadedFile> AddFilesFromStream(Stream stream)
        {
            ICollection <UploadedFile> files = UploadHelper.ExtractFiles(stream);

            foreach (UploadedFile file in files)
            {
                file.FileIdentifier = Guid.NewGuid().ToString().ToUpperInvariant();

                SaveFile(file.Data, file.FileIdentifier);
            }
            return(files);
        }
        public override IEnumerable <UploadedFile> AddMultipleFiles(Stream files)
        {
            Autenticate();

            WebRequest request = WebRequest.Create(new Uri(this.Url + "/PutFile"));

            request.Method      = "POST";
            request.ContentType = "application/octet-stream";

            Stream requestStream = request.GetRequestStream();
            var    bArray        = files.ToByteArray();
            int    position      = 0;
            int    bytesToWrite  = 0;
            int    chunk         = 1024;

            while (position < bArray.LongLength)
            {
                bytesToWrite = (bArray.LongLength - position < chunk) ? (int)bArray.LongLength - position : chunk;
                requestStream.Write(bArray, position, bytesToWrite);
                position += bytesToWrite;
            }
            requestStream.Close();

            XDocument responseXml = null;

            using (StreamReader responseReader = new StreamReader(request.GetResponse().GetResponseStream()))
            {
                responseXml = XDocument.Parse(responseReader.ReadToEnd());
            }

            var uploadedFiles = UploadHelper.ExtractFiles(bArray);

            foreach (UploadedFile file in uploadedFiles)
            {
                file.FileIdentifier = (from node in responseXml.Root.Elements("file")
                                       where node.Attribute("oldFilename").Value == file.FileName
                                       select node.Attribute("newFilename").Value).ElementAt(0);

                var descriptor = AddDocumentDescriptor(file.FileName, file.FileIdentifier);

                if (this.IsCachingEnabled)
                {
                    using (FileStream fs = new FileStream(System.IO.Path.Combine(this.Path, file.FileIdentifier), FileMode.Create, FileAccess.Write))
                    {
                        fs.Write(file.Data, 0, file.Data.Length);
                    }
                    this.WriteToConfig(descriptor.Id, descriptor.ModificationDate);
                }
            }
            return(uploadedFiles);
        }