Exemplo n.º 1
0
        public void Run()
        {
            var streamTask = Audio.GetContentAsync();

            streamTask.Wait();
            var stream = streamTask.Result;

            if (stream == null)
            {
                State = LongTaskState.done;
                return;
            }

            State = LongTaskState.running;
            string tempFile;

            using (stream)
            {
                tempFile = Path.Combine(Audio.context.tempDir, Id);
                if (Audio.MimeToExtension.ContainsKey(Audio.node.mime))
                {
                    tempFile += "." + Audio.MimeToExtension[Audio.node.mime];
                }

                using (var tmpStream = File.OpenWrite(tempFile))
                    stream.CopyTo(tmpStream);
            }

            try
            {
                Blob audioMp3Blob = new Blob
                {
                    id        = Guid.NewGuid().ToString(),
                    parent_id = Audio.node.blob_id,
                    mimetype  = "audio/mpeg",
                    name      = "webaudio"
                };
                var audioMp3File = BuildMp3(tempFile);
                if (audioMp3File != null)
                {
                    using (DB db = DB.Create(Audio.context.docs.dbUrl, true))
                    {
                        var task = Audio.context.blobs.CreateBlobFromTempFileAsync(db, audioMp3Blob, audioMp3File);
                        task.Wait();
                        audioMp3Blob = task.Result;
                        db.CommitAsync().Wait();
                    }
                    Blob = audioMp3Blob;
                }
                else
                {
                    Audio.context.docs.logger.Log(LogLevel.Error, $"Error while encoding file '{Audio.node.name}' (id: {Audio.node.id}).");
                    // write a zero size blob to signal the encoding failure
                    using (DB db = DB.Create(Audio.context.docs.dbUrl, true))
                    {
                        var task = Audio.context.blobs.CreateBlobAsync(db, new FileDefinition(), audioMp3Blob);
                        task.Wait();
                        audioMp3Blob = task.Result;
                        db.CommitAsync().Wait();
                    }
                    Blob = audioMp3Blob;
                }
            }
            finally
            {
                State = LongTaskState.done;
                File.Delete(tempFile);
            }
            tcs.TrySetResult(Blob);
        }
Exemplo n.º 2
0
        public async Task <Stream> GetPdfStreamAsync()
        {
            await node.blob.LoadExpandFieldAsync(context.db, "children");

            Console.WriteLine(node.blob.children.Dump());

            var pdfBlob = node.blob.children.Find(child => child.name == "pdf");

            if (pdfBlob != null)
            {
                return(context.blobs.GetBlobStream(pdfBlob.id));
            }

            Stream pdfStream = null;
            OnlyOfficeDocumentType documentType;
            OnlyOfficeFileType     fileType;

            NodeToFileType(node, out documentType, out fileType);
            var file = new Docs.TempFile
            {
                Id     = Guid.NewGuid() + "-" + StringExt.RandomSecureString(),
                Mime   = node.mime,
                Stream = await GetContentAsync()
            };

            lock (context.docs.tempFilesLock)
            {
                context.docs.tempFiles[file.Id] = file;
            }
            try
            {
                var fileUri    = new Uri(new Uri(context.docs.globalSetup.server.publicUrl), $"/api/docs/tempFile/{file.Id}");
                var convertUrl = await ConvertAsync(context.docs.logger, context.httpContext, fileUri.ToString(), fileType, OnlyOfficeFileType.pdf);

                if (convertUrl != null)
                {
                    var convertUri = new Uri(convertUrl);
                    using (var client = HttpClient.Create(convertUri))
                    {
                        HttpClientResponse response;
                        HttpClientRequest  request = new HttpClientRequest();
                        request.Method = "GET";
                        request.Path   = convertUri.PathAndQuery;
                        client.SendRequest(request);
                        response = client.GetResponse();
                        if (response.StatusCode == 200)
                        {
                            var fileDefinition = new FileDefinition
                            {
                                Stream   = response.InputStream,
                                Mimetype = "application/pdf",
                                Name     = "pdf"
                            };

                            pdfBlob = new Blob
                            {
                                parent_id = node.blob_id,
                                id        = Guid.NewGuid().ToString(),
                                name      = "pdf",
                                mimetype  = "application/pdf"
                            };
                            pdfBlob = await context.blobs.CreateBlobAsync(context.db, fileDefinition, pdfBlob);

                            pdfStream = context.blobs.GetBlobStream(pdfBlob.id);
                        }
                    }
                }
            }
            finally
            {
                lock (context.docs.tempFilesLock)
                {
                    context.docs.tempFiles.Remove(file.Id);
                }
                file.Stream.Close();
            }
            return(pdfStream);
        }