1-----------------------------------------------------------------------
2--  util-http-parts -- HTTP Parts
3--  Copyright (C) 2011, 2012 Stephane Carrez
4--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
5--
6--  Licensed under the Apache License, Version 2.0 (the "License");
7--  you may not use this file except in compliance with the License.
8--  You may obtain a copy of the License at
9--
10--      http://www.apache.org/licenses/LICENSE-2.0
11--
12--  Unless required by applicable law or agreed to in writing, software
13--  distributed under the License is distributed on an "AS IS" BASIS,
14--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15--  See the License for the specific language governing permissions and
16--  limitations under the License.
17-----------------------------------------------------------------------
18with Ada.Directories;
19with Ada.IO_Exceptions;
20
21with Util.Log.Loggers;
22
23package body Util.Http.Parts is
24
25   Log : constant Util.Log.Loggers.Logger := Util.Log.Loggers.Create ("Util.Http.Parts", Util.Log.ERROR_LEVEL);
26
27   --  ------------------------------
28   --  Write the part data item to the file.  This method is not guaranteed to succeed
29   --  if called more than once for the same part. This allows a particular implementation
30   --  to use, for example, file renaming, where possible, rather than copying all of
31   --  the underlying data, thus gaining a significant performance benefit.
32   --  ------------------------------
33   procedure Save (Data : in Part;
34                   Path : in String) is
35      Old_Path : constant String := Part'Class (Data).Get_Local_Filename;
36   begin
37      Log.Info ("Saving uploaded file {0} to {1}", Old_Path, Path);
38
39      Ada.Directories.Rename (Old_Name => Old_Path,
40                              New_Name => Path);
41
42   exception
43      when Ada.IO_Exceptions.Use_Error =>
44         Log.Error ("Cannot save uploaded file");
45   end Save;
46
47   --  ------------------------------
48   --  Deletes the underlying storage for a file item, including deleting any associated
49   --  temporary disk file.
50   --  ------------------------------
51   procedure Delete (Data : in out Part) is
52      Path : constant String := Part'Class (Data).Get_Local_Filename;
53   begin
54      Ada.Directories.Delete_File (Path);
55   end Delete;
56
57end Util.Http.Parts;
58