1+ // --------------------------------------------------------------------------------------------------------------------
2+ // <copyright company="Aspose" file="CellsApiExtension.cs.cs">
3+ // Copyright (c) 2024 Aspose.Cells Cloud
4+ // </copyright>
5+ // <summary>
6+ // Permission is hereby granted, free of charge, to any person obtaining a copy
7+ // of this software and associated documentation files (the "Software"), to deal
8+ // in the Software without restriction, including without limitation the rights
9+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+ // copies of the Software, and to permit persons to whom the Software is
11+ // furnished to do so, subject to the following conditions:
12+ //
13+ // The above copyright notice and this permission notice shall be included in all
14+ // copies or substantial portions of the Software.
15+ //
16+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+ // SOFTWARE.
23+ // </summary>
24+ // --------------------------------------------------------------------------------------------------------------------
25+
26+ namespace Aspose . Cells . Cloud . SDK . Api
27+ {
28+ using Aspose . Cells . Cloud . SDK . Extensions ;
29+ using System ;
30+ using System . Collections . Generic ;
31+ using System . IO ;
32+ ///<summary>
33+ ///
34+ ///</summary>
35+ public static class CellsApiExtension
36+ {
37+ ///<summary>
38+ ///
39+ ///</summary>
40+ ///<param name="conversionRequest"></param>
41+ public static void Convert ( this CellsApi cellsApi , Request . ConversionRequest conversionRequest )
42+ {
43+ var password = string . Empty ;
44+ var outPath = string . Empty ;
45+ var storageName = string . Empty ;
46+ if ( ! File . Exists ( conversionRequest . InputPath ) )
47+ {
48+ throw new ApplicationException ( "Input file no exists." ) ;
49+ }
50+ if ( string . IsNullOrEmpty ( conversionRequest . OutputPath ) )
51+ {
52+ throw new ApplicationException ( "OutputPath is Empty." ) ;
53+ }
54+ Request . PutConvertWorkbookRequest putConvertWorkbookRequest = new Request . PutConvertWorkbookRequest ( ) ;
55+ putConvertWorkbookRequest . File = new Dictionary < string , Stream > ( ) ;
56+ putConvertWorkbookRequest . File . Add ( ( new System . IO . FileInfo ( conversionRequest . InputPath ) ) . Name , File . OpenRead ( conversionRequest . InputPath ) ) ;
57+ putConvertWorkbookRequest . format = conversionRequest . OutputPath . GetFileFormat ( ) ;
58+ putConvertWorkbookRequest . password = conversionRequest . password ;
59+ putConvertWorkbookRequest . outPath = conversionRequest . OutputPath ;
60+ putConvertWorkbookRequest . storageName = conversionRequest . StorageName ;
61+ putConvertWorkbookRequest . streamFormat = conversionRequest . InputPath . GetFileFormat ( ) ;
62+
63+ using ( Stream responseStream = cellsApi . PutConvertWorkbook ( putConvertWorkbookRequest ) )
64+ {
65+ if ( conversionRequest . SaveToCloud . HasValue && conversionRequest . SaveToCloud . Value )
66+ {
67+ responseStream . Close ( ) ;
68+ }
69+ else
70+ {
71+ Stream outStream = File . Create ( conversionRequest . OutputPath ) ;
72+ responseStream . CopyTo ( outStream ) ;
73+ outStream . Flush ( ) ;
74+ outStream . Close ( ) ;
75+ }
76+ }
77+ }
78+
79+ ///<summary>
80+ ///
81+ ///</summary>
82+ ///<param name="mergeRequest"></param>
83+ public static void Merge ( this CellsApi cellsApi , Request . MergeRequest mergeRequest )
84+ {
85+ if ( mergeRequest . InputFiles == null || mergeRequest . InputFiles . Length == 0 )
86+ {
87+ throw new ApplicationException ( "Input file no exists." ) ;
88+ }
89+ foreach ( string filename in mergeRequest . InputFiles )
90+ {
91+ if ( ! File . Exists ( filename ) )
92+ {
93+ throw new ApplicationException ( "Input file no exists." ) ;
94+ }
95+ }
96+
97+ if ( string . IsNullOrEmpty ( mergeRequest . OutputPath ) )
98+ {
99+ throw new ApplicationException ( "OutputPath is Empty." ) ;
100+ }
101+
102+ Request . PostMergeRequest request = new Request . PostMergeRequest ( ) ;
103+ request . File = new Dictionary < string , Stream > ( ) ;
104+ foreach ( string filename in mergeRequest . InputFiles )
105+ {
106+ request . File . Add ( ( new System . IO . FileInfo ( filename ) ) . Name , File . OpenRead ( filename ) ) ;
107+ }
108+
109+ request . outFormat = mergeRequest . OutputPath . GetFileFormat ( ) ;
110+ request . mergeToOneSheet = mergeRequest . mergeToOneSheet ;
111+
112+ Model . FileInfo fileInfo = cellsApi . PostMerge ( request ) ;
113+ Stream resultStream = fileInfo . FileContent . Base64StringToStream ( ) ;
114+ if ( mergeRequest . SaveToCloud . HasValue && mergeRequest . SaveToCloud . Value )
115+ {
116+ Request . UploadFileRequest uploadFileRequest = new Request . UploadFileRequest ( ) ;
117+ uploadFileRequest . path = mergeRequest . OutputPath ;
118+ uploadFileRequest . storageName = mergeRequest . StorageName ;
119+ uploadFileRequest . UploadFiles = new Dictionary < string , Stream > { { ( new System . IO . FileInfo ( fileInfo . Filename ) ) . Name , resultStream } } ;
120+ cellsApi . UploadFile ( uploadFileRequest ) ;
121+ }
122+ else
123+ {
124+ Stream outStream = File . Create ( mergeRequest . OutputPath ) ;
125+ resultStream . CopyTo ( outStream ) ;
126+ outStream . Flush ( ) ;
127+ outStream . Close ( ) ;
128+ resultStream . Close ( ) ;
129+ }
130+ }
131+ public static void Unlock ( this CellsApi cellsApi , Request . UnlockRequest unlockRequest )
132+ {
133+ if ( unlockRequest . InputPath == null )
134+ {
135+ throw new ApplicationException ( "Input file no exists." ) ;
136+ }
137+ if ( ! File . Exists ( unlockRequest . InputPath ) )
138+ {
139+ throw new ApplicationException ( "Input file no exists." ) ;
140+ }
141+ if ( string . IsNullOrEmpty ( unlockRequest . OutputPath ) )
142+ {
143+ throw new ApplicationException ( "OutputPath is Empty." ) ;
144+ }
145+ Request . PostUnlockRequest request = new Request . PostUnlockRequest ( ) ;
146+ request . File = new Dictionary < string , Stream > ( ) ;
147+ request . File . Add ( ( new System . IO . FileInfo ( unlockRequest . InputPath ) ) . Name , File . OpenRead ( unlockRequest . InputPath ) ) ;
148+
149+ request . password = unlockRequest . Password ;
150+
151+ Model . FilesResult filesResult = cellsApi . PostUnlock ( request ) ;
152+ foreach ( Model . FileInfo fileInfo in filesResult . Files )
153+ {
154+ Stream resultStream = fileInfo . FileContent . Base64StringToStream ( ) ;
155+ if ( unlockRequest . SaveToCloud . HasValue && unlockRequest . SaveToCloud . Value )
156+ {
157+ Request . UploadFileRequest uploadFileRequest = new Request . UploadFileRequest ( ) ;
158+ uploadFileRequest . path = unlockRequest . OutputPath ;
159+ uploadFileRequest . storageName = unlockRequest . StorageName ;
160+ uploadFileRequest . UploadFiles = new Dictionary < string , Stream > { { ( new System . IO . FileInfo ( fileInfo . Filename ) ) . Name , resultStream } } ;
161+ cellsApi . UploadFile ( uploadFileRequest ) ;
162+ }
163+ else
164+ {
165+ Stream outStream = File . Create ( unlockRequest . OutputPath ) ;
166+ resultStream . CopyTo ( outStream ) ;
167+ outStream . Flush ( ) ;
168+ outStream . Close ( ) ;
169+ resultStream . Close ( ) ;
170+ }
171+ }
172+
173+ }
174+ public static void Protect ( this CellsApi cellsApi , Request . ProtectRequest protectRequest )
175+ {
176+ if ( protectRequest . InputPath == null )
177+ {
178+ throw new ApplicationException ( "Input file no exists." ) ;
179+ }
180+ if ( ! File . Exists ( protectRequest . InputPath ) )
181+ {
182+ throw new ApplicationException ( "Input file no exists." ) ;
183+ }
184+ if ( string . IsNullOrEmpty ( protectRequest . OutputPath ) )
185+ {
186+ throw new ApplicationException ( "OutputPath is Empty." ) ;
187+ }
188+
189+ Request . PostProtectRequest request = new Request . PostProtectRequest ( ) ;
190+ request . File = new Dictionary < string , Stream > ( ) ;
191+ request . File . Add ( ( new System . IO . FileInfo ( protectRequest . InputPath ) ) . Name , File . OpenRead ( protectRequest . InputPath ) ) ;
192+
193+ request . password = protectRequest . Password ;
194+
195+ Model . FilesResult filesResult = cellsApi . PostProtect ( request ) ;
196+ foreach ( Model . FileInfo fileInfo in filesResult . Files )
197+ {
198+ Stream resultStream = fileInfo . FileContent . Base64StringToStream ( ) ;
199+ if ( protectRequest . SaveToCloud . HasValue && protectRequest . SaveToCloud . Value )
200+ {
201+ Request . UploadFileRequest uploadFileRequest = new Request . UploadFileRequest ( ) ;
202+ uploadFileRequest . path = protectRequest . OutputPath ;
203+ uploadFileRequest . storageName = protectRequest . StorageName ;
204+ uploadFileRequest . UploadFiles = new Dictionary < string , Stream > { { ( new System . IO . FileInfo ( fileInfo . Filename ) ) . Name , resultStream } } ;
205+ cellsApi . UploadFile ( uploadFileRequest ) ;
206+ }
207+ else
208+ {
209+ Stream outStream = File . Create ( protectRequest . OutputPath ) ;
210+ resultStream . CopyTo ( outStream ) ;
211+ outStream . Flush ( ) ;
212+ outStream . Close ( ) ;
213+ resultStream . Close ( ) ;
214+ }
215+ }
216+ }
217+ }
218+ }
0 commit comments