@@ -157,15 +157,25 @@ public function exec(string $command, string $log = '', ?int $siteId = null, ?bo
157
157
/**
158
158
* @throws Throwable
159
159
*/
160
- public function upload (string $ local , string $ remote ): void
160
+ public function upload (string $ local , string $ remote, ? string $ owner = null ): void
161
161
{
162
162
$ this ->log = null ;
163
163
164
164
if (! $ this ->connection instanceof SFTP ) {
165
165
$ this ->connect (true );
166
166
}
167
167
168
- $ this ->connection ->put ($ remote , $ local , SFTP ::SOURCE_LOCAL_FILE );
168
+ $ tmpName = Str::random (10 ).strtotime ('now ' );
169
+ $ tempPath = home_path ($ this ->user ).'/ ' .$ tmpName ;
170
+
171
+ $ this ->connection ->put ($ tempPath , $ local , SFTP ::SOURCE_LOCAL_FILE );
172
+
173
+ $ this ->exec (sprintf ('sudo mv %s %s ' , $ tempPath , $ remote ));
174
+ if (! $ owner ) {
175
+ $ owner = $ this ->user ;
176
+ }
177
+ $ this ->exec (sprintf ('sudo chown %s:%s %s ' , $ owner , $ owner , $ remote ));
178
+ $ this ->exec (sprintf ('sudo chmod 644 %s ' , $ remote ));
169
179
}
170
180
171
181
/**
@@ -185,22 +195,15 @@ public function download(string $local, string $remote): void
185
195
/**
186
196
* @throws SSHError
187
197
*/
188
- public function write (string $ remotePath , string $ content , bool $ sudo = false ): void
198
+ public function write (string $ remotePath , string $ content , ? string $ owner = null ): void
189
199
{
190
200
$ tmpName = Str::random (10 ).strtotime ('now ' );
191
201
192
202
try {
193
203
/** @var FilesystemAdapter $storageDisk */
194
204
$ storageDisk = Storage::disk ('local ' );
195
-
196
205
$ storageDisk ->put ($ tmpName , $ content );
197
-
198
- if ($ sudo ) {
199
- $ this ->upload ($ storageDisk ->path ($ tmpName ), sprintf ('/home/%s/%s ' , $ this ->server ->ssh_user , $ tmpName ));
200
- $ this ->exec (sprintf ('sudo mv /home/%s/%s %s ' , $ this ->server ->ssh_user , $ tmpName , $ remotePath ));
201
- } else {
202
- $ this ->upload ($ storageDisk ->path ($ tmpName ), $ remotePath );
203
- }
206
+ $ this ->upload ($ storageDisk ->path ($ tmpName ), $ remotePath , $ owner );
204
207
} catch (Throwable $ e ) {
205
208
throw new SSHCommandError (
206
209
message: $ e ->getMessage ()
0 commit comments