Class: AssetsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/assets_controller.rb

Overview

manage assets’creation inside the sharebox site

Instance Method Summary collapse

Methods inherited from ApplicationController

#check_lang, #prepare_attached_docs_request

Instance Method Details

#asset_paramsObject (private)



192
193
194
195
196
# File 'app/controllers/assets_controller.rb', line 192

def asset_params
  params.require(:asset).permit(:uploaded_file, :folder_id)
  # previous config when form was only composed of a file input
  #params.require(:asset).permit(:uploaded_file, :folder_id) if params[:asset]
end

#createObject

following the call to the new asset method, upload an asset and register it in the database
if the asset is a root file, we redirect to root else we redirect to the parent folder



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/controllers/assets_controller.rb', line 103

def create
    @asset = current_user.assets.new(asset_params)
    if @asset.save
      flash[:notice] = t('sb.uploaded')
      if @asset.folder_id
        redirect_to folder_path(@asset.folder_id)
      else
        redirect_to root_url
      end
    else
      if @asset.folder_id
        @current_folder = current_user.folders.find_by_id(@asset.folder_id)
      end
      render 'new'
    end
end

#delete_assetObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/controllers/assets_controller.rb', line 120

def delete_asset
  results={}
  if asset = current_user.assets.find_by_id(params[:id])
    if asset.destroy
      results["success"]=true
      results["message"]=t('sb.deleted')
    else
      results["success"]=false
      results["message"]=t('sb.not_deleted')
    end
  else
    results["success"]=false
    results["message"]= "#{t('sb.inexisting')} - #{t('sb.no_permission')}"
  end
  render json: results
end

#destroyObject

Destroy the asset
only for owners - to be fixed



140
141
142
143
144
145
146
147
148
149
# File 'app/controllers/assets_controller.rb', line 140

def destroy
  @asset = current_user.assets.find(params[:id])
  @asset.destroy
  flash[:notice] = t('sb.deleted')
  if @asset.folder_id
    redirect_to folder_path(@asset.folder_id)
  else
    redirect_to root_url
  end
end

#getObject

Permits the user to download a file



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'app/controllers/assets_controller.rb', line 154

def get
  #asset = current_user.assets.find_by_id(params[:id])
  asset = Asset.find_by_id(params[:id])

  if asset
    #case 1 : asset is a root file
    if !asset.folder_id
      if current_user.has_asset_ownership?(asset)
        get_file(asset)
      else
        flash[:notice] = t('sb.no_permission')
        redirect_to root_url
      end
    else
      #case 2 : asset belongs to a directory
      current_folder = Folder.find_by_id(asset.folder_id)
      if current_user.has_shared_access?(current_folder)
        #using the shared_folders message field to track file openings from a given share on folder
        if @shared_folder = SharedFolder.find_by_share_user_id_and_folder_id(current_user.id,asset.folder_id)
          n = @shared_folder.message.to_i + 1
          puts("*******trying to download a file from share number #{@shared_folder.id}")
          puts("*******tracked #{n} access from that share !!")
          @shared_folder.message= n
          @shared_folder.save
        end
        get_file(asset)
      else
        flash[:notice] = t('sb.no_permission')
        redirect_to root_url
      end
    end
  else
    flash[:notice] = t('sb.inexisting')
    redirect_to root_url
  end
end

#get_file(asset) ⇒ Object (private)

private method for file opening management
2 different options for file storage are possible :

  • 1) local storage in application_root/storage/

  • 2) Amazon S3 mode, in a cloud storage



203
204
205
# File 'app/controllers/assets_controller.rb', line 203

def get_file(asset)
  redirect_to url_for(asset.uploaded_file)
end

#indexObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/assets_controller.rb', line 13

def index
  # request preparation
  req=prepare_attached_docs_request
  fullreq=[]

  unless params[:folder_id]
      fullreq[0]=req.join("")
      fullreq.push('Asset')
      assets=Asset.find_by_sql(fullreq)
  else
      req.push(" and assets.folder_id = ?")
      fullreq[0]=req.join("")
      fullreq.push('Asset')
      fullreq.push(params[:folder_id])
      assets=Asset.find_by_sql(fullreq)
  end
  render json: assets
end

#newObject

Show the new form in order to upload a new asset
method used when following a route /folders/folder_id/new_file or /assets/new
/folders/folder_id/new_file will upload the asset in the folder identified by folder_id
/assets/new will upload the asset at the root of the user - such files are strictly personal and cannot be shared
Only admin or private users are able to upload files
They cannot upload files outside the folders they own



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/assets_controller.rb', line 51

def new
  unless (current_user.is_admin? || current_user.is_private?)
    flash[:notice] = t('sb.no_permission')
    redirect_to root_url
  end
  @asset = current_user.assets.new
  # If there is a folder_id, we attach the file to the corresponding folder<br>
  # if not, it will be a root located file
  if params[:folder_id]
    @hosting_folder = current_user.folders.find_by_id(params[:folder_id])
    if @hosting_folder
      @asset.folder_id = @hosting_folder.id
    else
      flash[:notice] = t('sb.no_permission')
      flash[:notice] = "#{flash[:notice]} - #{t('sb.inexisting_folder')}<br>"
      flash[:notice] = "#{flash[:notice]} - #{t('sb.folder_not_for_yu')}"
      redirect_to root_url
    end
  end
end

#showObject

Method used when following the route /assets/assets_id
Show the name of the file and its directory (forge/attachments/asset_id/asset_name)
actually not used.…



36
37
38
39
40
41
42
# File 'app/controllers/assets_controller.rb', line 36

def show
  if @asset = current_user.assets.find_by_id(params[:id])
      render json: @asset
  else
      render json: {id: false, message:"#{t('sb.inexisting')} - #{t('sb.no_permission')}"}
  end
end

#upload_assetObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/assets_controller.rb', line 72

def upload_asset
    results={}
    if params[:asset][:folder_id]==""
      params[:asset][:folder_id]=nil
    end
    if current_user.is_private? || current_user.is_admin?
        unless Folder.find_by_id(params[:asset][:folder_id]) || params[:asset][:folder_id].nil?
          results["success"]=false
          results["message"]="#{t('sb.stop')} - #{t('sb.inexisting_folder')}"
        else
       asset = current_user.assets.new(asset_params)
          if asset.save
            results["success"]=true
            results["message"]=t('sb.uploaded')
          else
            results["success"]=false
            result="#{t('sb.not_uploaded')}\n"
            result="#{result}#{t('sb.size_or_type_problem')}"
            results["message"]=result
          end
        end
    else
        results["success"]=false
        results["message"]=t('sb.no_permission')
    end
    render json: results
end