Beispiel #1
0
        public async Task <string> CacheImage(byte[] bytes)
        {
            var outputFile = CreateTempFile();

            System.IO.MemoryStream s = new System.IO.MemoryStream(bytes);
            using (System.IO.FileStream fileStream = System.IO.File.OpenWrite(outputFile.AbsolutePath))
            {
                await s.CopyToAsync(fileStream);

                await s.FlushAsync();

                await fileStream.FlushAsync();
            }
            return(outputFile.AbsolutePath);
        }
        static int _m_FlushAsync(RealStatePtr L)
        {
            try {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);


                System.IO.FileStream gen_to_be_invoked = (System.IO.FileStream)translator.FastGetCSObj(L, 1);



                {
                    System.Threading.CancellationToken _cancellationToken; translator.Get(L, 2, out _cancellationToken);

                    System.Threading.Tasks.Task gen_ret = gen_to_be_invoked.FlushAsync(_cancellationToken);
                    translator.Push(L, gen_ret);



                    return(1);
                }
            } catch (System.Exception gen_e) {
                return(LuaAPI.luaL_error(L, "c# exception:" + gen_e));
            }
        }
Beispiel #3
0
        public async Task <string> DownloadAndCacheImage(int index, string url)
        {
            var outputFile = CreateTempFile();

            var httpReq = (HttpWebRequest)WebRequest.Create(new Uri(url));

            httpReq.Method = "GET";
            using (var response = await httpReq.GetResponseAsync())
            {
                using (System.IO.Stream s = response.GetResponseStream())
                {
                    using (System.IO.FileStream fileStream = System.IO.File.OpenWrite(outputFile.AbsolutePath))
                    {
                        await s.CopyToAsync(fileStream);

                        await s.FlushAsync();

                        await fileStream.FlushAsync();
                    }
                }
            }

            return(outputFile.AbsolutePath);
        }
Beispiel #4
0
 public override Task FlushAsync(CancellationToken cancellationToken)
 {
     return(InternalFileStream.FlushAsync(cancellationToken));
 }
        private static async System.Threading.Tasks.Task <ResultType> TaskMain(Fee.File.OnFileTask_CallBackInterface a_callback_interface, Path a_path, Fee.TaskW.CancelToken a_cancel)
                #endif
        {
            ResultType t_ret;

            {
                t_ret.binary      = null;
                t_ret.errorstring = null;
            }

            Fee.TaskW.TaskW.GetInstance().Post((a_state) => {
                a_callback_interface.OnFileTask(0.1f);
            }, null);

            System.IO.FileStream t_filestream = null;

            try{
                //ファイルパス。
                System.IO.FileInfo t_fileinfo = new System.IO.FileInfo(a_path.GetPath());

                //開く。
                t_filestream = t_fileinfo.OpenRead();

                //読み込み。
                if (t_filestream != null)
                {
                    t_ret.binary = new byte[t_filestream.Length];
                    if (Config.USE_ASYNC == true)
                    {
                                                #if ((UNITY_5) || (UNITY_WEBGL))
                        Tool.Assert(false);
                                                #else
                        await t_filestream.ReadAsync(t_ret.binary, 0, t_ret.binary.Length, a_cancel.GetToken());

                        await t_filestream.FlushAsync(a_cancel.GetToken());
                                                #endif
                    }
                    else
                    {
                        t_filestream.Read(t_ret.binary, 0, t_ret.binary.Length);
                    }
                }
            }catch (System.Exception t_exception) {
                t_ret.binary      = null;
                t_ret.errorstring = "Task_LoadLocalBinaryFile : " + t_exception.Message;
            }

            //閉じる。
            if (t_filestream != null)
            {
                t_filestream.Close();
            }

            if (a_cancel.IsCancellationRequested() == true)
            {
                t_ret.binary      = null;
                t_ret.errorstring = "Task_LoadLocalBinaryFile : Cancel";

                a_cancel.ThrowIfCancellationRequested();
            }

            if (t_ret.binary == null)
            {
                if (t_ret.errorstring == null)
                {
                    t_ret.errorstring = "Task_LoadLocalBinaryFile : null";
                }
            }

            return(t_ret);
        }
        public async Task <IActionResult> DownloadMeasureTemplate(Guid metricID, string format)
        {
            var metric = await _db.Metrics.Where(m => m.ID == metricID).Select(m => new { m.Title, ResultsType = m.ResultsType.Value }).FirstOrDefaultAsync();

            if (metric == null)
            {
                return(NotFound());
            }

            if (string.Equals("json", format, StringComparison.OrdinalIgnoreCase))
            {
                var measure = new Models.MeasureSubmissionViewModel {
                    MetricID    = metricID,
                    ResultsType = metric.ResultsType,
                    Measures    = new HashSet <Models.MeasurementSubmissionViewModel> {
                        new Models.MeasurementSubmissionViewModel {
                            Definition = string.Empty, RawValue = string.Empty, Measure = 0, Total = 0
                        }
                    }
                };

                var serializerSettings = new Newtonsoft.Json.JsonSerializerSettings {
                    Formatting = Formatting.Indented, DateFormatString = "'yyyy-MM-dd'"
                };
                var result = new FileContentResult(System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(measure, serializerSettings)), Microsoft.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json"))
                {
                    FileDownloadName = CleanFilename("MeasureSubmission_" + metric.Title) + ".json"
                };

                return(result);
            }
            else if (string.Equals("xlsx", format, StringComparison.OrdinalIgnoreCase))
            {
                using (var scope = _serviceProvider.CreateScope())
                {
                    var hostingEnvironment = scope.ServiceProvider.GetService <IWebHostEnvironment>();

                    System.IO.MemoryStream ms = new System.IO.MemoryStream();

                    using (var fs = new System.IO.FileStream(System.IO.Path.Combine(hostingEnvironment.WebRootPath, "assets", "MeasureSubmission_template.xlsx"), System.IO.FileMode.Open, System.IO.FileAccess.Read))
                    {
                        await fs.CopyToAsync(ms);

                        await fs.FlushAsync();
                    }

                    ms.Position = 0;

                    using (var document = DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open(ms, true))
                    {
                        var helper = new Utils.MeasuresExcelReader(document);
                        helper.UpdateMetricInformation(metricID, metric.ResultsType);
                        helper.Document.Save();
                        document.Close();
                    }

                    ms.Position = 0;

                    var result = new FileStreamResult(ms, Microsoft.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
                    {
                        FileDownloadName = CleanFilename("MeasureSubmission_" + metric.Title + ".xlsx")
                    };

                    return(result);
                }
            }

            return(BadRequest("Invalid document format, must be either 'json' or 'xlsx'."));
        }
        private static async System.Threading.Tasks.Task <ResultType> TaskMain(Fee.File.OnFileTask_CallBackInterface a_callback_interface, Path a_path, byte[] a_binary, Fee.TaskW.CancelToken a_cancel)
                #endif
        {
            ResultType t_ret;

            {
                t_ret.saveend     = false;
                t_ret.errorstring = null;
            }

            System.IO.FileStream t_filestream = null;

            try{
                //ファイルパス。
                System.IO.FileInfo t_fileinfo = new System.IO.FileInfo(a_path.GetPath());

                //開く。
                t_filestream = t_fileinfo.Create();

                //書き込み。
                if (t_filestream != null)
                {
                    if (a_binary != null)
                    {
                        if (Config.USE_ASYNC == true)
                        {
                                                        #if ((UNITY_5) || (UNITY_WEBGL))
                            Tool.Assert(false);
                                                        #else
                            await t_filestream.WriteAsync(a_binary, 0, a_binary.Length, a_cancel.GetToken());

                            await t_filestream.FlushAsync(a_cancel.GetToken());
                                                        #endif
                        }
                        else
                        {
                            t_filestream.Write(a_binary, 0, a_binary.Length);
                            t_filestream.Flush();
                        }
                        t_ret.saveend = true;
                    }
                    else
                    {
                        t_ret.saveend     = false;
                        t_ret.errorstring = "Task_SaveLocalBinaryFile : binary == null";
                    }
                }
            }catch (System.Exception t_exception) {
                t_ret.saveend     = false;
                t_ret.errorstring = "Task_SaveLocalBinaryFile : " + t_exception.Message;
            }

            //閉じる。
            if (t_filestream != null)
            {
                t_filestream.Close();
            }

            Platform.Platform.GetInstance().SyncFs();

            if (a_cancel.IsCancellationRequested() == true)
            {
                t_ret.saveend     = false;
                t_ret.errorstring = "Task_SaveLocalBinaryFile : Cancel";

                a_cancel.ThrowIfCancellationRequested();
            }

            if (t_ret.saveend == false)
            {
                if (t_ret.errorstring == null)
                {
                    t_ret.errorstring = "Task_SaveLocalBinaryFile : null";
                }
            }

            return(t_ret);
        }