Ejemplo n.º 1
0
            //ExEnd:SourceMp3FilePath + SourceMp3DirectoryPath


            /// <summary>
            ///  Export metadata of Mp3 format to Excel.
            ///  Each new metadata type will be presented in separate sheet
            /// </summary>
            public static void ExportMetadataToExcel()
            {
                // path to the out dir
                string outputDir = directoryPath;

                // get mp3 files only
                string[] files = Directory.GetFiles(directoryPath, "*.mp3");

                foreach (string path in files)
                {
                    // get excel file as byte array
                    byte[] bytes = ExportFacade.ExportToExcel(path);

                    // prepare excel file name
                    string resultFileName = string.Format("{0}\\{1}.xlsx", outputDir, Path.GetFileNameWithoutExtension(path));

                    // remove output file if exist
                    if (File.Exists(resultFileName))
                    {
                        File.Delete(resultFileName);
                    }

                    // write file to the file system
                    File.WriteAllBytes(resultFileName, bytes);
                }
            }
        public HttpResponseMessage Get(bool isExcel, string file)
        {
            try
            {
                FileStream original   = File.Open(Utils._storagePath + "\\" + file, FileMode.OpenOrCreate);
                string     outputPath = Utils._storagePath + "\\Metadata_" + Path.GetFileNameWithoutExtension(file) + (isExcel ? ".xlsx" : ".csv");

                if (isExcel)
                {
                    byte[] content = ExportFacade.ExportToExcel(original);
                    // write data to the file
                    File.WriteAllBytes(outputPath, content);
                }
                else
                {
                    byte[] content = ExportFacade.ExportToCsv(original);
                    // write data to the file
                    File.WriteAllBytes(outputPath, content);
                }

                using (var ms = new MemoryStream())
                {
                    original = File.OpenRead(outputPath);
                    original.CopyTo(ms);
                    var result = new HttpResponseMessage(HttpStatusCode.OK)
                    {
                        Content = new ByteArrayContent(ms.ToArray())
                    };
                    result.Content.Headers.ContentDisposition =
                        new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
                    {
                        FileName = "Metadata_" + Path.GetFileNameWithoutExtension(file) + (isExcel ? ".xlsx" : ".csv")
                    };
                    result.Content.Headers.ContentType =
                        new MediaTypeHeaderValue("application/octet-stream");

                    original.Close();
                    File.Delete(outputPath);
                    return(result);
                }
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }
            /// <summary>
            /// Exports AVI metadata to Csv,Xls file
            /// </summary>
            public static void ExportMetadata()
            {
                try
                {
                    //ExStart:ExportMetadataAvi
                    // export to excel
                    byte[] content = ExportFacade.ExportToExcel(Common.MapSourceFilePath(filePath));

                    // write data to the file
                    File.WriteAllBytes(Common.MapDestinationFilePath(OutputDataFilePathAvi), content);
                    //ExEnd:ExportMetadataAvi
                    Console.WriteLine("Metadata has been export successfully");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        /// <summary>
        /// Exports metadata of specified file into specified type
        /// </summary>
        public static void ExportMetadata(string filePath, int exportType)
        {
            try
            {
                //ExStart:ExportMetadataAPI
                filePath = Common.MapSourceFilePath(filePath);

                if (exportType == ExportTypes.ToExcel)
                {
                    //ExStart:ExportMetadataToExcel
                    // path to the output file
                    string outputPath = Common.MapDestinationFilePath("metadata.xlsx");

                    // export to excel
                    byte[] content = ExportFacade.ExportToExcel(filePath);

                    // write data to the file
                    File.WriteAllBytes(outputPath, content);
                    //ExEnd:ExportMetadataToExcel
                }
                else if (exportType == ExportTypes.ToCSV)
                {
                    //ExStart:ExportMetadataToCVS
                    // path to the output file
                    string outputPath = Common.MapDestinationFilePath("metadata.csv");

                    // export to csv
                    byte[] content = ExportFacade.ExportToCsv(filePath);

                    // write data to the file
                    File.WriteAllBytes(outputPath, content);
                    //ExEnd:ExportMetadataToCVS
                }
                else
                {
                    //ExStart:ExportMetadataToDataSet
                    // export to DataSet
                    DataSet ds = ExportFacade.ExportToDataSet(filePath);
                    // get first table
                    DataTable products = ds.Tables[0];

                    // need to System.Data.DataSetExtension reference
                    IEnumerable <DataRow> query =
                        from product in products.AsEnumerable()
                        select product;

                    Console.WriteLine("Properties:");
                    foreach (DataRow p in query)
                    {
                        Console.Write(p.Field <string>("Metadata property"));
                        Console.Write(": ");
                        Console.WriteLine(p.Field <string>("Value"));
                    }
                    //ExEnd:ExportMetadataToDataSet
                }
                //ExEnd:ExportMetadataAPI
            }
            catch (Exception exp)
            {
                Console.WriteLine("Exception occurred: " + exp.Message);
            }
        }